VintaSoft Imaging .NET SDK v8.7
In This Topic
    PDF: Working with PDF annotations of PDF document
    In This Topic

    VintaSoft PDF .NET Plug-in provides the functionality for non-visual low-level work with annotations of PDF document.
    A combination of VintaSoft PDF .NET Plug-in and VintaSoft Annotation .NET Plug-in provides the functionality for full-featured visual and non-visual annotating of PDF document in WinForms, WPF and ASP.NET. For detailed information about functionality of VintaSoft Annotation .NET Plug-in goto here.


    PdfAnnotation class defines annotation of PDF document and allows to:


    Here is the hierarchy of classes, that defines standard annotations of PDF document:

    Here is an example that demonstrates how to obtain information about all annotations of PDF page:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Gets and prints information about all annotations of PDF page.
    ''' </summary>
    ''' <param name="pdfFileName">The filename of PDF document.</param>
    Public Shared Sub PrintAnnotationsInfo(pdfFileName As String)
            ' open PDF document
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFileName)
                    ' for each PDF page
                    For pageIndex As Integer = 0 To document.Pages.Count - 1
                            ' get PDF page
                            Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(pageIndex)
                            ' get a collection of annotations of PDF page
                            Dim annotations As Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList = page.Annotations
                            If annotations Is Nothing Then
                                    System.Console.WriteLine("Page {0}: no annotations.", pageIndex + 1)
                            Else
                                    ' print the page index and count of annotations
                                    System.Console.WriteLine("Page {0} Annotation count: {1}", pageIndex + 1, annotations.Count)
                                    ' for each annotation
                                    For Each annotation As Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation In annotations
                                            ' print information about annotation
                                            System.Console.WriteLine("Annotation:")
                                            System.Console.WriteLine("           Name: ""{0}""", annotation.Name)
                                            System.Console.WriteLine(" Title (Author): ""{0}""", annotation.Title)
                                            System.Console.WriteLine("        Subject: ""{0}""", annotation.Subject)
                                            System.Console.WriteLine("       Contents: ""{0}""", annotation.Contents)
                                            System.Console.WriteLine("AppearanceState: ""{0}""", annotation.AppearanceState)
                                            System.Console.WriteLine("       Modified: {0}", annotation.Modified)
                                            System.Console.WriteLine("          Flags: {0}", annotation.Flags)
                                            System.Console.WriteLine()
                                    Next
                            End If
                    Next
            End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Gets and prints information about all annotations of PDF page.
    /// </summary>
    /// <param name="pdfFileName">The filename of PDF document.</param>
    public static void PrintAnnotationsInfo(string pdfFileName)
    {
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument(pdfFileName))
        {
            // for each PDF page
            for (int pageIndex = 0; pageIndex < document.Pages.Count; pageIndex++)
            {
                // get PDF page
                Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages[pageIndex];
                // get a collection of annotations of PDF page
                Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList annotations = page.Annotations;
                if (annotations == null)
                {
                    System.Console.WriteLine("Page {0}: no annotations.", pageIndex + 1);
                }
                else
                {
                    // print the page index and count of annotations
                    System.Console.WriteLine("Page {0} Annotation count: {1}", pageIndex + 1, annotations.Count);
                    // for each annotation
                    foreach (Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation annotation in annotations)
                    {
                        // print information about annotation
                        System.Console.WriteLine("Annotation:");
                        System.Console.WriteLine("           Name: \"{0}\"", annotation.Name);
                        System.Console.WriteLine(" Title (Author): \"{0}\"", annotation.Title);
                        System.Console.WriteLine("        Subject: \"{0}\"", annotation.Subject);
                        System.Console.WriteLine("       Contents: \"{0}\"", annotation.Contents);
                        System.Console.WriteLine("AppearanceState: \"{0}\"", annotation.AppearanceState);
                        System.Console.WriteLine("       Modified: {0}", annotation.Modified);
                        System.Console.WriteLine("          Flags: {0}", annotation.Flags);
                        System.Console.WriteLine();
                    }
                }
            }
        }
    }
                    
    



    Create new PDF annotation

    To create a new annotation it is necessary to do the following:

    Here is an example that demonstrates how to create an annotation, which consists from red rectangle:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Creates an annotation, which consists from red rectangle.
    ''' </summary>
    ''' <param name="page">The page.</param>
    ''' <returns>The created annotation.</returns>
    Public Shared Function CreatePdfAnnotation(page As Vintasoft.Imaging.Pdf.Tree.PdfPage) As Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation
            ' create a rectangular annotation
            Dim annotation As New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfSquareAnnotation(page)
            ' set interior color
            annotation.InteriorColor = System.Drawing.Color.Red
            ' set rectangle
            annotation.Rectangle = New System.Drawing.RectangleF(40, 40, 160, 80)
            ' create graphics for normal appearance
            Using pdfGraphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = annotation.CreateNormalAppearanceGraphics()
                    ' draw an appearance of the annotation
                    pdfGraphics.FillRectangle(New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Red), 0, 0, annotation.Rectangle.Width, annotation.Rectangle.Height)
            End Using
    
            ' return created annotation
            Return annotation
    End Function
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Creates an annotation, which consists from red rectangle.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <returns>The created annotation.</returns>
    public static Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation CreatePdfAnnotation(
        Vintasoft.Imaging.Pdf.Tree.PdfPage page)
    {
        // create a rectangular annotation
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfSquareAnnotation annotation = 
            new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfSquareAnnotation(page);
        // set interior color
        annotation.InteriorColor = System.Drawing.Color.Red;
        // set rectangle
        annotation.Rectangle = new System.Drawing.RectangleF(40, 40, 160, 80);
        // create graphics for normal appearance
        using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics pdfGraphics = annotation.CreateNormalAppearanceGraphics())
        {
            // draw an appearance of the annotation
            pdfGraphics.FillRectangle(
                new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Red), 
                0, 0, 
                annotation.Rectangle.Width, annotation.Rectangle.Height);
        }
    
        // return created annotation
        return annotation;
    }
                    
    



    Add annotation onto PDF page

    To add a new annotation onto PDF page it is necessary to do the following:

    Here is an example that demonstrates how to create a line annotation and add it onto PDF page:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Creates a Line annotation and adds it onto PDF page.
    ''' </summary>
    ''' <param name="pdfFilename">The filename of PDF document.</param>
    Public Shared Sub AddLineAnnotationOntoPage(pdfFilename As String)
            ' open PDF document
            Using pdfDocument As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
                    ' get first page of the document
                    Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = pdfDocument.Pages(0)
                    ' create line annotation
                    Dim lineAnnotation As New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfLineAnnotation(page)
                    ' set color of the annotation
                    lineAnnotation.Color = System.Drawing.Color.Green
                    ' set rectangle of the annotation
                    lineAnnotation.Rectangle = page.CropBox
                    ' set start point of the annotation
                    lineAnnotation.StartPoint = New System.Drawing.PointF(0, 0)
                    ' set end point of the annotation
                    lineAnnotation.EndPoint = New System.Drawing.PointF(page.CropBox.Width, page.CropBox.Height)
                    ' create graphics for normal appearance
                    Using pdfGraphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = lineAnnotation.CreateNormalAppearanceGraphics()
                            ' draw appearance of the annotation
                            pdfGraphics.DrawLine(New Vintasoft.Imaging.Pdf.Drawing.PdfPen(System.Drawing.Color.Green, 3F), 0, 0, lineAnnotation.Rectangle.Width, lineAnnotation.Rectangle.Height)
                    End Using
                    ' if there is no annotations
                    If page.Annotations Is Nothing Then
                            ' create collection of annotations of the page
                            page.Annotations = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(pdfDocument)
                    End If
                    ' add the annotation to the collection
                    page.Annotations.Add(lineAnnotation)
                    ' save changes to the source
                    pdfDocument.SaveChanges()
            End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Creates a Line annotation and adds it onto PDF page.
    /// </summary>
    /// <param name="pdfFilename">The filename of PDF document.</param>
    public static void AddLineAnnotationOntoPage(string pdfFilename)
    {
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
        {
            // get first page of the document
            Vintasoft.Imaging.Pdf.Tree.PdfPage page = pdfDocument.Pages[0];
            // create line annotation
            Vintasoft.Imaging.Pdf.Tree.Annotations.PdfLineAnnotation lineAnnotation = 
                new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfLineAnnotation(page);
            // set color of the annotation
            lineAnnotation.Color = System.Drawing.Color.Green;
            // set rectangle of the annotation
            lineAnnotation.Rectangle = page.CropBox;
            // set start point of the annotation
            lineAnnotation.StartPoint = new System.Drawing.PointF(0, 0);
            // set end point of the annotation
            lineAnnotation.EndPoint = new System.Drawing.PointF(page.CropBox.Width, page.CropBox.Height);
            // create graphics for normal appearance
            using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics pdfGraphics = lineAnnotation.CreateNormalAppearanceGraphics())
            {
                // draw appearance of the annotation
                pdfGraphics.DrawLine(new Vintasoft.Imaging.Pdf.Drawing.PdfPen(System.Drawing.Color.Green, 3f),
                    0, 0, lineAnnotation.Rectangle.Width, lineAnnotation.Rectangle.Height);
            }
            // if there is no annotations
            if (page.Annotations == null)
                // create collection of annotations of the page
                page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(pdfDocument);
            // add the annotation to the collection
            page.Annotations.Add(lineAnnotation);
            // save changes to the source
            pdfDocument.SaveChanges();
        }
    }
                    
    



    Delete annotation from PDF page

    To delete an annotation from PDF page it is necessary to do the following:

    Here is an example that demonstrates how to delete annotation from PDF page:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Removes the first annotation from PDF page
    ''' if it is a link annotation.
    ''' </summary>
    ''' <param name="page">The page of PDF document.</param>
    Public Shared Function RemoveAnnotationFromPdfPage(page As Vintasoft.Imaging.Pdf.Tree.PdfPage) As Boolean
            ' if annotations exist
            If page.Annotations IsNot Nothing AndAlso page.Annotations.Count > 0 Then
                    ' if contents of the first annotation if it is a link
                    If TypeOf page.Annotations(0) Is Vintasoft.Imaging.Pdf.Tree.Annotations.PdfLinkAnnotation Then
                            ' remove the annotation
                            page.Annotations.RemoveAt(0)
                            Return True
                    End If
            End If
    
            Return False
    End Function
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Removes the first annotation from PDF page
    /// if it is a link annotation.
    /// </summary>
    /// <param name="page">The page of PDF document.</param>
    public static bool RemoveAnnotationFromPdfPage(Vintasoft.Imaging.Pdf.Tree.PdfPage page)
    {
        // if annotations exist
        if (page.Annotations != null && page.Annotations.Count > 0)
        {
            // if contents of the first annotation if it is a link
            if (page.Annotations[0] is Vintasoft.Imaging.Pdf.Tree.Annotations.PdfLinkAnnotation)
            {
                // remove the annotation
                page.Annotations.RemoveAt(0);
                return true;
            }
        }
    
        return false;
    }