Back to Devexpress

PdfGraphics.AddFormField(PdfGraphicsAcroFormField) Method

officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-addformfield-x28-devexpress-dot-pdf-dot-pdfgraphicsacroformfield-x29.md

latest11.6 KB
Original Source

PdfGraphics.AddFormField(PdfGraphicsAcroFormField) Method

Adds an interactive form field as graphics content.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public void AddFormField(
    PdfGraphicsAcroFormField field
)
vb
Public Sub AddFormField(
    field As PdfGraphicsAcroFormField
)

Parameters

NameTypeDescription
fieldPdfGraphicsAcroFormField

An interactive form field that should be added.

|

Remarks

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

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

Form FieldClassMethod
Check BoxPdfGraphicsAcroFormCheckBoxFieldNo method
Combo BoxPdfGraphicsAcroFormComboBoxFieldPdfGraphicsAcroFormField.CreateComboBox
List BoxPdfGraphicsAcroFormListBoxFieldPdfGraphicsAcroFormField.CreateListBox
Radio GroupPdfGraphicsAcroFormRadioGroupFieldPdfGraphicsAcroFormField.CreateRadioGroup
SignaturePdfGraphicsAcroFormSignatureFieldPdfGraphicsAcroFormField.CreateSignature
Text BoxPdfGraphicsAcroFormTextBoxFieldPdfGraphicsAcroFormField.CreateTextBox

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

PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackgroundThese methods allow you to draw content on an existing page.PdfDocumentProcessor.RenderNewPageDraws content on a new page.

Note

Coordinate system transformations (for example, system rotation) are not taken into account when the AddFormField method is called.

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

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

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddFormField(PdfGraphicsAcroFormField) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

pdf-document-api-create-interactive-form/CS/AddFormFieldsToNewDocument/Program.cs#L36

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

pdf-document-api-create-combo-box-field/CS/AddComboBoxField/Program.cs#L43

csharp
// Add the field to the document.
    graphics.AddFormField(comboBox);
}

pdf-document-api-create-check-box-field/CS/AddCheckBoxField/Program.cs#L36

csharp
// Add the field to the document.
    graphics.AddFormField(checkBox);
}

pdf-document-api-create-radio-button-group-field/CS/AddRadioButtonField/Program.cs#L43

csharp
// Add the field to graphics.
    graphics.AddFormField(radioGroup);
}

pdf-document-api-add-signature-field-to-document/CS/AddSignatureField/Program.cs#L39

csharp
// Add the field to the document.
    graphics.AddFormField(signature);
}

pdf-document-api-create-interactive-form/VB/AddFormFieldsToNewDocument/Program.vb#L32

vb
' Add the text box field to graphics.
graphics.AddFormField(textBox)
' Create a radio group field.

pdf-document-api-create-combo-box-field/VB/AddComboBoxField/Program.vb#L38

vb
' Add the field to the document.
    graphics.AddFormField(comboBox)
End Sub

pdf-document-api-create-check-box-field/VB/AddCheckBoxField/Program.vb#L33

vb
' Add the field to the document.
    graphics.AddFormField(checkBox)
End Sub

pdf-document-api-create-radio-button-group-field/VB/AddRadioButtonField/Program.vb#L37

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

pdf-document-api-add-signature-field-to-document/VB/AddSignatureField/Program.vb#L31

vb
' Add the field to the document.
    graphics.AddFormField(signature)
End Sub

See Also

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace