ZingPDF logo

Guide

How to create PDF form fields in C#

Call GetOrCreateFormAsync(), add fields to 1-based pages, and pass PDF rectangles for widget bounds.

ZingPDF creates the AcroForm field dictionary, widget annotation, page annotation reference, and default appearance resources for the supported field types.

Create several fields

This example adds a text field, checkbox, radio group, combo box, list box, and signature field to page 1.

using ZingPDF;
using ZingPDF.Elements.Drawing;
using ZingPDF.Elements.Forms.FieldTypes.Button;
using ZingPDF.Elements.Forms.FieldTypes.Choice;
using ZingPDF.Syntax.CommonDataStructures;

using var pdf = Pdf.Create();
var form = await pdf.GetOrCreateFormAsync();

await form.AddTextFieldAsync(
    1,
    "Applicant.Name",
    Rectangle.FromCoordinates(new Coordinate(48, 720), new Coordinate(260, 748)),
    options =>
    {
        options.Description = "Applicant name";
        options.DefaultValue = "Ada Lovelace";
    });

await form.AddCheckboxFieldAsync(
    1,
    "Applicant.AcceptedTerms",
    Rectangle.FromCoordinates(new Coordinate(48, 680), new Coordinate(64, 696)),
    options =>
    {
        options.Description = "Accepted terms";
        options.ExportValue = "Yes";
        options.Checked = true;
    });

await form.AddRadioButtonFieldAsync(
    1,
    "Applicant.ContactMethod",
    [
        new RadioButtonFieldOption("Email", Rectangle.FromCoordinates(new Coordinate(48, 640), new Coordinate(64, 656))),
        new RadioButtonFieldOption("Phone", Rectangle.FromCoordinates(new Coordinate(48, 612), new Coordinate(64, 628)))
    ],
    options => options.SelectedValue = "Email");

await form.AddComboBoxFieldAsync(
    1,
    "Applicant.State",
    Rectangle.FromCoordinates(new Coordinate(48, 560), new Coordinate(220, 586)),
    [
        new ChoiceFieldOption("NSW", "New South Wales"),
        new ChoiceFieldOption("VIC", "Victoria"),
        new ChoiceFieldOption("QLD", "Queensland")
    ],
    options => options.DefaultValue = "NSW");

await form.AddListBoxFieldAsync(
    1,
    "Applicant.Products",
    Rectangle.FromCoordinates(new Coordinate(48, 480), new Coordinate(220, 540)),
    [
        new ChoiceFieldOption("Core"),
        new ChoiceFieldOption("OCR"),
        new ChoiceFieldOption("HTML")
    ]);

await form.AddSignatureFieldAsync(
    1,
    "Approval.Signature",
    Rectangle.FromCoordinates(new Coordinate(48, 400), new Coordinate(260, 450)));

await pdf.SaveAsync(File.Create("created-form.pdf"));

Supported field types

  • AddTextFieldAsync(...) creates a text field and can set a default value.
  • AddCheckboxFieldAsync(...) creates a single checkbox with an export value.
  • AddRadioButtonFieldAsync(...) creates one field with multiple widget options.
  • AddComboBoxFieldAsync(...) and AddListBoxFieldAsync(...) create choice fields.
  • AddSignatureFieldAsync(...) creates a visible signature widget that can later be signed.

Bounds and page numbers

Public page numbers are 1-based. Field bounds are PDF rectangles on the target page. Keep field names stable and unique because later filling code often addresses fields by fully qualified name.

Signature fields

Creating a signature field and signing it are separate operations. Create the visible field first, save or keep working in memory, then call SignatureFormField.SignAsync(...) when certificate material is available.

Reset behavior

Field creation stores default values where the field type supports them. A future form reset API will restore fields to those defaults.

Next step

After creating fields, see How to fill PDF form fields in C# and How to sign a PDF form field in C#.