Back to Devexpress

PdfPageFacade.Annotations Property

officefileapi-devexpress-dot-pdf-dot-pdfpagefacade.md

latest13.7 KB
Original Source

PdfPageFacade.Annotations Property

Returns all page annotation properties.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Pdf.v25.2.Core.dll

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public IReadOnlyList<PdfAnnotationFacade> Annotations { get; }
vb
Public ReadOnly Property Annotations As IReadOnlyList(Of PdfAnnotationFacade)

Property Value

TypeDescription
IReadOnlyList<PdfAnnotationFacade>

A list of objects that contain annotation properties.

|

Remarks

The Annotations property retrieves all page annotation properties. Use the PdfDocumentFacade.Pages property to access the PdfPageFacade class.

You can filter annotation properties, cast them to the corresponding class, and use class properties to modify annotation parameters.

Refer to the following article for more information about annotations: Annotations in PDF Documents

Create Annotations

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

AnnotationClassMethod
LinkPdfLinkAnnotationFacadePdfPageFacade.AddLinkAnnotation
Text MarkupPdfTextMarkupAnnotationFacadePdfPageFacade.AddTextMarkupAnnotation
Sticky NotePdfTextAnnotationFacadePdfPageFacade.AddTextAnnotation
CaretPdfCaretAnnotationFacadePdfPageFacade.AddCaretAnnotation
Rubber StampPdfRubberStampAnnotationFacadePdfPageFacade.AddRubberStampAnnotation
CirclePdfCircleAnnotationFacadePdfPageFacade.AddCircleAnnotation
SquarePdfSquareAnnotationFacadePdfPageFacade.AddSquareAnnotation
File AttachmentPdfFileAttachmentAnnotationFacadePdfPageFacade.AddFileAttachmentAnnotation
Free TextPdfFreeTextAnnotationFacadePdfPageFacade.AddFreeTextAnnotation
InkPdfInkAnnotationFacadePdfPageFacade.AddInkAnnotation
LinePdfLineAnnotationFacadePdfPageFacade.AddLineAnnotation
PolylinePdfPolyLineAnnotationFacadePdfPageFacade.AddPolyLineAnnotation
PolygonPdfPolygonAnnotationFacadePdfPageFacade.AddPolygonAnnotation

The code sample below creates a rubber stamp, file attachment, free text, and a circle annotation:

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

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    // Load a document
    processor.LoadDocument("..\\..\\Document.pdf");

    // Access the first page properties
    PdfPageFacade pageFacade = processor.DocumentFacade.Pages[0];

    // Define a rubber stamp rectangle
    PdfRectangle rubberStampRectangle = new PdfRectangle(663, 526, 763, 576);

    // 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(52, 550);

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

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

    // Specify a free text annotation area
    PdfRectangle freeTextRectangle = new PdfRectangle(14, 321, 145, 340);

    // Create a free text annotation in this area
    PdfFreeTextAnnotationFacade freeTextAnnotation =
       pageFacade.AddFreeTextAnnotation(freeTextRectangle, "Free Text Annotation");
    freeTextAnnotation.Author = "Rayn Anita W";

    // Add a callout line
    freeTextAnnotation.SetCallout(PdfAnnotationLineEndingStyle.OpenArrow, new PdfPoint(152,351));

    // Find the target phrase in the document
    string circleText = "dogfooded";
    PdfTextSearchResults searchResults = processor.FindText(circleText);

    if (searchResults.Status == PdfTextSearchStatus.Found)
    {
        // Define an area around the phrase to add an annotation
        PdfRectangle circleRectangle = searchResults.Rectangles[0].BoundingRectangle;

        // Create a circle annotation in this area
        PdfCircleAnnotationFacade circleAnnotation = pageFacade.AddCircleAnnotation(circleRectangle);
        circleAnnotation.Author = "Cardle Anita W";
        circleAnnotation.Contents = "It's better to say 'used' in this case";
    }

    // Save the result
    processor.SaveDocument("..\\..\\Result.pdf");
}
vb
Imports DevExpress.Pdf
Imports System
Imports System.IO

Using processor As New PdfDocumentProcessor()
  ' Load a document
  processor.LoadDocument("..\..\Document.pdf")

  ' Access the first page properties
  Dim pageFacade As PdfPageFacade = processor.DocumentFacade.Pages(0)

  ' Define a rubber stamp rectangle
  Dim rubberStampRectangle As New PdfRectangle(663, 526, 763, 576)

  ' 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(52, 550)

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

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

  ' Specify a free text annotation area
  Dim freeTextRectangle As New PdfRectangle(14, 321, 145, 340)

  ' Create a free text annotation
  Dim freeTextAnnotation As PdfFreeTextAnnotationFacade =
     pageFacade.AddFreeTextAnnotation(freeTextRectangle, "Free Text Annotation")
  freeTextAnnotation.Author = "Rayn Anita W"

  ' Add a callout line
  freeTextAnnotation.SetCallout(PdfAnnotationLineEndingStyle.OpenArrow, New PdfPoint(152,351))

  ' Find the target phrase in the document
  Dim circleText As String = "dogfooded"
  Dim searchResults As PdfTextSearchResults = processor.FindText(circleText)

  If searchResults.Status = PdfTextSearchStatus.Found Then
    ' Define an area around the phrase to add an annotation
    Dim circleRectangle As PdfRectangle = searchResults.Rectangles(0).BoundingRectangle

    ' Create a circle annotation in this area
    Dim circleAnnotation As PdfCircleAnnotationFacade = pageFacade.AddCircleAnnotation(circleRectangle)
    circleAnnotation.Author = "Cardle Anita W"
    circleAnnotation.Contents = "It's better to say 'used' in this case"
  End If

  ' Save the result
  processor.SaveDocument("..\..\Result.pdf")
End Using

Flatten Annotations

Call one of the following methods to flatten an annotation:

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

The code sample below flattens all annotations that are located on the lower half of the first page:

csharp
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
  // Load a document
  processor.LoadDocument("..\\..\\Document.pdf");
  PdfPageFacade pageFacade = processor.DocumentFacade.Pages[0];

  // Flatten annotations located
  // on the lower half of the first page
  double halfPage = processor.Document.Pages[0].CropBox.Top / 2;
  pageFacade.FlattenAnnotations(x => x.Rectangle.Top < halfPage);

  // Save the result
  processor.SaveDocument("..\\..\\Result.pdf");
}
vb
Using processor As New PdfDocumentProcessor()
  ' Load a document
  processor.LoadDocument("..\..\Document.pdf")
  Dim pageFacade As PdfPageFacade = processor.DocumentFacade.Pages(0)

  ' Flatten annotations located
  ' on the lower half of the first page
  Dim halfPage As Double = processor.Document.Pages(0).CropBox.Top / 2
  pageFacade.FlattenAnnotations(Function(x) x.Rectangle.Top < halfPage)

  ' Save the result
  processor.SaveDocument("..\..\Result.pdf")
End Using

Remove Annotations

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

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

csharp
using DevExpress.Pdf;
using System.Linq;

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    // Load a document
    processor.LoadDocument("..\\..\\Document.pdf");

    // Access the first page properties
    PdfPageFacade pageFacade = processor.DocumentFacade.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();
        }
    }
    // Save the result
    processor.SaveDocument("..\\..\\Result.pdf");
}
vb
Imports DevExpress.Pdf
Imports System.Linq

Using processor As New PdfDocumentProcessor()
  ' Load a document
  processor.LoadDocument("..\..\Document.pdf")

  ' Access the first page properties
  Dim pageFacade As PdfPageFacade = processor.DocumentFacade.Pages(0)

  ' Retrieve all markup annotations
  Dim 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

  ' Save the result
  processor.SaveDocument("..\..\Result.pdf")
End Using

See Also

PdfPageFacade Class

PdfPageFacade Members

DevExpress.Pdf Namespace