Docs / Guides
Forms
SoftZenPdf creates interactive forms (AcroForms) whose fields look the same in every viewer, reads the fields of existing forms, and fills them without rewriting the original file.
Creating fields
Each PdfPage.Add… method places a field on the page and returns it for further settings. Field names must
be unique in the document and cannot contain dots.
using SoftZenPdf;
var document = new PdfDocument();
var page = document.AddPage();
page.AddTextField("Name", 200, 690, 260, 22).MaxLength = 50;
page.AddCheckBox("Subscribe", 200, 652, 16, isChecked: true);
page.AddRadioGroup("Plan", 16, ("Basic", 200, 612), ("Pro", 300, 612)).Value = "Pro";
page.AddDropdown("Country", 200, 570, 260, 22, ["Pakistan", "Germany", "Japan"]);
page.AddSignatureField("Signature", 200, 500, 260, 50);
document.Save("form-fields.pdf");
foreach (var field in PdfReader.Open("form-fields.pdf").ReadFormFields())
Console.WriteLine($"{field.Name}: {field.Kind} = {field.Value ?? "(empty)"}");
| Method | Creates |
|---|---|
AddTextField | A text field; single-line by default |
AddCheckBox | A square check box |
AddRadioGroup | Radio buttons of which at most one is selected; use document.Form.AddRadioGroup and AddOption to spread one group over several pages |
AddDropdown | A dropdown list (combo box), optionally editable |
AddListBox | A scrollable list; one selection, or several with MultiSelect |
AddPushButton | A button with a caption that performs an action when clicked |
AddSignatureField | An empty signature placeholder for a signing application |
Field options
Every field has Tooltip, ReadOnly, Required, BorderColor,
BackgroundColor and BorderWidth. Text fields add Value, DefaultValue,
Font, FontSize, TextColor, Alignment, Multiline,
MaxLength and Comb (one character per box, for codes).
using SoftZenPdf;
var document = new PdfDocument();
var page = document.AddPage();
var comments = page.AddTextField("Comments", 72, 600, 300, 80);
comments.Multiline = true;
comments.FontSize = 10;
comments.Tooltip = "Anything else we should know?";
var code = page.AddTextField("Code", 72, 560, 120, 24);
code.MaxLength = 6;
code.Comb = true;
var customer = page.AddTextField("CustomerId", 72, 520, 200, 24, "C-1024");
customer.ReadOnly = true;
customer.Alignment = TextAlignment.Right;
customer.BackgroundColor = PdfColor.FromHex("#F4F5F8");
var country = page.AddDropdown("Country", 72, 480, 200, 24, ["Pakistan", "Germany", "Japan"], selected: "Japan");
country.Editable = true;
document.Save("form-options.pdf");
Every field gets appearance streams, so it looks right even in viewers that do not generate appearances themselves.
List boxes and push buttons
A list box shows several options at once and, with MultiSelect, lets the user pick more than one; set the
selection with Value or SelectedValues. A push button has no value: it shows a
Caption and performs its Action when clicked.
using SoftZenPdf;
var document = new PdfDocument();
var page = document.AddPage();
var toppings = page.AddListBox("Toppings", 72, 640, 200, 80, ["Cheese", "Mushrooms", "Olives", "Peppers", "Onions"]);
toppings.MultiSelect = true;
toppings.SelectedValues = ["Cheese", "Olives"];
page.AddPushButton("Help", 72, 590, 120, 26, "Online help").Action =
PdfButtonAction.OpenUri(new Uri("https://example.com/help"));
page.AddPushButton("Clear", 204, 590, 120, 26, "Clear form").Action = PdfButtonAction.ResetForm();
page.AddPushButton("Send", 336, 590, 120, 26, "Submit").Action =
PdfButtonAction.SubmitForm(new Uri("https://example.com/orders"), PdfSubmitFormat.Html);
document.Save("list-box-and-buttons.pdf");
| PdfButtonAction | When clicked |
|---|---|
OpenUri(uri) | Opens a web address or other absolute URI |
GoToPage(page, top) | Jumps to a page of the same document |
ResetForm() | Resets every field to its default value |
SubmitForm(url, format) | Sends the field values to a URL as an HTML form post (the default), FDF, XFDF or the whole PDF |
These are standard PDF actions and need no JavaScript, but the viewer performs them: desktop viewers such as Adobe Acrobat Reader support all four, while browser and mobile viewers may ignore form submission. Without an action, clicking a push button does nothing.
Filling an existing form
PdfFormFiller opens a PDF form, sets values and saves the result as an incremental update: the original bytes
are kept and the changes appended, and new appearances are generated for the changed fields.
using SoftZenPdf;
var document = new PdfDocument();
var page = document.AddPage();
page.AddTextField("Name", 200, 690, 260, 22);
page.AddCheckBox("Subscribe", 200, 652, 16);
document.Save("form.pdf");
var filler = PdfFormFiller.Open("form.pdf");
filler.SetValue("Name", "Jane Doe");
filler.SetChecked("Subscribe", true);
filler.Save("form-filled.pdf");
Fields lists every field with its type, value and options, which is how you discover the names in a form you
did not create. SetValue sets text, the choice of a dropdown, list box or radio group, or a check box
("Off", "false" or empty clears it).
using SoftZenPdf;
var filler = PdfFormFiller.Open("form-fields.pdf");
foreach (var field in filler.Fields)
Console.WriteLine($"{field.Name} ({field.Kind}) options: {string.Join(", ", field.Options)}");
filler.SetValue("Name", "Jane Doe");
filler.SetValue("Plan", "Basic");
filler.SetValue("Country", "Germany");
filler.SetChecked("Subscribe", false);
filler.Save("form-fields-filled.pdf");
An unknown field name throws KeyNotFoundException whose message lists the form's field names. Values outside a choice field's options, or longer than a text field's maximum length, throw ArgumentException.
Limits
Field appearances use the standard fonts, so field text is limited to Latin (WinAnsi) characters. Signature fields are not signed, forms cannot be flattened, and there is no JavaScript or XFA support, so no date pickers, formatting or calculated fields. Files that needed repair to open cannot be filled in place, and form fields are not supported in PDF/A mode yet.
API: PdfPage.AddTextField, PdfTextField, PdfListBox, PdfPushButton, PdfButtonAction, PdfFormFiller, PdfReader.ReadFormFields.