FindAndUnderline.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 GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Pdf.Annotations;
  11. using System.Collections.Generic;
  12. using GrapeCity.Documents.Common;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This example shows how to find all occurrences of two words in a PDF document and
  17. // underline those occurrences using the text markup underline annotation.
  18. public class FindAndUnderline
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. // Load the PDF:
  23. var doc = new GcPdfDocument();
  24. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "The-Rich-History-of-JavaScript.pdf"));
  25. doc.Load(fs);
  26.  
  27. // Find all occurrences of "JavaScript" or "ECMAScript" using regex:
  28. var ft = new FindTextParams("JavaScript|ECMAScript", wholeWord: false, matchCase: false, regex: true);
  29. var found = doc.FindText(ft, null);
  30.  
  31. // Add a text markup annotation to underline each occurrence:
  32. foreach (var f in found)
  33. {
  34. var markup = new TextMarkupAnnotation()
  35. {
  36. Page = doc.Pages[f.PageIndex],
  37. MarkupType = TextMarkupType.Underline,
  38. Color = Color.Red
  39. };
  40. List<Quadrilateral> area = new List<Quadrilateral>();
  41. foreach (var b in f.Bounds)
  42. area.Add(b);
  43. markup.Area = area;
  44. }
  45.  
  46. // Done:
  47. doc.Save(stream);
  48. return doc.Pages.Count;
  49. }
  50. }
  51. }
  52.