|
| 1 | +using PdfSharpCore.Drawing; |
| 2 | +using PdfSharpCore.Pdf; |
| 3 | +using System.Security.Cryptography.X509Certificates; |
| 4 | +using PdfSharpCore.Pdf.IO; |
| 5 | +using PdfSharpCore.Drawing.Layout; |
| 6 | +using PdfSharpCore.Pdf.Signatures; |
| 7 | +using System; |
| 8 | + |
| 9 | +namespace TestConsole |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + public static void Main(string[] args) |
| 14 | + { |
| 15 | + Program.CreateAndSign(); |
| 16 | + Program.SignExisting(); |
| 17 | + } |
| 18 | + |
| 19 | + private static void CreateAndSign() |
| 20 | + { |
| 21 | + string text = "CreateAndSign.pdf"; |
| 22 | + XFont font = new XFont("Verdana", 10.0, XFontStyle.Regular); |
| 23 | + PdfDocument pdfDocument = new PdfDocument(); |
| 24 | + PdfPage pdfPage = pdfDocument.AddPage(); |
| 25 | + XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage); |
| 26 | + XRect layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width, pdfPage.Height); |
| 27 | + xGraphics.DrawString("Sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter); |
| 28 | + PdfSignatureOptions options = new PdfSignatureOptions |
| 29 | + { |
| 30 | + ContactInfo = "Contact Info", |
| 31 | + Location = "Paris", |
| 32 | + Reason = "Test signatures", |
| 33 | + Rectangle = new XRect(36.0, 700.0, 200.0, 50.0) |
| 34 | + }; |
| 35 | + PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler( new DefaultSigner(Program.GetCertificate()), null, options); |
| 36 | + pdfSignatureHandler.AttachToDocument(pdfDocument); |
| 37 | + pdfDocument.Save(text); |
| 38 | + } |
| 39 | + private static void SignExisting() |
| 40 | + { |
| 41 | + string text = string.Format("SignExisting.pdf", new object[0]); |
| 42 | + PdfDocument pdfDocument = PdfReader.Open("TestFiles\\doc1.pdf"); |
| 43 | + PdfSignatureOptions options = new PdfSignatureOptions |
| 44 | + { |
| 45 | + ContactInfo = "Contact Info", |
| 46 | + Location = "Paris", |
| 47 | + Reason = "Test signatures", |
| 48 | + Rectangle = new XRect(36.0, 735.0, 200.0, 50.0), |
| 49 | + AppearanceHandler = new Program.SignAppearenceHandler() |
| 50 | + }; |
| 51 | + |
| 52 | + PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler(new DefaultSigner(Program.GetCertificate()), null, options); |
| 53 | + pdfSignatureHandler.AttachToDocument(pdfDocument); |
| 54 | + pdfDocument.Save(text); |
| 55 | + } |
| 56 | + |
| 57 | + private static X509Certificate2 GetCertificate() |
| 58 | + { |
| 59 | + // add yours here |
| 60 | + return new X509Certificate2("TestFiles\\myself.pfx", "1234", X509KeyStorageFlags.Exportable); |
| 61 | + } |
| 62 | + |
| 63 | + private class SignAppearenceHandler : ISignatureAppearanceHandler |
| 64 | + { |
| 65 | + private XImage Image = XImage.FromFile("TestFiles\\logo.jpg"); |
| 66 | + public void DrawAppearance(XGraphics gfx, XRect rect) |
| 67 | + { |
| 68 | + XColor empty = XColor.Empty; |
| 69 | + string text = "Signed by Napoleon \nLocation: Paris \nDate: " + DateTime.Now.ToString(); |
| 70 | + XFont font = new XFont("Verdana", 7.0, XFontStyle.Regular); |
| 71 | + XTextFormatter xTextFormatter = new XTextFormatter(gfx); |
| 72 | + int num = this.Image.PixelWidth / this.Image.PixelHeight; |
| 73 | + XPoint xPoint = new XPoint(0.0, 0.0); |
| 74 | + bool flag = this.Image != null; |
| 75 | + if (flag) |
| 76 | + { |
| 77 | + gfx.DrawImage(this.Image, xPoint.X, xPoint.Y, rect.Width / 4.0, rect.Width / 4.0 / (double)num); |
| 78 | + xPoint = new XPoint(rect.Width / 4.0, 0.0); |
| 79 | + } |
| 80 | + xTextFormatter.DrawString(text, font, new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)), new XRect(xPoint.X, xPoint.Y, rect.Width - xPoint.X, rect.Height), XStringFormats.TopLeft); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments