Back to Devexpress

How to: Use PDF Facade API to Manage Annotations

windowsforms-403206-controls-and-libraries-pdf-viewer-examples-pdf-facade-api-how-to-organize-annotations.md

latest12.9 KB
Original Source

How to: Use PDF Facade API to Manage Annotations

  • Jul 01, 2025
  • 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:

AnnotationClassMethod
LinkPdfLinkAnnotationFacadeAddLinkAnnotation
Text MarkupPdfTextMarkupAnnotationFacadeAddTextMarkupAnnotation
Sticky NotePdfTextAnnotationFacadeAddTextAnnotation
CaretPdfCaretAnnotationFacadeAddCaretAnnotation
Rubber StampPdfRubberStampAnnotationFacadeAddRubberStampAnnotation
CirclePdfCircleAnnotationFacadeAddCircleAnnotation
SquarePdfSquareAnnotationFacadeAddSquareAnnotation
File AttachmentPdfFileAttachmentAnnotationFacadeAddFileAttachmentAnnotation
Free TextPdfFreeTextAnnotationFacadeAddFreeTextAnnotation
InkPdfInkAnnotationFacadeAddInkAnnotation
LinePdfLineAnnotationFacadeAddLineAnnotation
PolylinePdfPolyLineAnnotationFacadeAddPolyLineAnnotation
PolygonPdfPolygonAnnotationFacadeAddPolygonAnnotation
RedactionPdfRedactAnnotationFacadeAddRedactAnnotation

The following code snippet creates a file attachment and a rubber stamp annotation:

csharp
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";
vb
pdfViewer.LoadDocument("Document.pdf")

' Access the first page properties
Dim documentFacade As PdfDocumentFacade = pdfViewer.GetDocumentFacade()
Dim pageFacade As PdfPageFacade = documentFacade.Pages(0)

' Define a rubber stamp rectangle
Dim rubberStampRectangle As New PdfRectangle(491, 727, 591, 773)

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

' Define a file attachment area 
Dim attachmentPoint As New PdfPoint(29, 568)

' Specify attachment data
Dim attachment As New PdfFileAttachment() With {.CreationDate = Date.Now, .Description = "This is my attached file", .FileName = "MyAttach.txt", .Data = File.ReadAllBytes("..\..\Attachment.txt")}

' Create a file attachment annotation
Dim pdfFileAttachment As PdfFileAttachmentAnnotationFacade =
     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 following code snippet changes annotations created in the previous example, so they appear as follows:

csharp
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;
}
vb
Imports DevExpress.Pdf
Imports System.Linq

Private pageFacade As PdfPageFacade = pdfViewer.GetDocumentFacade().Pages(0)

' Get all rubber stamps 
Private rubberStampAnnotations = pageFacade.Annotations.Where(Function(annotation) annotation.Type = PdfAnnotationType.RubberStamp)
For Each pdfRubberStamp As PdfRubberStampAnnotationFacade In rubberStampAnnotations
    ' Generate a stamp from another document’s page
    pdfRubberStamp.SetCustomIcon("..\..\Demo.pdf", 4)
Next pdfRubberStamp

' Obtain all file attachments
Dim fileAttachments = pageFacade.Annotations.Where(Function(annotation) annotation.Type = PdfAnnotationType.FileAttachment)
For Each fileAttachment As PdfFileAttachmentAnnotationFacade In fileAttachments
    ' Change the icon and its color
    fileAttachment.Color = New PdfRGBColor(0.83, 0.13, 0.18)
    fileAttachment.IconName = PdfFileAttachmentAnnotationIconName.Tag
Next fileAttachment

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:

MethodDescription
PdfDocumentFacade.FlattenAnnotationsFlattens document annotations.
PdfPageFacade.FlattenAnnotationsFlattens the page annotations.
PdfAnnotationFacade.Flatten()Flattens a specific annotation.

The following code snippet flattens document annotations:

csharp
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();
vb
Dim documentFacade As PdfDocumentFacade = pdfViewer.GetDocumentFacade()

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

Dim pageFacade As PdfPageFacade = documentFacade.Pages(0)

Dim pageSize As SizeF = pdfViewer.GetPageSize(0)
Dim halfPage As Double = pageSize.Height \ 2

' Flatten annotations that are located
' on the lower half of the page:
pageFacade.FlattenAnnotations(Function(x) x.Rectangle.Top < halfPage)

Dim annotations = documentFacade.Pages(0).Annotations

' Flatten the first annotation:
annotations(0).Flatten()

Remove Annotations

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

The follwoing code snippet removes all annotations from a specific author:

csharp
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();
    }
}
vb
Imports DevExpress.Pdf
Imports System.Linq

' Access the first page properties
Private pageFacade As PdfPageFacade = pdfViewer.GetDocumentFacade().Pages(0)

' Retrieve all markup annotations
Private markups = pageFacade.Annotations.Where(Function(annotation) annotation.Type <> PdfAnnotationType.Link).ToList()
For Each markupAnnotation As PdfMarkupAnnotationFacade In markups
    ' Check the annotation author
    If markupAnnotation.Author = "Brian Zetc" Then
        ' Remove the annotation
        markupAnnotation.Remove()
    End If
Next markupAnnotation