FindAndHighlight.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System
  6. Imports System.IO
  7. Imports System.Drawing
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Pdf.Annotations
  11. Imports GrapeCity.Documents.Common
  12.  
  13. ' This example shows how to find all occurrences of a word in a PDF document
  14. ' and highlight all those occurrences using the text markup highlight annotation.
  15. '
  16. ' The PDF used in this example was downloaded from https://www.frontiersin.org/articles/10.3389/fendo.2022.1005722/full.
  17. Public Class FindAndHighlight
  18. Public Function CreatePDF(stream As Stream) As Integer
  19. ' Load an existing PDF:
  20. Dim doc = New GcPdfDocument()
  21. Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "fendo-13-1005722.pdf"))
  22. doc.Load(fs)
  23.  
  24. ' Find all occurrences of the word "childbirths":
  25. Dim found = doc.FindText(New FindTextParams("childbirths", True, False), Nothing)
  26.  
  27. ' Add a text markup annotation to highlight each occurrence:
  28. For Each f In found
  29. Dim markup = New TextMarkupAnnotation() With {
  30. .Page = doc.Pages(f.PageIndex),
  31. .MarkupType = TextMarkupType.Highlight,
  32. .Color = Color.Yellow
  33. }
  34. Dim area As List(Of Quadrilateral) = New List(Of Quadrilateral)
  35. For Each b In f.Bounds
  36. area.Add(b)
  37. Next
  38. markup.Area = area
  39. Next
  40.  
  41. ' Done:
  42. doc.Save(stream)
  43. Return doc.Pages.Count
  44. End Using
  45. End Function
  46. End Class
  47.