diff --git a/PdfSharpCore.sln b/PdfSharpCore.sln index 4343b14f..26e461ed 100644 --- a/PdfSharpCore.sln +++ b/PdfSharpCore.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29911.84 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32519.379 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PdfSharpCore", "PdfSharpCore\PdfSharpCore.csproj", "{4005DEBC-75F8-48DA-BBCC-353E17872B3E}" EndProject @@ -18,6 +18,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PdfSharpCore.Test", "PdfSharpCore.Test\PdfSharpCore.Test.csproj", "{A862C0CE-C095-459C-A32B-8FCDD15A93BF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignatureTestConsole", "SignatureTestConsole\SignatureTestConsole.csproj", "{272E99C1-3210-497B-8CCE-D561E353FC13}" + ProjectSection(ProjectDependencies) = postProject + {4005DEBC-75F8-48DA-BBCC-353E17872B3E} = {4005DEBC-75F8-48DA-BBCC-353E17872B3E} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,6 +49,10 @@ Global {A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Debug|Any CPU.Build.0 = Debug|Any CPU {A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Release|Any CPU.ActiveCfg = Release|Any CPU {A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Release|Any CPU.Build.0 = Release|Any CPU + {272E99C1-3210-497B-8CCE-D561E353FC13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {272E99C1-3210-497B-8CCE-D561E353FC13}.Debug|Any CPU.Build.0 = Debug|Any CPU + {272E99C1-3210-497B-8CCE-D561E353FC13}.Release|Any CPU.ActiveCfg = Release|Any CPU + {272E99C1-3210-497B-8CCE-D561E353FC13}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SignatureTestConsole/Program.cs b/SignatureTestConsole/Program.cs new file mode 100644 index 00000000..07129999 --- /dev/null +++ b/SignatureTestConsole/Program.cs @@ -0,0 +1,82 @@ +using PdfSharpCore.Drawing; +using PdfSharpCore.Pdf; +using System.Security.Cryptography.X509Certificates; +using PdfSharpCore.Pdf.IO; +using PdfSharpCore.Drawing.Layout; +using PdfSharpCore.Pdf.Signatures; + +namespace TestConsole +{ + class Program + { + public static void Main(string[] args) + { + Program.CreateAndSign(); + Program.SignExisting(); + } + + private static void CreateAndSign() + { + string text = "CreateAndSign.pdf"; + XFont font = new XFont("Verdana", 10.0, XFontStyle.Regular); + PdfDocument pdfDocument = new PdfDocument(); + PdfPage pdfPage = pdfDocument.AddPage(); + XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage); + XRect layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width, pdfPage.Height); + xGraphics.DrawString("Sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter); + PdfSignatureOptions options = new PdfSignatureOptions + { + ContactInfo = "Contact Info", + Location = "Paris", + Reason = "Test signatures", + Rectangle = new XRect(36.0, 700.0, 200.0, 50.0) + }; + PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler( new DefaultSigner(Program.GetCertificate()), null, options); + pdfSignatureHandler.AttachToDocument(pdfDocument); + pdfDocument.Save(text); + } + private static void SignExisting() + { + string text = string.Format("SignExisting.pdf", new object[0]); + PdfDocument pdfDocument = PdfReader.Open("TestFiles\\doc1.pdf"); + PdfSignatureOptions options = new PdfSignatureOptions + { + ContactInfo = "Contact Info", + Location = "Paris", + Reason = "Test signatures", + Rectangle = new XRect(36.0, 735.0, 200.0, 50.0), + AppearanceHandler = new Program.SignAppearenceHandler() + }; + + PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler(new DefaultSigner(Program.GetCertificate()), null, options); + pdfSignatureHandler.AttachToDocument(pdfDocument); + pdfDocument.Save(text); + } + + private static X509Certificate2 GetCertificate() + { + return new X509Certificate2("C:\\code.siemens.com\\pdf\\cert\\myself.pfx", "1234", X509KeyStorageFlags.Exportable); + } + + private class SignAppearenceHandler : ISignatureAppearanceHandler + { + private XImage Image = XImage.FromFile("TestFiles\\logo.jpg"); + public void DrawAppearance(XGraphics gfx, XRect rect) + { + XColor empty = XColor.Empty; + string text = "Signed by Napoleon \nLocation: Paris \nDate: " + DateTime.Now.ToString(); + XFont font = new XFont("Verdana", 7.0, XFontStyle.Regular); + XTextFormatter xTextFormatter = new XTextFormatter(gfx); + int num = this.Image.PixelWidth / this.Image.PixelHeight; + XPoint xPoint = new XPoint(0.0, 0.0); + bool flag = this.Image != null; + if (flag) + { + gfx.DrawImage(this.Image, xPoint.X, xPoint.Y, rect.Width / 4.0, rect.Width / 4.0 / (double)num); + xPoint = new XPoint(rect.Width / 4.0, 0.0); + } + xTextFormatter.DrawString(text, font, new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)), new XRect(xPoint.X, xPoint.Y, rect.Width - xPoint.X, rect.Height), XStringFormats.TopLeft); + } + } + } +} diff --git a/SignatureTestConsole/SignatureTestConsole.csproj b/SignatureTestConsole/SignatureTestConsole.csproj new file mode 100644 index 00000000..70b279fb --- /dev/null +++ b/SignatureTestConsole/SignatureTestConsole.csproj @@ -0,0 +1,23 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/SignatureTestConsole/TestFiles/doc1.pdf b/SignatureTestConsole/TestFiles/doc1.pdf new file mode 100644 index 00000000..2838868e Binary files /dev/null and b/SignatureTestConsole/TestFiles/doc1.pdf differ diff --git a/SignatureTestConsole/TestFiles/logo.jpg b/SignatureTestConsole/TestFiles/logo.jpg new file mode 100644 index 00000000..7ea75b00 Binary files /dev/null and b/SignatureTestConsole/TestFiles/logo.jpg differ