diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents.sln b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents.sln new file mode 100644 index 00000000..f361430c --- /dev/null +++ b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35617.110 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Drawing-Signature-Fields-in-Rotated-PDF-Documents", "Drawing-Signature-Fields-in-Rotated-PDF-Documents\Drawing-Signature-Fields-in-Rotated-PDF-Documents.csproj", "{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7BE57722-9807-4418-9F64-8BCD78D871A0} + EndGlobalSection +EndGlobal diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/PDF.pfx b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/PDF.pfx new file mode 100644 index 00000000..940afae4 Binary files /dev/null and b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/PDF.pfx differ diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/TestImage.png b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/TestImage.png new file mode 100644 index 00000000..35d18688 Binary files /dev/null and b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/TestImage.png differ diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/TestPDF.pdf b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/TestPDF.pdf new file mode 100644 index 00000000..1b7badeb Binary files /dev/null and b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Data/TestPDF.pdf differ diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents.csproj b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents.csproj new file mode 100644 index 00000000..17578b3a --- /dev/null +++ b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + Drawing_Signature_Fields_in_Rotated_PDF_Documents + enable + enable + + + + + + + diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Output/.gitkeep b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Program.cs b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Program.cs new file mode 100644 index 00000000..5b544e9f --- /dev/null +++ b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Program.cs @@ -0,0 +1,106 @@ +using Syncfusion.Pdf; +using System.IO; +using Syncfusion.Pdf.Parsing; +using Syncfusion.Drawing; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Security; + +internal class Program +{ + static void Main(string[] args) + { + //Open the Word document file stream. + using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/TestPDF.pdf"), FileMode.Open, FileAccess.Read)) + { + + PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(inputStream); + + PdfLoadedPage ldPage = pdfLoadedDocument.Pages[3] as PdfLoadedPage; + + //Create a certificate instance from a PFX file with a private key. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123"); + + PdfSignature signature = new PdfSignature(pdfLoadedDocument, ldPage, pdfCert, "Signature1"); + + RectangleF bounds = new RectangleF(new PointF(20, 20), new SizeF(240, 70)); + + + signature.Bounds = GetRelativeBounds(ldPage, bounds); + + PdfGraphics graphics = signature.Appearance.Normal.Graphics; + + FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/TestImage.png"), FileMode.Open, FileAccess.Read); + //Set an image for signature field. + PdfBitmap signatureImage = new PdfBitmap(imageStream); + + RotateSignatureAppearance(signatureImage, signature.Appearance.Normal.Graphics, ldPage.Rotation, signature.Bounds); + + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + pdfLoadedDocument.Save(outputFileStream); + } + //Closes the document + pdfLoadedDocument.Close(true); + + } + } + private static RectangleF GetRelativeBounds(PdfLoadedPage page, RectangleF bounds) + { + SizeF pagesize = page.Size; + RectangleF rectangle = bounds; + + if (page.Rotation == PdfPageRotateAngle.RotateAngle90) + { + rectangle.X = bounds.Y; + rectangle.Y = pagesize.Height - ((bounds.X + bounds.Width)); + rectangle.Width = bounds.Height; + rectangle.Height = bounds.Width; + } + else if (page.Rotation == PdfPageRotateAngle.RotateAngle270) + { + rectangle.Y = bounds.X; + rectangle.X = pagesize.Width - (bounds.Y + bounds.Height); + rectangle.Width = bounds.Height; + rectangle.Height = bounds.Width; + } + else if (page.Rotation == PdfPageRotateAngle.RotateAngle180) + { + rectangle.X = pagesize.Width - (bounds.X + bounds.Width); + rectangle.Y = pagesize.Height - (bounds.Y + bounds.Height); + } + return rectangle; + } + + private static void RotateSignatureAppearance(PdfImage image, PdfGraphics graphics, PdfPageRotateAngle angle, RectangleF bounds) + { + graphics.Save(); + + if (angle == PdfPageRotateAngle.RotateAngle90) + { + graphics.TranslateTransform(0, bounds.Height); + graphics.RotateTransform(-90); + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width)); + } + else if (angle == PdfPageRotateAngle.RotateAngle180) + { + graphics.TranslateTransform(bounds.Width, bounds.Height); + graphics.RotateTransform(-180); + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height)); + } + else if (angle == PdfPageRotateAngle.RotateAngle270) + { + graphics.TranslateTransform(bounds.Width, 0); + graphics.RotateTransform(-270); + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width)); + } + else + { + graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height)); + } + graphics.Restore(); + } + +} +