|
| 1 | +using Syncfusion.Pdf; |
| 2 | +using System.IO; |
| 3 | +using Syncfusion.Pdf.Parsing; |
| 4 | +using Syncfusion.Drawing; |
| 5 | +using Syncfusion.Pdf.Graphics; |
| 6 | +using Syncfusion.Pdf.Security; |
| 7 | + |
| 8 | +internal class Program |
| 9 | +{ |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + //Open the Word document file stream. |
| 13 | + using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/TestPDF.pdf"), FileMode.Open, FileAccess.Read)) |
| 14 | + { |
| 15 | + |
| 16 | + PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(inputStream); |
| 17 | + |
| 18 | + PdfLoadedPage ldPage = pdfLoadedDocument.Pages[3] as PdfLoadedPage; |
| 19 | + |
| 20 | + //Create a certificate instance from a PFX file with a private key. |
| 21 | + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); |
| 22 | + PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123"); |
| 23 | + |
| 24 | + PdfSignature signature = new PdfSignature(pdfLoadedDocument, ldPage, pdfCert, "Signature1"); |
| 25 | + |
| 26 | + RectangleF bounds = new RectangleF(new PointF(20, 20), new SizeF(240, 70)); |
| 27 | + |
| 28 | + |
| 29 | + signature.Bounds = GetRelativeBounds(ldPage, bounds); |
| 30 | + |
| 31 | + PdfGraphics graphics = signature.Appearance.Normal.Graphics; |
| 32 | + |
| 33 | + FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/TestImage.png"), FileMode.Open, FileAccess.Read); |
| 34 | + //Set an image for signature field. |
| 35 | + PdfBitmap signatureImage = new PdfBitmap(imageStream); |
| 36 | + |
| 37 | + RotateSignatureAppearance(signatureImage, signature.Appearance.Normal.Graphics, ldPage.Rotation, signature.Bounds); |
| 38 | + |
| 39 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) |
| 40 | + { |
| 41 | + //Save the PDF document to file stream. |
| 42 | + pdfLoadedDocument.Save(outputFileStream); |
| 43 | + } |
| 44 | + //Closes the document |
| 45 | + pdfLoadedDocument.Close(true); |
| 46 | + |
| 47 | + } |
| 48 | + } |
| 49 | + private static RectangleF GetRelativeBounds(PdfLoadedPage page, RectangleF bounds) |
| 50 | + { |
| 51 | + SizeF pagesize = page.Size; |
| 52 | + RectangleF rectangle = bounds; |
| 53 | + |
| 54 | + if (page.Rotation == PdfPageRotateAngle.RotateAngle90) |
| 55 | + { |
| 56 | + rectangle.X = bounds.Y; |
| 57 | + rectangle.Y = pagesize.Height - ((bounds.X + bounds.Width)); |
| 58 | + rectangle.Width = bounds.Height; |
| 59 | + rectangle.Height = bounds.Width; |
| 60 | + } |
| 61 | + else if (page.Rotation == PdfPageRotateAngle.RotateAngle270) |
| 62 | + { |
| 63 | + rectangle.Y = bounds.X; |
| 64 | + rectangle.X = pagesize.Width - (bounds.Y + bounds.Height); |
| 65 | + rectangle.Width = bounds.Height; |
| 66 | + rectangle.Height = bounds.Width; |
| 67 | + } |
| 68 | + else if (page.Rotation == PdfPageRotateAngle.RotateAngle180) |
| 69 | + { |
| 70 | + rectangle.X = pagesize.Width - (bounds.X + bounds.Width); |
| 71 | + rectangle.Y = pagesize.Height - (bounds.Y + bounds.Height); |
| 72 | + } |
| 73 | + return rectangle; |
| 74 | + } |
| 75 | + |
| 76 | + private static void RotateSignatureAppearance(PdfImage image, PdfGraphics graphics, PdfPageRotateAngle angle, RectangleF bounds) |
| 77 | + { |
| 78 | + graphics.Save(); |
| 79 | + |
| 80 | + if (angle == PdfPageRotateAngle.RotateAngle90) |
| 81 | + { |
| 82 | + graphics.TranslateTransform(0, bounds.Height); |
| 83 | + graphics.RotateTransform(-90); |
| 84 | + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width)); |
| 85 | + } |
| 86 | + else if (angle == PdfPageRotateAngle.RotateAngle180) |
| 87 | + { |
| 88 | + graphics.TranslateTransform(bounds.Width, bounds.Height); |
| 89 | + graphics.RotateTransform(-180); |
| 90 | + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height)); |
| 91 | + } |
| 92 | + else if (angle == PdfPageRotateAngle.RotateAngle270) |
| 93 | + { |
| 94 | + graphics.TranslateTransform(bounds.Width, 0); |
| 95 | + graphics.RotateTransform(-270); |
| 96 | + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width)); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height)); |
| 101 | + } |
| 102 | + graphics.Restore(); |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
0 commit comments