RemoveSignatureFields.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 signature fields from a PDF.
  12. // The code in this sample is almost identical to the code in RemoveSignatures,
  13. // which also finds all signature fields, but removes just the signatures,
  14. // leaving the fields.
  15. // The PDF used in this sample was created by TimeSheet.
  16. public class RemoveSignatureFields
  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. removeSignatureFields(doc.AcroForm.Fields);
  28.  
  29. // Done:
  30. doc.Save(stream);
  31. return doc.Pages.Count;
  32.  
  33. void removeSignatureFields(FieldCollection fields)
  34. {
  35. for (int i = fields.Count - 1; i >= 0; --i)
  36. {
  37. removeSignatureFields(fields[i].Children);
  38. // Note: if we just wanted to remove the signature
  39. // without removing the field itself, we would do:
  40. // ((SignatureField)fields[i]).Value = null;
  41. if (fields[i] is SignatureField)
  42. fields.RemoveAt(i);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49.