FindTransformed.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 Transforms sample,
  13. '' finds all occurrences of a string in the loaded document,
  14. '' and highlights these occurrences. Two points of interest about this sample:
  15. '' - The texts in the original document are graphically transformed,
  16. '' but the quadrilaterals supplied by the FindText method allows you to easily
  17. '' highlight the finds even in that case.
  18. '' - The sample inserts a new content stream at index 0 of the page,
  19. '' this ensures that the highlighting is drawn UNDER the original content.
  20. '' (The same approach may be used to add watermarks etc. to existing files.)
  21. Public Class FindTransformed
  22. Function CreatePDF(ByVal stream As Stream) As Integer
  23. Dim doc = New GcPdfDocument()
  24. '' The original file stream must be kept open while working with the loaded PDF, see LoadPDF for details:
  25. Using fs = New FileStream(Path.Combine("Resources", "PDFs", "Transforms.pdf"), FileMode.Open, FileAccess.Read)
  26. doc.Load(fs)
  27. '' Find all 'Text drawn at', using case-sensitive search:
  28. Dim finds = doc.FindText(
  29. New FindTextParams("Text drawn at", False, True),
  30. OutputRange.All)
  31.  
  32. '' Highlight all finds: first, find all pages where the text was found
  33. Dim pgIndices = finds.Select(Function(f_) f_.PageIndex).Distinct()
  34. '' Loop through pages, on each page insert a new content stream at index 0,
  35. '' so that our highlights go UNDER the original content:
  36. For Each pgIdx In pgIndices
  37. Dim page = doc.Pages(pgIdx)
  38. Dim pcs = page.ContentStreams.Insert(0)
  39. Dim g = pcs.GetGraphics(page)
  40. For Each find In finds.Where(Function(f_) f_.PageIndex = pgIdx)
  41. For Each ql In find.Bounds
  42. '' Note the solid color used to fill the polygon:
  43. g.FillPolygon(ql, Color.CadetBlue)
  44. g.DrawPolygon(ql, Color.Blue)
  45. Next
  46. Next
  47. Next
  48. ''
  49. '' Done:
  50. doc.Save(stream)
  51. End Using
  52. Return doc.Pages.Count
  53. End Function
  54. End Class
  55.