SignPAdESBB.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 System.Text;
  9. using System.Collections.Generic;
  10. using System.Security.Cryptography;
  11. using System.Security.Cryptography.X509Certificates;
  12.  
  13. using GrapeCity.Documents.Pdf;
  14. using GrapeCity.Documents.Pdf.Security;
  15. using GrapeCity.Documents.Pdf.AcroForms;
  16. using GrapeCity.Documents.Text;
  17.  
  18.  
  19. namespace DsPdfWeb.Demos
  20. {
  21. // This sample shows how to sign an existing PDF file containing
  22. // an empty signature field with a signature that complies with
  23. // PAdES (PDF Advanced Electronic Signatures) B-B level standard.
  24. public class SignPAdESBB
  25. {
  26. public int CreatePDF(Stream stream)
  27. {
  28. var doc = new GcPdfDocument();
  29. using var s = File.OpenRead(Path.Combine("Resources", "PDFs", "SignPAdESBB.pdf"));
  30. doc.Load(s);
  31.  
  32. var pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx");
  33. var cert = new X509Certificate2(pfxPath, "qq");
  34. var sp = SignatureProperties.CreatePAdES_B_B(cert);
  35. sp.SignatureAppearance.Caption = "PAdES B-B";
  36. sp.SignatureField = doc.AcroForm.Fields[0];
  37. doc.Sign(sp, stream);
  38.  
  39. // Done.
  40. return doc.Pages.Count;
  41. }
  42. }
  43. }
  44.