VintaSoft Imaging .NET SDK 12.4: Documentation for .NET developer
Vintasoft.Imaging.Pdf.Tree.Annotations Namespace / PdfTextMarkupAnnotation Class
Members Object Syntax Example Hierarchy Requirements SeeAlso
In This Topic
    PdfTextMarkupAnnotation Class
    In This Topic
    Represents a PDF annotation that can appears as highlight, underline, strikeout, or jagged ("squiggly") underline in the text of document.
    Object Model
    PdfPopupAnnotation PdfAnnotation PdfFileReferenceSpecificationList PdfAnnotationAppearanceGenerator PdfOptionalContentGroup PdfPage PdfAnnotationBorderStyle AffineMatrix PdfAction PdfAnnotationAppearances PdfAnnotationAdditionalActions PdfDocument PdfIndirectReference PdfBasicObject PdfTextMarkupAnnotation
    Syntax
    'Declaration
    
    Public Class PdfTextMarkupAnnotation
       Inherits PdfMarkupAnnotation
    
    
    public class PdfTextMarkupAnnotation : PdfMarkupAnnotation
    
    
    public __gc class PdfTextMarkupAnnotation : public PdfMarkupAnnotation*
    
    
    public ref class PdfTextMarkupAnnotation : public PdfMarkupAnnotation^
    
    
    Example

    Here is example that shows how to markup a text using PDF text markup annotation:

    
    ''' <summary>
    ''' Highlights specified text in PDF document.
    ''' </summary>
    ''' <param name="inputPdfFilename">The input PDF filename.</param>
    ''' <param name="outputPdfFilename">The output PDF filename.</param>
    ''' <param name="text">A text that must be highlighted.</param>
    Public Shared Sub HiglightTextInPdfDocument(inputPdfFilename As String, outputPdfFilename As String, text As String)
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Highlight, System.Drawing.Color.Orange, text)
    End Sub
    
    ''' <summary>
    ''' Strikeouts specified text in PDF document.
    ''' </summary>
    ''' <param name="inputPdfFilename">The input PDF filename.</param>
    ''' <param name="outputPdfFilename">The output PDF filename.</param>
    ''' <param name="text">A text that must be striked out.</param>
    Public Shared Sub StrikeoutTextInPdfDocument(inputPdfFilename As String, outputPdfFilename As String, text As String)
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.StrikeOut, System.Drawing.Color.Red, text)
    End Sub
    
    ''' <summary>
    ''' Underlines specified text in PDF document.
    ''' </summary>
    ''' <param name="inputPdfFilename">The input PDF filename.</param>
    ''' <param name="outputPdfFilename">The output PDF filename.</param>
    ''' <param name="text">A text that must be underlined.</param>
    Public Shared Sub UnderlineTextInPdfDocument(inputPdfFilename As String, outputPdfFilename As String, text As String)
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Underline, System.Drawing.Color.Green, text)
    End Sub
    
    ''' <summary>
    ''' Squiggly undeline specified text in PDF document.
    ''' </summary>
    ''' <param name="inputPdfFilename">The input PDF filename.</param>
    ''' <param name="outputPdfFilename">The output PDF filename.</param>
    ''' <param name="text">A text that must be squiggly underlined.</param>
    Public Shared Sub SquigglyUnderlineTextInPdfDocument(inputPdfFilename As String, outputPdfFilename As String, text As String)
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Squiggly, System.Drawing.Color.Blue, text)
    End Sub
    
    ''' <summary>
    ''' Markups specified text in PDF document.
    ''' </summary>
    ''' <param name="inputPdfFilename">The input PDF filename.</param>
    ''' <param name="outputPdfFilename">The output PDF filename.</param>
    ''' <param name="color">The markup annotation color.</param>
    ''' <param name="annotationType">The markup annotation type.</param>
    ''' <param name="text">A text to markup.</param>
    Private Shared Sub MarkupTextInPdfDocument(inputPdfFilename As String, outputPdfFilename As String, annotationType As Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType, color As System.Drawing.Color, text As String)
        ' open input PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(inputPdfFilename)
            ' for each PDF page
            For Each page As Vintasoft.Imaging.Pdf.Tree.PdfPage In document.Pages
                ' extract text region of PDF page
                Dim pageTextRegion As Vintasoft.Imaging.Text.TextRegion = page.ExtractTextRegion(False)
    
                ' search and markup all specified text on PDF page
                Dim index As Integer = 0
                Dim foundText As Vintasoft.Imaging.Text.TextRegion
                While True
                    ' find next text
                    foundText = pageTextRegion.FindText(text, index, False)
                    If foundText Is Nothing Then
                        Exit While
                    End If
                    index += foundText.Symbols.Length
    
                    ' markup found text
                    MarkupText(page, foundText, annotationType, color)
                End While
            Next
    
            ' save PDF document to output file
            document.SaveChanges(outputPdfFilename)
        End Using
    End Sub
    
    ''' <summary>
    ''' Markups specified text region in PDF page.
    ''' </summary>
    ''' <param name="page">The page.</param>
    ''' <param name="textRegion">The text region to markup.</param>
    ''' <param name="color">The markup annotation color.</param>
    ''' <param name="annotationType">The markup annotation type.</param>
    Private Shared Sub MarkupText(page As Vintasoft.Imaging.Pdf.Tree.PdfPage, textRegion As Vintasoft.Imaging.Text.TextRegion, annotationType As Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType, color As System.Drawing.Color)
        ' create markup annotation of specified type
        Dim markupAnnotation As New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfTextMarkupAnnotation(page, annotationType)
    
        ' set annotation properties
        markupAnnotation.SetArea(textRegion)
        markupAnnotation.Color = color
        markupAnnotation.Title = System.Environment.UserName
        markupAnnotation.CreationDate = System.DateTime.Now
    
        ' generate annotation appearance
        markupAnnotation.UpdateAppearance()
    
        ' add annotation to annotations of PDF page
        If page.Annotations Is Nothing Then
            page.Annotations = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(page.Document)
        End If
        page.Annotations.Add(markupAnnotation)
    End Sub
    
    
    
    /// <summary>
    /// Highlights specified text in PDF document.
    /// </summary>
    /// <param name="inputPdfFilename">The input PDF filename.</param>
    /// <param name="outputPdfFilename">The output PDF filename.</param>
    /// <param name="text">A text that must be highlighted.</param>
    public static void HiglightTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
    {
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Highlight, System.Drawing.Color.Orange, text);
    }
    
    /// <summary>
    /// Strikeouts specified text in PDF document.
    /// </summary>
    /// <param name="inputPdfFilename">The input PDF filename.</param>
    /// <param name="outputPdfFilename">The output PDF filename.</param>
    /// <param name="text">A text that must be striked out.</param>
    public static void StrikeoutTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
    {
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.StrikeOut, System.Drawing.Color.Red, text);
    }
    
    /// <summary>
    /// Underlines specified text in PDF document.
    /// </summary>
    /// <param name="inputPdfFilename">The input PDF filename.</param>
    /// <param name="outputPdfFilename">The output PDF filename.</param>
    /// <param name="text">A text that must be underlined.</param>
    public static void UnderlineTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
    {
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Underline, System.Drawing.Color.Green, text);
    }
    
    /// <summary>
    /// Squiggly undeline specified text in PDF document.
    /// </summary>
    /// <param name="inputPdfFilename">The input PDF filename.</param>
    /// <param name="outputPdfFilename">The output PDF filename.</param>
    /// <param name="text">A text that must be squiggly underlined.</param>
    public static void SquigglyUnderlineTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
    {
        MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Squiggly, System.Drawing.Color.Blue, text);
    }
    
    /// <summary>
    /// Markups specified text in PDF document.
    /// </summary>
    /// <param name="inputPdfFilename">The input PDF filename.</param>
    /// <param name="outputPdfFilename">The output PDF filename.</param>
    /// <param name="color">The markup annotation color.</param>
    /// <param name="annotationType">The markup annotation type.</param>
    /// <param name="text">A text to markup.</param>
    private static void MarkupTextInPdfDocument(
        string inputPdfFilename,
        string outputPdfFilename,
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType annotationType,
        System.Drawing.Color color,
        string text)
    {
        // open input PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(inputPdfFilename))
        {
            // for each PDF page
            foreach (Vintasoft.Imaging.Pdf.Tree.PdfPage page in document.Pages)
            {
                // extract text region of PDF page
                Vintasoft.Imaging.Text.TextRegion pageTextRegion = page.ExtractTextRegion(false);
    
                // search and markup all specified text on PDF page
                int index = 0;
                Vintasoft.Imaging.Text.TextRegion foundText;
                while (true)
                {
                    // find next text
                    foundText = pageTextRegion.FindText(text, ref index, false);
                    if (foundText == null)
                        break;
                    index += foundText.Symbols.Length;
    
                    // markup found text
                    MarkupText(page, foundText, annotationType, color);
                }
            }
    
            // save PDF document to output file
            document.SaveChanges(outputPdfFilename);
        }
    }
    
    /// <summary>
    /// Markups specified text region in PDF page.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="textRegion">The text region to markup.</param>
    /// <param name="color">The markup annotation color.</param>
    /// <param name="annotationType">The markup annotation type.</param>
    private static void MarkupText(
        Vintasoft.Imaging.Pdf.Tree.PdfPage page,
        Vintasoft.Imaging.Text.TextRegion textRegion,
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType annotationType,
        System.Drawing.Color color)
    {
        // create markup annotation of specified type
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfTextMarkupAnnotation markupAnnotation =
            new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfTextMarkupAnnotation(page, annotationType);
    
        // set annotation properties
        markupAnnotation.SetArea(textRegion);
        markupAnnotation.Color = color;
        markupAnnotation.Title = System.Environment.UserName;
        markupAnnotation.CreationDate = System.DateTime.Now;
    
        // generate annotation appearance
        markupAnnotation.UpdateAppearance();
    
        // add annotation to annotations of PDF page
        if (page.Annotations == null)
            page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(page.Document);
        page.Annotations.Add(markupAnnotation);
    }
    
    

    Inheritance Hierarchy

    System.Object
       Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase
          Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation
             Vintasoft.Imaging.Pdf.Tree.Annotations.PdfMarkupAnnotation
                Vintasoft.Imaging.Pdf.Tree.Annotations.PdfTextMarkupAnnotation

    Requirements

    Target Platforms: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    See Also