All docs
V21.2
21.2
21.1
20.2
The page you are viewing does not exist in version 20.2. This link will take you to the root page.
20.1
The page you are viewing does not exist in version 20.1. This link will take you to the root page.
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

How to: Use PDF Facade API to Manage Annotations

  • 5 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 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 with facade api

pdfViewer.LoadDocument("Document.pdf");

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

// Define a rubber stamp rectangle
PdfRectangle rubberStampRectangle = new PdfRectangle(491, 727, 591, 773); 

// 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;

PdfPageFacade pageFacade = pdfViewer.GetDocumentFacade().Pages[0];

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