Back to Devexpress

PdfFreeTextAnnotationFacade Class

officefileapi-devexpress-dot-pdf-165da9b8.md

latest13.2 KB
Original Source

PdfFreeTextAnnotationFacade Class

Contains members used to manage free text annotations without access to their inner structure.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public class PdfFreeTextAnnotationFacade :
    PdfMarkupAnnotationFacade
vb
Public Class PdfFreeTextAnnotationFacade
    Inherits PdfMarkupAnnotationFacade

The following members return PdfFreeTextAnnotationFacade objects:

Remarks

Create Free Text Annotations

Create Text Box Annotations

The code sample below creates a free text annotation:

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

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

    // Define an annotation area
    PdfRectangle rectangle = new PdfRectangle(663, 526, 763, 576);

    // Create a free text annotation in this area
    PdfFreeTextAnnotationFacade freeText =
       pageFacade.AddFreeTextAnnotation(rectangle, "Free Text Annotation");

    // Specify annotation parameters
    freeText.Author = "Nancy Davolio";
    freeText.BorderWidth = 2;

    // Save the result
    processor.SaveDocument("..\\..\\Result.pdf");
}
vb
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 an annotation area
  Dim rectangle As New PdfRectangle(663, 526, 763, 576)

  ' Create a free text annotation in this area
  Dim freeText As PdfFreeTextAnnotationFacade =
     pageFacade.AddFreeTextAnnotation(rectangle, "Free Text Annotation")

  ' Specify annotation parameters
  freeText.Author = "Nancy Davolio"
  freeText.BorderWidth = 2

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

Create Callout Annotations

Call a free text annotation’s SetCallout method to add a callout line to the annotation. The method call sets the annotation’s Intent property to FreeTextCallout.

The code sample below creates a callout annotation pointed at a specific phrase:

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

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

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

    if (searchResults.Status == PdfTextSearchStatus.Found)
    {
        // Obtain the target phrase rectangle
        calloutRectangle = searchResults.Rectangles[0].BoundingRectangle;

        // Define an annotation area
        PdfRectangle rectangle = new PdfRectangle(663, 526, 763, 576);

        // Create a free text annotation in this area
        PdfFreeTextAnnotationFacade freeText =
            pageFacade.AddFreeTextAnnotation(rectangle, "Free Text Annotation");

        // Specify annotation parameters
        freeText.Author = "Nancy Davolio";

        // Add a callout line pointed at the center of the target phrase
        freeText.SetCallout(PdfAnnotationLineEndingStyle.OpenArrow, calloutRectangle.Center);
    }

    // Save the result
    processor.SaveDocument("..\\..\\Result.pdf");
}
vb
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)
  Dim calloutRectangle As PdfRectangle

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

  If searchResults.Status = PdfTextSearchStatus.Found Then

    ' Obtain the target phrase rectangle
    calloutRectangle = searchResults.Rectangles(0).BoundingRectangle

    ' Define an annotation area
    Dim rectangle As New PdfRectangle(663, 526, 763, 576)

    ' Create a free text annotation in this area
    Dim freeText As PdfFreeTextAnnotationFacade =
       pageFacade.AddFreeTextAnnotation(rectangle, "Free Text Annotation")

    ' Specify annotation parameters
    freeText.Author = "Nancy Davolio"

    ' Add a callout line pointed at the center of the target phrase
    freeText.SetCallout(PdfAnnotationLineEndingStyle.OpenArrow, calloutRectangle.Center)
  End If

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

Create Typewriter Annotations

Set a free text annotation’s Intent property to FreeTextTypewriter to convert the annotation to a typewriter (click-to-type) object. This object has no border, callout line, or padding between the text and annotation bounds.

The code sample below creates a typewriter annotation:

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

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

    // Define an annotation area
    PdfRectangle rectangle = new PdfRectangle(663, 526, 763, 576);

    // Create a free text annotation
    PdfFreeTextAnnotationFacade freeText =
        pageFacade.AddFreeTextAnnotation(rectangle, "Free Text Annotation");

    // Specify annotation parameters
    freeText.Author = "Nancy Davolio";
    freeText.Intent = PdfFreeTextAnnotationIntent.FreeTextTypewriter;

    // Save the result
    processor.SaveDocument("..\\..\\Result.pdf");
}
vb
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 an annotation area
  Dim rectangle As New PdfRectangle(663, 526, 763, 576)

  ' Create a free text annotation
  Dim freeText As PdfFreeTextAnnotationFacade =
     pageFacade.AddFreeTextAnnotation(rectangle, "Free Text Annotation")

  ' Specify annotation parameters
  freeText.Author = "Nancy Davolio"
  freeText.Intent = PdfFreeTextAnnotationIntent.FreeTextTypewriter

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

Edit Free Text Annotations

The PdfPageFacade.Annotations property returns all page annotation properties. You can filter free text annotation properties, cast them to the PdfFreeTextAnnotationFacade class, and use class properties to modify annotation parameters.

The following code snippet changes border parameters for all free text annotations:

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 free text annotations
    var freeTextAnnotations = pageFacade.Annotations.Where
            (annotation => annotation.Type == PdfAnnotationType.FreeText);
    foreach (PdfFreeTextAnnotationFacade freeText in freeTextAnnotations) 
    {
        // Change the border parameters
        freeText.BorderWidth = 2;
        freeText.BorderStyle = PdfBorderStyle.Dash;
        // Change the font color
        freeText.FontColor = new PdfRGBColor(0.90, 0.00, 0.30);
    }
    // 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 free text annotations
  Dim freeTextAnnotations = pageFacade.Annotations.Where
    (Function(annotation) annotation.Type = PdfAnnotationType.FreeText)
  For Each freeText As PdfFreeTextAnnotationFacade In freeTextAnnotations

    ' Change the border parameters
    freeText.BorderWidth = 2
    freeText.BorderStyle = PdfBorderStyle.Dash
  Next freeText
    ' Change the font color
    freeText.FontColor = New PdfRGBColor(0.90, 0.00, 0.30)

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

Flatten Free Text 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 following code snippet flattens all free text annotations on the first page:

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

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

    // Flatten all free text annotations
    pageFacade.FlattenAnnotations(PdfAnnotationType.FreeText);
}
vb
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)

  ' Flatten all free text annotations
  pageFacade.FlattenAnnotations(PdfAnnotationType.FreeText)
End Using

Remove Free Text Annotations

Call the PdfAnnotationFacade.Remove() method to remove an annotation. The following code snippet removes all free text annotations from the first page:

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 free text annotations
  var freeTextAnnotations = pageFacade.Annotations.Where
    (annotation => annotation.Type == PdfAnnotationType.FreeText).ToList();

  // Remove all free text annotations
  foreach (PdfFreeTextAnnotationFacade freeText in freeTextAnnotations)
  {
      freeText.Remove();
  }
}
vb
Imports DevExpress.Pdf
Imports System.Linq

Using processor As New PdfDocumentProcessor()
processor

  'Load a document:
  processor.LoadDocument("..\..\Document.pdf")

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

  ' Retrieve all free text annotations
  Dim freeTextAnnotations = pageFacade.Annotations.Where
    (Function(annotation) annotation.Type = PdfAnnotationType.FreeText).ToList()

  ' Remove all free text annotations
  For Each freeText As PdfFreeTextAnnotationFacade In freeTextAnnotations
    freeText.Remove()
  Next freeText
End Using

Inheritance

Object PdfAnnotationFacade PdfMarkupAnnotationFacade PdfFreeTextAnnotationFacade

See Also

PdfFreeTextAnnotationFacade Members

DevExpress.Pdf Namespace