Back to Devexpress

PdfDocumentFacade Class

officefileapi-devexpress-dot-pdf-7e5b7ad9.md

latest15.2 KB
Original Source

PdfDocumentFacade Class

Exposes a set of methods used to perform various operations on a PDF document 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 PdfDocumentFacade
vb
Public Class PdfDocumentFacade

The following members return PdfDocumentFacade objects:

Remarks

Change AcroForm Field Options

The AcroForm property obtains a set of methods used to organize interactive forms.

Utilize one of the following methods to get form field properties:

MethodDescription
PdfAcroFormFacade.GetFields()Retrieves all AcroForm fields.
GetFormFieldObtains properties of a field with a specific name.
GetButtonFormField
GetCheckBoxFormField
GetComboBoxFormField
and so onReturns properties of a specific form field type.

Utilize the PdfAcroFormFacade.GetNames() method to get a list of form field names.

The code sample below retrieves all fields and changes their appearance:

csharp
using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
    pdfDocumentProcessor.LoadDocument("Documents//FormDemo.pdf");

    PdfDocumentFacade documentFacade = pdfDocumentProcessor.DocumentFacade;
    PdfAcroFormFacade acroForm = documentFacade.AcroForm;

    //Change color settings for all form fields:
    var fields = acroForm.GetFields();
    foreach (PdfFormFieldFacade field in fields)
    {
        ChangeFormFieldColor(field);
    }
    pdfDocumentProcessor.SaveDocument("FormDemo_new.pdf");
}
  private static void ChangeFormFieldColor(PdfFormFieldFacade field)
  {
      foreach (PdfWidgetFacade pdfWidget in field)
      {
          //Change color and border settings
          //for all form fields:
          pdfWidget.BorderWidth = 1;
          pdfWidget.BackgroundColor = new PdfRGBColor(0.81, 0.81, 0.81);
          pdfWidget.BorderColor = new PdfRGBColor(0.47, 0.44, 0.67);
          pdfWidget.FontColor = new PdfRGBColor(0.34, 0.25, 0.36);

          //Change border style for text form fields:
          if (field.Type == PdfFormFieldType.Text)
          {
              pdfWidget.BorderStyle = PdfBorderStyle.Underline;
          }
      }
  }
vb
Using pdfDocumentProcessor As New PdfDocumentProcessor()
  pdfDocumentProcessor.LoadDocument("Documents//FormDemo.pdf")

  Dim documentFacade As PdfDocumentFacade = pdfDocumentProcessor.DocumentFacade
  Dim acroForm As PdfAcroFormFacade = documentFacade.AcroForm

  'Change color settings for all form fields:
  Dim fields = acroForm.GetFields()
  For Each field As PdfFormFieldFacade In fields
    ChangeFormFieldColor(field)
  Next field
    pdfDocumentProcessor.SaveDocument("FormDemo_new.pdf")
End Using

  private static void ChangeFormFieldColor(PdfFormFieldFacade field)
    For Each pdfWidget As PdfWidgetFacade In field
      'Change color and border settings
      'for all form fields:
      pdfWidget.BorderWidth = 1
      pdfWidget.BackgroundColor = New PdfRGBColor(0.81, 0.81, 0.81)
      pdfWidget.BorderColor = New PdfRGBColor(0.47, 0.44, 0.67)
      pdfWidget.FontColor = New PdfRGBColor(0.34, 0.25, 0.36)

      'Change border style for text form fields:
      If field.Type = PdfFormFieldType.Text Then
        pdfWidget.BorderStyle = PdfBorderStyle.Underline
      End If
    Next pdfWidget

Manage Annotations

You can create and delete annotations, edit their content, flatten, add related comments and reviews. Refer to the following topic for more information about annotations: Annotations in PDF Documents.

Create Annotations

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

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 text annotations in the document:

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

  // Flatten all text annotations:
  PdfDocumentFacade documentFacade = processor.DocumentFacade;
  documentFacade.FlattenAnnotations(PdfAnnotationType.Text);

  // Save the result:
  processor.SaveDocument("..\\..\\Result.pdf");
}
vb
Using processor As New PdfDocumentProcessor()
  ' Load a document:
  processor.LoadDocument("..\..\Document.pdf")

  ' Flatten all text annotations:
  Dim documentFacade As PdfDocumentFacade = processor.DocumentFacade
  documentFacade.FlattenAnnotations(PdfAnnotationType.Text)

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

Inheritance

Object PdfDocumentFacade

See Also

PdfDocumentFacade Members

DevExpress.Pdf Namespace