Skip to content

How to Add Form Fields

Jorj X. McKie edited this page Jun 20, 2018 · 5 revisions

Starting with version v1.13.11, form field annotations (also called "widgets") can be added to a new or existing PDF.

If necessary, the first added widget will also turn the PDF into a Form PDF.

Widgets represent an annotation type with many special features and heterogeneous subtypes. To present a manageable programmer API covering a broad range of functions, we have introduced a new class, called Widget, which serves as a "communication vehicle" in the following way:

  • To access complete widget information of a widget annotation annot on a page, consult its property annot.widget which is a Widget object (or None if a different annotation type). Among this information are field name and field value. Demo script list-fields.py prints all information of all widgets of a PDF.

  • To add a widget on a page, use the following sequence of instructions:

widget = fitz.Widget()                 # create a new empty widget object
widget.rect = fitz.Rect(...)           # where to locate the field
widget.field_type = fitz.ANNOT_WG_TEXT # set other necessary properties
...                                    # ...
annot = page.addWidget(widget)         # add the widget
# now the PDF has eventually become a Form PDF
# and further widgets can be added to this or other pages

Demo script widgettest.py creates a new PDF page with 5 different form fields. After using a suitable viewer software to fill out this form, the result looks like this: widgettest.pdf

In a future version, we may enable to also change some properties of an existing widget (like setting the field value).

Clone this wiki locally