Back to Devexpress

PdfGraphicsAcroFormField Class

officefileapi-devexpress-dot-pdf-ef492d99.md

latest8.8 KB
Original Source

PdfGraphicsAcroFormField Class

A base class for all interactive form fields in PDF Graphics API.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public abstract class PdfGraphicsAcroFormField
vb
Public MustInherit Class PdfGraphicsAcroFormField

Remarks

To access the PdfGraphics class, you need to reference the DevExpress.Pdf.Drawing.v25.2 assembly.

Tip

You can use the PdfAcroFormField class to add interactive form fields to a PDF file. Refer to the following article for more information: Interactive Forms in PDF Documents

Create a Form Field

The table below lists available form fields and API used to create each type. All methods and class constructors operate in the world coordinate system.

Form FieldConstructorMethod
Check BoxPdfGraphicsAcroFormCheckBoxField(String, RectangleF)
Combo BoxPdfGraphicsAcroFormComboBoxField(String, RectangleF)PdfGraphicsAcroFormField.CreateComboBox
List BoxPdfGraphicsAcroFormListBoxField(String, RectangleF)PdfGraphicsAcroFormField.CreateListBox
Radio GroupPdfGraphicsAcroFormRadioGroupField(String)PdfGraphicsAcroFormField.CreateRadioGroup
SignaturePdfGraphicsAcroFormSignatureField(String, RectangleF)PdfGraphicsAcroFormField.CreateSignature
Text BoxPdfGraphicsAcroFormTextBoxField(String, RectangleF)PdfGraphicsAcroFormField.CreateTextBox

Add a Form Field as a Graphics Content

Call the PdfGraphics.AddFormField or PdfDocumentProcessor.AddFormFields method to add a form field as a graphics content.

Draw a Form Field on a Page

To add an interactive field to a page, call one of the following methods:

Example

The following code snippet uses PDF Graphics API to add interactive form fields – text box and radio button group.

View Example

csharp
using DevExpress.Pdf;
using System.Drawing;

using (var processor = new PdfDocumentProcessor()) {
    // Create an empty document.
    processor.CreateEmptyDocument("..\\..\\Result.pdf");

    // Create graphics and draw form fields.
    using (PdfGraphics graphics = processor.CreateGraphicsPageSystem()) {
        DrawFormFields(graphics);

        // Render a page with graphics.
        processor.RenderNewPage(PdfPaperSize.Letter, graphics);
    }
}

static void DrawFormFields(PdfGraphics graphics){
    // Create a text box field and specify its location on the page.
    var textBox =
         new PdfGraphicsAcroFormTextBoxField("text box", new RectangleF(30, 10, 200, 30));

    // Specify text and appearance parameters.
    textBox.Text = "Text Box";
    textBox.Appearance.FontSize = 12;
    textBox.Appearance.BackgroundColor = Color.AliceBlue;

    // Add the text box field to graphics.
    graphics.AddFormField(textBox);

    // Create a radio group field.
    var radioGroup =
         new PdfGraphicsAcroFormRadioGroupField("First Group");

    // Add the first radio button to the group and specify its location.
    radioGroup.AddButton("button1", new RectangleF(30, 60, 20, 20));

    // Add the second radio button to the group.
    radioGroup.AddButton("button2", new RectangleF(30, 90, 20, 20));

    // Specify radio group selected index
    radioGroup.SelectedIndex = 0;

    // Specify appearance options:
    radioGroup.Appearance.BorderAppearance =
         new PdfGraphicsAcroFormBorderAppearance()
        {
            Color = Color.Red,
            Width = 3
        };

    // Add the radio group field to graphics.
    graphics.AddFormField(radioGroup);
}
vb
Imports DevExpress.Pdf
Imports System.Drawing

Shared Sub Main(ByVal args() As String)
    Using processor As New PdfDocumentProcessor()

        ' Create an empty document.
        processor.CreateEmptyDocument("..\..\Result.pdf")

        ' Create graphics and draw form fields.
        Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
            DrawFormFields(graphics)

            ' Render a page with graphics.
            processor.RenderNewPage(PdfPaperSize.Letter, graphics)
        End Using
    End Using
End Sub

Private Shared Sub DrawFormFields(ByVal graphics As PdfGraphics)

    ' Create a text box field and specify its location on the page.
    Dim textBox As New PdfGraphicsAcroFormTextBoxField("text box", New RectangleF(30, 10, 200, 30))

    ' Specify text and appearance parameters.
    textBox.Text = "Text Box"
    textBox.Appearance.FontSize = 12
    textBox.Appearance.BackgroundColor = Color.AliceBlue

    ' Add the text box field to graphics.
    graphics.AddFormField(textBox)

    ' Create a radio group field.
    Dim radioGroup As New PdfGraphicsAcroFormRadioGroupField("First Group")

    ' Add the first radio button to the group and specify its location.
    radioGroup.AddButton("button1", New RectangleF(30, 60, 20, 20))

    ' Add the second radio button to the group.
    radioGroup.AddButton("button2", New RectangleF(30, 90, 20, 20))

    ' Specify radio group selected index,
    radioGroup.SelectedIndex = 0

    ' Specify appearance parameters
    radioGroup.Appearance.BorderAppearance = New PdfGraphicsAcroFormBorderAppearance() With {.Color = Color.Red, .Width = 3}

    ' Add the radio group field to graphics.
    graphics.AddFormField(radioGroup)
End Sub

Inheritance

Object PdfGraphicsAcroFormField PdfGraphicsAcroFormCommonField

PdfGraphicsAcroFormRadioGroupField

PdfGraphicsAcroFormSignatureField

PdfGraphicsAcroFormTextBoxField

PdfGraphicsAcroFormCheckBoxField

PdfGraphicsAcroFormChoiceField

PdfGraphicsAcroFormComboBoxField

PdfGraphicsAcroFormListBoxField

See Also

PdfGraphicsAcroFormField Members

DevExpress.Pdf Namespace