DeleteText.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 GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Pdf.AcroForms;
  12.  
  13. namespace DsPdfWeb.Demos
  14. {
  15. // This example shows how to find and delete all occurrences of a text string in a PDF.
  16. public class DeleteText
  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. doc.Load(fs);
  23.  
  24. // Delete dates and times using regex pattern:
  25. var ftps = new FindTextParams(@"\d+/\d+/\d+|\d+:\d+| am| pm", false, false, 72, 72, false, true);
  26. doc.DeleteText(ftps, GrapeCity.Documents.Pdf.TextMap.DeleteTextMode.PreserveSpace);
  27.  
  28. // For reference, append the original PDF:
  29. Common.Util.AddNote("For reference, the following page contains a copy of the original unmodified PDF.", doc.NewPage());
  30. var docOrig = new GcPdfDocument();
  31. docOrig.Load(fs);
  32. doc.MergeWithDocument(docOrig);
  33.  
  34. // Done:
  35. doc.Save(stream);
  36. return doc.Pages.Count;
  37. }
  38. }
  39. }
  40.