AddCaretAnnotation.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.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This example shows how to create a caret annotation used to indicate a specific position in a text.
  15. public class AddCaretAnnotation
  16. {
  17. public int CreatePDF(Stream stream)
  18. {
  19. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"));
  20. var doc = new GcPdfDocument();
  21. doc.Load(fs);
  22.  
  23. foreach (var page in doc.Pages)
  24. {
  25. var tm = page.GetTextMap();
  26. // insert the CaretAnnotation after "The Importance" text
  27. tm.FindText(new FindTextParams("wetlands", false, false), (fp_) =>
  28. {
  29. // Add a CaretAnnotation near the found text:
  30. var r = fp_.Bounds[0].ToRect();
  31. var ca = new CaretAnnotation();
  32. ca.Page = page;
  33. ca.Rect = new RectangleF(r.Left - 8, r.Bottom - 16, 16, 16);
  34. });
  35. }
  36. // Done:
  37. doc.Save(stream);
  38. return doc.Pages.Count;
  39. }
  40. }
  41. }
  42.