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 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).

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

widget = fitz.Widget()                 # create a new empty widget object
widget.field_type = fitz.ANNOT_WG_TEXT # set the necessary properties
widget.rect = fitz.Rect(...)           # ...
...                                    # ...
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

Clone this wiki locally