RemoveSignatures.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.IO;
  6. using GrapeCity.Documents.Pdf;
  7. using GrapeCity.Documents.Pdf.AcroForms;
  8.  
  9. namespace DsPdfWeb.Demos
  10. {
  11. // This sample shows how to find and remove existing signatures
  12. // from a PDF that had been digitally signed.
  13. // See the RemoveSignatureFields for code that removes any
  14. // signature fields instead.
  15. // The PDF used in this sample was created by TimeSheet.
  16. public class RemoveSignatures
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. var doc = new GcPdfDocument();
  21. using (var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "TimeSheet.pdf")))
  22. {
  23. doc.Load(fs);
  24.  
  25. // Fields can be children of other fields, so we use
  26. // a recursive method to iterate through the whole tree:
  27. removeSignatures(doc.AcroForm.Fields);
  28.  
  29. // Done:
  30. doc.Save(stream);
  31. return doc.Pages.Count;
  32.  
  33. void removeSignatures(FieldCollection fields)
  34. {
  35. foreach (var f in fields)
  36. {
  37. if (f is SignatureField sf)
  38. sf.Value = null;
  39. removeSignatures(f.Children);
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46.