VintaSoft Imaging .NET SDK 12.3: Documentation for .NET developer
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. Detailed information about functionality of VintaSoft Annotation .NET Plug-in please see 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:
    /// <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();
                    }
                }
            }
        }
    }
    
    ''' <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
    



    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:
    /// <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;
    }
    
    ''' <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
    



    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:
    /// <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();
        }
    }
    
    ''' <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
    



    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:
    /// <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;
    }
    
    ''' <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
    



    Change appearance of PDF annotation

    The PdfAnnotation.AppearanceGenerator property allows to get or set the appearance generator of PDF annotation. Custom appearance generator must be created if annotation appearance must be changed. PDF Editor Demo has an example of custom appearance generator for PDF signature field - the SignatureAppearanceGenerator class, which can be found in file "<install_path>\Examples\WinForms\CSharp\PdfEditorDemo\DemosCommonCode.Pdf\AnnotationTool\FormFields\AppearanceGenerators\Signature\SignatureAppearanceGenerator.cs".


    Add comments to PDF annotation

    For working with comments of PDF page is necessary to do the following steps:
    Here is an example that demonstrates how to print comments comments of specified PDF document:
    /// <summary>
    /// Demonstrates how to prints comments of specified PDF document.
    /// </summary>
    public class PdfAnnotationCommentExample
    {
        /// <summary>
        /// Prints comments of specified PDF document.
        /// </summary>
        /// <param name="pdfFilename">The PDF filename.</param>
        public static void PrintComments(string pdfFilename)
        {
            // create an image collection
            using (Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection())
            {
                // add PDF document to the image collection
                images.Add(pdfFilename);
    
                // create PDF annotation comment controller
                using (Vintasoft.Imaging.Annotation.Comments.Pdf.ImageCollectionPdfAnnotationCommentController pdfAnnotationCommentController =
                    new Vintasoft.Imaging.Annotation.Comments.Pdf.ImageCollectionPdfAnnotationCommentController())
                {
                    // specify that PDF annotation comment controller is associated with image collection
                    pdfAnnotationCommentController.Images = images;
    
                    // print comments from PDF annotation comment controller
                    PrintComments(pdfAnnotationCommentController);
                }
    
                // clear and dispose images
                images.ClearAndDisposeItems();
            }
        }
    
        /// <summary>
        /// Prints comments from specified comment controller.
        /// </summary>
        /// <param name="commentController">The comment controller.</param>
        public static void PrintComments(Vintasoft.Imaging.Annotation.Comments.ImageCollectionCommentController commentController)
        {
            // for each image
            for (int i = 0; i < commentController.Images.Count; i++)
            {
                Vintasoft.Imaging.VintasoftImage image = commentController.Images[i];
    
                // get comment collection, which is associated with image/PDF page
                Vintasoft.Imaging.Annotation.Comments.CommentCollection comments = commentController.GetComments(image);
                // if comments are found
                if (comments != null && comments.Count > 0)
                {
                    System.Console.WriteLine(string.Format("Page {0}:", i + 1));
    
                    // for each comment
                    foreach (Vintasoft.Imaging.Annotation.Comments.Comment comment in comments)
                    {
                        // print comment
                        PrintComment(comment, 0);
                    }
                }
            }
        }
    
        /// <summary>
        /// Prints the comment.
        /// </summary>
        /// <param name="comment">The comment.</param>
        /// <param name="replyLevel">The reply level.</param>
        private static void PrintComment(Vintasoft.Imaging.Annotation.Comments.Comment comment, int replyLevel)
        {
            // print comment
            System.Console.Write(string.Empty.PadLeft(replyLevel * 4));
            System.Console.WriteLine(string.Format("[{0}] {1}: {2} ({3})",comment.Type, comment.UserName, comment.Text, comment.ModifyDate));
    
            // if comment has replies
            if (comment.Replies != null)
            {
                // print replies
                foreach (Vintasoft.Imaging.Annotation.Comments.Comment reply in comment.Replies)
                    PrintComment(reply, replyLevel + 1);
            }
        }
    
    }
    
    
    ''' <summary>
    ''' Demonstrates how to prints comments of specified PDF document.
    ''' </summary>
    Public Class PdfAnnotationCommentExample
        ''' <summary>
        ''' Prints comments of specified PDF document.
        ''' </summary>
        ''' <param name="pdfFilename">The PDF filename.</param>
        Public Shared Sub PrintComments(pdfFilename As String)
            ' create an image collection
            Using images As New Vintasoft.Imaging.ImageCollection()
                ' add PDF document to the image collection
                images.Add(pdfFilename)
    
                ' create PDF annotation comment controller
                Using pdfAnnotationCommentController As New Vintasoft.Imaging.Annotation.Comments.Pdf.ImageCollectionPdfAnnotationCommentController()
                    ' specify that PDF annotation comment controller is associated with image collection
                    pdfAnnotationCommentController.Images = images
    
                    ' print comments from PDF annotation comment controller
                    PrintComments(pdfAnnotationCommentController)
                End Using
    
                ' clear and dispose images
                images.ClearAndDisposeItems()
            End Using
        End Sub
    
        ''' <summary>
        ''' Prints comments from specified comment controller.
        ''' </summary>
        ''' <param name="commentController">The comment controller.</param>
        Public Shared Sub PrintComments(commentController As Vintasoft.Imaging.Annotation.Comments.ImageCollectionCommentController)
            ' for each image
            For i As Integer = 0 To commentController.Images.Count - 1
                Dim image As Vintasoft.Imaging.VintasoftImage = commentController.Images(i)
    
                ' get comment collection, which is associated with image/PDF page
                Dim comments As Vintasoft.Imaging.Annotation.Comments.CommentCollection = commentController.GetComments(image)
                ' if comments are found
                If comments IsNot Nothing AndAlso comments.Count > 0 Then
                    System.Console.WriteLine(String.Format("Page {0}:", i + 1))
    
                    ' for each comment
                    For Each comment As Vintasoft.Imaging.Annotation.Comments.Comment In comments
                        ' print comment
                        PrintComment(comment, 0)
                    Next
                End If
            Next
        End Sub
    
        ''' <summary>
        ''' Prints the comment.
        ''' </summary>
        ''' <param name="comment">The comment.</param>
        ''' <param name="replyLevel">The reply level.</param>
        Private Shared Sub PrintComment(comment As Vintasoft.Imaging.Annotation.Comments.Comment, replyLevel As Integer)
            ' print comment
            System.Console.Write(String.Empty.PadLeft(replyLevel * 4))
            System.Console.WriteLine(String.Format("[{0}] {1}: {2} ({3})", comment.Type, comment.UserName, comment.Text, comment.ModifyDate))
    
            ' if comment has replies
            If comment.Replies IsNot Nothing Then
                ' print replies
                For Each reply As Vintasoft.Imaging.Annotation.Comments.Comment In comment.Replies
                    PrintComment(reply, replyLevel + 1)
                Next
            End If
        End Sub
    
    End Class