Document Solutions for PDF
Features / Annotations / Caret Annotation
In This Topic
    Caret Annotation
    In This Topic

    Caret annotation is a visual symbol typically used to mark text for some changes or to indicate some missing text. DsPdf provides CaretAnnotation class that allows a user to add Caret annotations to the PDF file.

    CaretAnnotation class provides the following properties to set various options for the caret annotation:

    Property Description
    UserName Adds the user name to the text label in the title bar of the annotation’s pop-up window when the annotation is open and active.
    Subject Adds the text representing the subject of the annotation.
    Contents Adds the text to the annotation for display.
    RichText Adds the text to the annotation for display in the pop-up window when opened. You can format this text using HTML tags.
    Opacity Sets the opacity of the annotation.
    Color Sets the annotation color, popup window color, line color, etc.
    Rect Adds the rectangle that defines the location and size of the annotation on a page. The coordinates of the rectangle are relative to the upper left corner of the page's media box, with the Y (vertical) coordinates increasing from top to bottom.

    Refer to the following example code to add a caret annotation to a PDF document:

    C#
    Copy Code
    // Initialize GcPdfDocument.
    GcPdfDocument doc = new GcPdfDocument();
    
    // Create a file stream.
    FileStream fs = new FileStream("Wetlands.pdf", FileMode.Open, FileAccess.Read);
    
    // Load the file stream.
    doc.Load(fs);
    
    // Get the first page.
    var page = doc.Pages[0];
                
    // Get the text map.
    var tm = page.GetTextMap();
    
    // Insert the CaretAnnotation after "The Importance" text.
    tm.FindText(new FindTextParams("The Importance", false, true), (fp_) =>
    {
        // r is bounds of the found text.
        var r = fp_.Bounds[0].ToRect();
        // Create the CaretAnnotation and add it to the page.
        CaretAnnotation ca = new CaretAnnotation();
        ca.Page = page;
        // in this code annotation size is hardcoded, you can make a code
        // which will adjust size of the annotation depending on height of the found text fragment
        ca.Rect = new System.Drawing.RectangleF(r.Right - 4, r.Bottom - 8, 8, 8);
        ca.Opacity = 1f;
        ca.Color = Color.Red;
        ca.Contents = "This is Caret annotation.";
    });
    
    doc.Save("CaretAnnotation.pdf");