Skip to main content
A newer version of this page is available. .
All docs
V21.2

How to: Use PDF Facade API to Manage Annotations

  • 6 minutes to read

The following article describes how to use PDF Document Facade to manage PDF annotations.

Important

The Universal Subscription or an additional Office File API Subscription is required to use this example in production code. Refer to the DevExpress Subscription page for pricing information.

The PdfViewerExtensions.GetDocumentFacade(IPdfViewer) method retrieves the PdfDocumentFacade class object that allows you to change the PDF document without access to its inner structure. The PdfDocumentFacade.Pages property returns a list of PdfPageFacade objects that contain PDF page properties.

The PdfPageFacade.Annotations property retrieves all page annotation properties. You can create and delete annotations, edit their content, flatten, add related comments and reviews. Refer to the following article for more information on PDF annotations:

Read Tutorial: Annotations in PDF Documents

Create Annotations

The table below lists available annotation types and API used to create these annotations:

Annotation Class Method
Link PdfLinkAnnotationFacade AddLinkAnnotation
Text Markup PdfTextMarkupAnnotationFacade AddTextMarkupAnnotation
Sticky Note PdfTextAnnotationFacade AddTextAnnotation
Caret PdfCaretAnnotationFacade AddCaretAnnotation
Rubber Stamp PdfRubberStampAnnotationFacade AddRubberStampAnnotation
Circle PdfCircleAnnotationFacade AddCircleAnnotation
Square PdfSquareAnnotationFacade AddSquareAnnotation
File Attachment PdfFileAttachmentAnnotationFacade AddFileAttachmentAnnotation
Free Text PdfFreeTextAnnotationFacade AddFreeTextAnnotation
Ink PdfInkAnnotationFacade AddInkAnnotation
Line PdfLineAnnotationFacade AddLineAnnotation
Polyline PdfPolyLineAnnotationFacade AddPolyLineAnnotation
Polygon PdfPolygonAnnotationFacade AddPolygonAnnotation

The code sample below creates a file attachment and a rubber stamp annotation:

annotations created

using DevExpress.Pdf;
using System;
using System.IO;

public MainWindow()
{
    InitializeComponent();
    pdfViewer.OpenDocument("Demo.pdf");
    pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
}

private void PdfViewer_DocumentLoaded(object sender, RoutedEventArgs e)
{
    PdfPageFacade pageFacade = pdfViewer.GetDocumentFacade().Pages[0];
    CreateAnnotations(pageFacade);
}

private void CreateAnnotations(PdfPageFacade pageFacade)
{
    // Define a rubber stamp rectangle
    PdfRectangle rubberStampRectangle = new PdfRectangle(641, 10, 741, 110);

    // Create a "Draft" rubber stamp annotation
    PdfRubberStampAnnotationFacade rubberStamp =
        pageFacade.AddRubberStampAnnotation(rubberStampRectangle,
        PdfRubberStampAnnotationIconName.Draft);
    rubberStamp.Author = "Jesse Faden";
    rubberStamp.Contents = "Made in PDF Document API";

    // Define a file attachment area 
    PdfPoint attachmentPoint = new PdfPoint(29, 568);

    // Specify attachment data
    PdfFileAttachment attachment = new PdfFileAttachment()
    {
        CreationDate = DateTime.Now,
        Description = "This is my attached file",
        FileName = "MyAttach.txt",
        Data = File.ReadAllBytes("..\\..\\Attachment.txt")
    };

    // Create a file attachment annotation
    PdfFileAttachmentAnnotationFacade pdfFileAttachment =
        pageFacade.AddFileAttachmentAnnotation(attachmentPoint, attachment,
        PdfFileAttachmentAnnotationIconName.PaperClip);
    pdfFileAttachment.Author = "Nancy Skywalker";
    pdfFileAttachment.Contents = "Additional Content";
}

Edit Annotations

The PdfPageFacade.Annotations property returns all page annotation properties. You can filter annotation properties, cast them to the corresponding class, and use class properties to modify annotation parameters.

The code sample below changes annotations created in the previous example, so they appear as follows:

annotations edited

using DevExpress.Pdf;
using System.Linq;

public MainWindow()
{
    InitializeComponent();
    pdfViewer.OpenDocument("Demo.pdf");
    pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
}

private void PdfViewer_DocumentLoaded(object sender, RoutedEventArgs e)
{
    PdfPageFacade pageFacade = pdfViewer.GetDocumentFacade().Pages[0];
    EditAnnotations(pageFacade);
}

private void EditAnnotations(PdfPageFacade pageFacade)
{
    // Get all rubber stamps 
    var rubberStampAnnotations = pageFacade.Annotations.Where
        (annotation => annotation.Type == PdfAnnotationType.RubberStamp);
    foreach (PdfRubberStampAnnotationFacade pdfRubberStamp in rubberStampAnnotations)
    {
        // Generate a stamp from another document’s page
        pdfRubberStamp.SetCustomIcon("..\\..\\Demo.pdf", 4);
    }

    // Obtain all file attachments
    var fileAttachments = pageFacade.Annotations.Where
        (annotation => annotation.Type == PdfAnnotationType.FileAttachment);
    foreach (PdfFileAttachmentAnnotationFacade fileAttachment in fileAttachments)
    {
        // Change the icon and its color
        fileAttachment.Color = new PdfRGBColor(0.83, 0.13, 0.18);
        fileAttachment.IconName = PdfFileAttachmentAnnotationIconName.Tag;
    }
}

Flatten Annotations

The PdfDocumentFacade allows you to flatten all document annotations, annotations on a specific page, and a single annotation. Call one of the following methods to complete the task:

Method Description
PdfDocumentFacade.FlattenAnnotations Flattens document annotations.
PdfPageFacade.FlattenAnnotations Flattens the page annotations.
PdfAnnotationFacade.Flatten() Flattens a specific annotation.

The code sample below flattens document annotations:

PdfDocumentFacade documentFacade = pdfViewer.GetDocumentFacade();

// Flatten all text annotations in the document:
documentFacade.FlattenAnnotations(PdfAnnotationType.Text);

PdfPageFacade pageFacade = documentFacade.Pages[0];

SizeF pageSize = pdfViewer.GetPageSize(0);
double halfPage = pageSize.Height / 2; 

// Flatten annotations that are located
// on the lower half of the page:
pageFacade.FlattenAnnotations(x => x.Rectangle.Top < halfPage);

var annotations = documentFacade.Pages[0].Annotations;

// Flatten the first annotation:
annotations[0].Flatten();

Remove Annotations

Call the PdfAnnotationFacade.Remove() method to remove an annotation.

The code sample below removes all annotations from a specific author:

using DevExpress.Pdf;
using System.Linq;

// Access the first page properties
PdfPageFacade pageFacade = pdfViewer.GetDocumentFacade().Pages[0];

// Retrieve all markup annotations
var markups = pageFacade.Annotations.Where
        (annotation => annotation.Type != PdfAnnotationType.Link).ToList();
foreach(PdfMarkupAnnotationFacade markupAnnotation in markups)
{
    // Check the annotation author
    if (markupAnnotation.Author == "Brian Zetc")
    {
        // Remove the annotation
        markupAnnotation.Remove();
    }
}