FindAndSquiggly.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 a phrase in a PDF document and
  17. // highlight all those occurrences using the text markup squiggly underline annotation.
  18. // To ensure that all occurrences of the phrase ("javascript framework") are found
  19. // even if the words are separated by a line break, a regular expressions is used
  20. // in the search string.
  21. public class FindAndSquiggly
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. // Load the PDF:
  26. var doc = new GcPdfDocument();
  27. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf"));
  28. doc.Load(fs);
  29.  
  30. // Find all occurrences of the phrase "javascript framework" using regex so that
  31. // words separated by line breaks are found:
  32. var ft = new FindTextParams("javascript\\s+framework", wholeWord: false, matchCase: false, regex: true);
  33. var found = doc.FindText(ft, null);
  34.  
  35. // Add a text markup annotation to wavy underline each occurrence:
  36. foreach (var f in found)
  37. {
  38. var markup = new TextMarkupAnnotation()
  39. {
  40. Page = doc.Pages[f.PageIndex],
  41. MarkupType = TextMarkupType.Squiggly,
  42. Color = Color.Magenta
  43. };
  44. List<Quadrilateral> area = new List<Quadrilateral>();
  45. foreach (var b in f.Bounds)
  46. area.Add(b);
  47. markup.Area = area;
  48. }
  49.  
  50. // Done:
  51. doc.Save(stream);
  52. return doc.Pages.Count;
  53. }
  54. }
  55. }
  56.