Back to Devexpress

PdfPageFacade Class

officefileapi-devexpress-dot-pdf-b8673ff9.md

latest14.3 KB
Original Source

PdfPageFacade Class

Exposes members used to perform various operations on a PDF page without access to its inner structure.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public class PdfPageFacade
vb
Public Class PdfPageFacade

Remarks

The Pages property returns a list of PdfPageFacade objects used to organize PDF pages without access to their inner structure.

The PdfPageFacade class allows you to perform the following actions:

  • Create, access, and flatten annotations
  • Create destinations
  • Clear page content

Organize Annotations

Refer to the following article for more information about annotations:

Read Tutorial: Annotations in PDF Documents

Add Annotations

The Annotations property retrieves all page annotation properties.

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

Access Annotations

The 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.

Flatten Annotations

Call the FlattenAnnotations method to flatten page annotations.

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

You can also flatten a specific annotation. Use the Annotations property to retrieve a list of annotation objects and call the Flatten() method.

Clear Page Content

Call the ClearContent method to remove content in one or multiple page areas. You can specify what content type to keep in the areas that are to be cleared. To do that, pass the PdfClearContentOptions class object as the method parameter.

Run Demo: PDF Clear Page Content

The code sample below removes only text in the upper half of the first page:

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

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

    // Define an area to clear
    PdfRectangle cropBox = pdfDocumentProcessor.Document.Pages[0].CropBox;
    double halfPage = cropBox.Top / 2;
    PdfRectangle pageRectangle = new PdfRectangle(cropBox.Left, halfPage, cropBox.Right + halfPage, cropBox.Top);

    // Set what content type to keep
    PdfClearContentOptions options = new PdfClearContentOptions()
    {
      ClearAnnotations = false,
      ClearGraphics = false, 
      ClearImages = false
    };

    // Clear the page area
    pageFacade.ClearContent(pageRectangle, options);

    pdfDocumentProcessor.SaveDocument("Document_cleared.pdf");
}
vb
Using pdfDocumentProcessor As New PdfDocumentProcessor()

  ' Load a document
  pdfDocumentProcessor.LoadDocument("Document.pdf")

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

  ' Define an area to clear
  Dim cropBox As PdfRectangle = pdfDocumentProcessor.Document.Pages(0).CropBox
  Dim halfPage As Double = cropBox.Top / 2
  Dim pageRectangle As New PdfRectangle(cropBox.Left, halfPage, cropBox.Right + halfPage, cropBox.Top)

  ' Set what content type to keep
  Dim options As New PdfClearContentOptions() With
   {
      .ClearAnnotations = False,
      .ClearGraphics = False,
      .ClearImages = False
  }

  ' Clear the page area
  pageFacade.ClearContent(pageRectangle, options)

  pdfDocumentProcessor.SaveDocument("Document_cleared.pdf")
End Using

Inheritance

Object PdfPageFacade

See Also

PdfPageFacade Members

DevExpress.Pdf Namespace