Skip to content

Working with Forms

@scaryterry/pdfium provides robust support for interactive PDF forms, including AcroForms and XFA forms (limited support). This guide covers how to detect forms, interact with form fields, and manage form state.

Before attempting to interact with form fields, you should check if the document actually contains a form.

import { PDFium, FormType } from '@scaryterry/pdfium';
// ... initialise and load document ...
if (document.hasForm()) {
console.log('Document contains a form');
const type = document.formType;
if (type === FormType.AcroForm) {
console.log('Standard AcroForm detected');
} else if (type === FormType.XFAFull || type === FormType.XFAForeground) {
console.log('XFA Form detected');
}
}

You can also use helper methods:

if (document.hasAcroForm()) {
// Handle standard PDF form
}
if (document.hasXFAForm()) {
// Handle XFA form (limited support)
}

Form fields in PDFium are represented as “Widget” annotations on a page. Retrieve them by filtering the page’s annotations:

using page = document.getPage(0);
// Get all widget annotations on the page
const widgets = page.getAnnotations().filter((a) => a.isWidget());
for (const widget of widgets) {
console.log(`Field Name: ${widget.getFormFieldName()}`);
console.log(`Field Type: ${widget.getFormFieldType()}`);
console.log(`Value: ${widget.getFormFieldValue()}`);
console.log(`Bounds: ${JSON.stringify(widget.bounds)}`);
// For combo boxes or list boxes, check options
const options = widget.getFormFieldOptions();
if (options) {
for (const option of options) {
console.log(`Option: ${option.label}, Selected: ${option.selected}`);
}
}
}
MethodReturnsDescription
isWidget()booleanWhether this annotation is a form field widget
getFormFieldType()FormFieldTypeThe field type (Text, Button, CheckBox, RadioButton, ComboBox, ListBox, Signature)
getFormFieldName()string | undefinedThe field’s fully qualified name
getFormFieldValue()string | undefinedThe current field value
getFormFieldAlternateName()string | undefinedThe field’s alternate (tooltip) name
getFormFieldExportValue()string | undefinedThe export value (for checkboxes/radio buttons)
getFormFieldOptions()WidgetOption[] | undefinedOptions for combo/list boxes
getFormFieldFlags()FormFieldFlagsBitfield of field flags (read-only, required, etc.)

You can control how form fields are highlighted when rendered. This is useful for making fields more visible to users.

import { FormFieldType } from '@scaryterry/pdfium';
// Set highlight colour for all field types to yellow
document.setFormFieldHighlightColour(FormFieldType.Unknown, { r: 255, g: 255, b: 0, a: 255 });
// Set transparency (0-255)
document.setFormFieldHighlightAlpha(100);

When a user interacts with a form field (e.g., via a UI you build on top of the rendered canvas), you might need to manage text selection within that field.

// Get selected text in the currently focused form field
const selectedText = page.getFormSelectedText();
if (selectedText) {
console.log(`Selected: ${selectedText}`);
}
// Replace the selected text
page.replaceFormSelection('New Text');

To ensure proper form behaviour, you may need to programmatically release focus from form fields, for example, when the user clicks away from a form element.

// Force release of focus from any form field
const released = document.killFormFocus();

PDFium supports undo and redo operations for form field editing.

if (page.canFormUndo()) {
page.formUndo();
}
if (page.canFormRedo()) {
page.formRedo();
}

When rendering a page that contains form fields, ensure you enable the renderFormFields option.

const { data } = page.render({
scale: 2,
renderFormFields: true // Disabled by default, so you must enable it
});