AddCaretAnnotation.cs
C# VB
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.IO;using System.Drawing;using GrapeCity.Documents.Pdf;using GrapeCity.Documents.Pdf.Annotations;
namespace DsPdfWeb.Demos{ // This example shows how to create a caret annotation used to indicate a specific position in a text. public class AddCaretAnnotation { public int CreatePDF(Stream stream) { using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf")); var doc = new GcPdfDocument(); doc.Load(fs);
foreach (var page in doc.Pages) { var tm = page.GetTextMap(); // insert the CaretAnnotation after "The Importance" text tm.FindText(new FindTextParams("wetlands", false, false), (fp_) => { // Add a CaretAnnotation near the found text: var r = fp_.Bounds[0].ToRect(); var ca = new CaretAnnotation { Page = page, Rect = new RectangleF(r.Left - 8, r.Bottom - 16, 16, 16) }; }); } // Done: doc.Save(stream); return doc.Pages.Count; } }}