ReplaceText2.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 replace all occurrences of a text string in a PDF.
  16. public class ReplaceText2
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. var doc = new GcPdfDocument();
  21. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "gc-docs-data-sheet.pdf"));
  22. doc.Load(fs);
  23.  
  24. // Replace:
  25. doc.ReplaceText(new FindTextParams(".NET Standard 2.0", false, true), ".NET 6");
  26.  
  27. // For reference, append the original PDF:
  28. Common.Util.AddNote("For reference, the following pages contain a copy of the original unmodified PDF.", doc.NewPage());
  29. var docOrig = new GcPdfDocument();
  30. docOrig.Load(fs);
  31. doc.MergeWithDocument(docOrig);
  32.  
  33. // Done:
  34. doc.Save(stream);
  35. return doc.Pages.Count;
  36. }
  37. }
  38. }
  39.