SignWithP12.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 a PDF with a .p12 file
  22. // containing a digital certificate that uses PKCS#12
  23. // (Public Key Cryptography Standard #12) encryption.
  24. public class SignWithP12
  25. {
  26. public int CreatePDF(Stream stream)
  27. {
  28. var doc = new GcPdfDocument();
  29. using var s = File.OpenRead(Path.Combine("Resources", "PDFs", "SignWithP12.pdf"));
  30. doc.Load(s);
  31.  
  32. var p12 = Path.Combine("Resources", "Misc", "DsPdfTest2KeyStore.p12");
  33. var sp = new SignatureProperties()
  34. {
  35. SignatureBuilder = new Pkcs7SignatureBuilder()
  36. {
  37. CertificateChain = SecurityUtils.GetCertificateChain(p12, "qq"),
  38. },
  39. SignatureField = doc.AcroForm.Fields[0],
  40. };
  41. sp.SignatureAppearance.Caption = "PKCS #12";
  42. doc.Sign(sp, stream);
  43.  
  44. // Done.
  45. return doc.Pages.Count;
  46. }
  47. }
  48. }
  49.