Signing a PDF using a visual signature with a .pfx file

PDF TIFF SVG JPG C# VB
VisualSignature.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Pdf.AcroForms;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using System.Security.Cryptography.X509Certificates;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsPdfWeb.Demos
  17. {
  18. // This sample demonstrates how to create and sign a PDF with a .pfx file,
  19. // using a SignatureField and a signature image.
  20. // The sample then loads the signed file back into another GcPdfDocument instance
  21. // and verifies the signature.
  22. // This sample is identical to SignDoc, but adds an image representing the signature.
  23. public class VisualSignature
  24. {
  25. public int CreatePDF(Stream stream)
  26. {
  27. var doc = new GcPdfDocument();
  28. var page = doc.NewPage();
  29. var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
  30. page.Graphics.DrawString("Hello, World!\n" +
  31. "Signed by DsPdfWeb VisualSignature sample.",
  32. tf, new PointF(72, 72));
  33.  
  34. // Init a test certificate:
  35. var pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx");
  36. var cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
  37. X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
  38. var sp = new SignatureProperties()
  39. {
  40. SignatureBuilder = new Pkcs7SignatureBuilder()
  41. {
  42. CertificateChain = new X509Certificate2[] { cert }
  43. },
  44. Location = "DsPdfWeb Demo Browser",
  45. SignerName = "DsPdfWeb",
  46. };
  47.  
  48. // Add an image representing the signature:
  49. sp.SignatureAppearance.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "signature.png"));
  50. sp.SignatureAppearance.CaptionImageRelation = GrapeCity.Documents.Pdf.Annotations.CaptionImageRelation.ImageOnly;
  51.  
  52. // Init a signature field to hold the signature:
  53. var sf = new SignatureField();
  54. sf.Widget.Rect = new RectangleF(72, 72 * 2, 72 * 4, 36);
  55. sf.Widget.Page = page;
  56. sf.Widget.BackColor = Color.LightSeaGreen;
  57. sf.Widget.DefaultAppearance.Font = StandardFonts.Helvetica;
  58. sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}\r\nLocation: {sp.Location}";
  59. // Add the signature field to the document:
  60. doc.AcroForm.Fields.Add(sf);
  61. // Connect the signature field and signature props:
  62. sp.SignatureField = sf;
  63.  
  64. // Sign and save the document:
  65. // NOTES:
  66. // - Signing and saving is an atomic operation, the two cannot be separated.
  67. // - The stream passed to the Sign() method must be readable.
  68. doc.Sign(sp, stream);
  69.  
  70. // Rewind the stream to read the document just created
  71. // into another GcPdfDocument and verify the signature:
  72. stream.Seek(0, SeekOrigin.Begin);
  73. var doc2 = new GcPdfDocument();
  74. doc2.Load(stream);
  75. SignatureField sf2 = (SignatureField)doc2.AcroForm.Fields[0];
  76. if (!sf2.Value.VerifySignatureValue())
  77. throw new Exception("Failed to verify the signature");
  78.  
  79. // Done (the generated and signed document has already been saved to 'stream').
  80. return doc.Pages.Count;
  81. }
  82. }
  83. }
  84.