MarkupPerWord.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.Pdf.TextMap;
  13. using GrapeCity.Documents.Common;
  14.  
  15. namespace DsPdfWeb.Demos
  16. {
  17. // This example shows how to find all occurrences of two words in a PDF document and
  18. // highlight each of those words with different color using text markup highlight annotations.
  19. // Points of interest in this code:
  20. // - We use separate FindText() calls for each word. To speed up the searches,
  21. // for each page we cache the text map and reuse it in the two FindText() calls.
  22. // While the performance improvement is minor in the case of two words, it would
  23. // be significant if the task were to find multiple words (e.g. when building a word index,
  24. // as illustrated in the WordIndex example which uses a similar approach.
  25. // - In order to highlight each of the two words using a different color,
  26. // on each page we create one highlight markup annotation per word.
  27. public class MarkupPerWord
  28. {
  29. public int CreatePDF(Stream stream)
  30. {
  31. const string word0 = "JavaScript";
  32. const string word1 = "framework";
  33.  
  34. // Load the PDF:
  35. var doc = new GcPdfDocument();
  36. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf"));
  37. doc.Load(fs);
  38.  
  39. // A FindTextParams for each of the search words:
  40. var ft0 = new FindTextParams(word0, wholeWord: false, matchCase: false, regex: false);
  41. var ft1 = new FindTextParams(word1, wholeWord: false, matchCase: false, regex: false);
  42.  
  43. // Find and highlight words on the pages, caching and reusing the text map
  44. // of each page for the two searches:
  45. foreach (var p in doc.Pages)
  46. {
  47. // Find and highlight the first word:
  48. var textMap = p.GetTextMap();
  49. TextMarkupAnnotation markup0 = null;
  50. List<Quadrilateral> area0 = null;
  51. textMap.FindText(ft0, fp_ => {
  52. markup0 ??= new TextMarkupAnnotation()
  53. {
  54. Subject = word0,
  55. Page = doc.Pages[fp_.PageIndex],
  56. MarkupType = TextMarkupType.Highlight,
  57. Color = Color.Cyan
  58. };
  59. area0 ??= new List<Quadrilateral>();
  60. foreach (var b in fp_.Bounds)
  61. area0.Add(b);
  62. });
  63. if (markup0 != null)
  64. markup0.Area = area0;
  65.  
  66. // Find and highlight the second word:
  67. TextMarkupAnnotation markup1 = null;
  68. List<Quadrilateral> area1 = null;
  69. textMap.FindText(ft1, fp_ => {
  70. markup1 ??= new TextMarkupAnnotation()
  71. {
  72. Subject = word1,
  73. Page = doc.Pages[fp_.PageIndex],
  74. MarkupType = TextMarkupType.Highlight,
  75. Color = Color.Fuchsia
  76. };
  77. area1 ??= new List<Quadrilateral>();
  78. foreach (var b in fp_.Bounds)
  79. area1.Add(b);
  80. });
  81. if (markup1 != null)
  82. markup1.Area = area1;
  83. }
  84.  
  85. // Done:
  86. doc.Save(stream);
  87. return doc.Pages.Count;
  88. }
  89. }
  90. }
  91.