FindText.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.IO
  6. Imports System.Drawing
  7. Imports System.Text
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Common
  10. Imports GrapeCity.Documents.Drawing
  11.  
  12. '' This sample loads the PDF file created by the BalancedColumns sample,
  13. '' finds all occurrences of the words 'lorem' and 'ipsum' in the loaded document,
  14. '' and highlights these two words using different colors.
  15. Public Class FindText
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. Dim doc = New GcPdfDocument()
  18.  
  19. '' The original file stream must be kept open while working with the loaded PDF, see LoadPDF for details:
  20. Using fs = New FileStream(Path.Combine("Resources", "PDFs", "BalancedColumns.pdf"), FileMode.Open, FileAccess.Read)
  21. doc.Load(fs)
  22. '' Find all 'lorem', using case-insensitive word search:
  23. Dim findsLorem = doc.FindText(
  24. New FindTextParams("lorem", True, False),
  25. OutputRange.All)
  26. '' Ditto for 'ipsum':
  27. Dim findsIpsum = doc.FindText(
  28. New FindTextParams("ipsum", True, False),
  29. OutputRange.All)
  30. '' Highlight all 'lorem' using semi-transparent orange red:
  31. For Each find In findsLorem
  32. For Each ql In find.Bounds
  33. doc.Pages(find.PageIndex).Graphics.FillPolygon(ql, Color.FromArgb(100, Color.OrangeRed))
  34. Next
  35. Next
  36. '' Put a violet red border around all 'ipsum':
  37. For Each find In findsIpsum
  38. For Each ql In find.Bounds
  39. doc.Pages(find.PageIndex).Graphics.DrawPolygon(ql, Color.MediumVioletRed)
  40. Next
  41. Next
  42. ''
  43. '' Done:
  44. doc.Save(stream)
  45. End Using
  46. Return doc.Pages.Count
  47. End Function
  48. End Class
  49.