FindAndSquiggly.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 phrase in a PDF document and
  14. '' highlight all those occurrences using the text markup squiggly underline annotation.
  15. '' To ensure that all occurrences of the phrase ("javascript framework") are found
  16. '' even if the words are separated by a line break, regular expressions are turned
  17. '' on in the search.
  18. Public Class FindAndSquiggly
  19. Public Function CreatePDF(stream As Stream) As Integer
  20. ' Load an existing PDF:
  21. Dim doc As New GcPdfDocument()
  22. Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf"))
  23. doc.Load(fs)
  24.  
  25. ' Find all occurrences of the phrase "javascript framework" using regex so that
  26. ' words separated by line breaks are found:
  27. Dim ft As New FindTextParams("javascript\s+framework", False, False, , , , True)
  28. Dim found = doc.FindText(ft, Nothing)
  29.  
  30. ' Add a text markup annotation to wavy underline each occurrence:
  31. For Each f In found
  32. Dim markup As New TextMarkupAnnotation() With {
  33. .Page = doc.Pages(f.PageIndex),
  34. .MarkupType = TextMarkupType.Squiggly,
  35. .Color = Color.Magenta
  36. }
  37. Dim area As List(Of Quadrilateral) = New List(Of Quadrilateral)
  38. For Each b In f.Bounds
  39. area.Add(b)
  40. Next
  41. markup.Area = area
  42. Next
  43.  
  44. ' Done:
  45. doc.Save(stream)
  46. Return doc.Pages.Count
  47. End Using
  48. End Function
  49. End Class
  50.