-
Notifications
You must be signed in to change notification settings - Fork 600
How to Add Form Fields
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 propertyannot.widget
which is aWidget
object (orNone
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).
HOWTO Button annots with JavaScript
HOWTO work with PDF embedded files
HOWTO extract text from inside rectangles
HOWTO extract text in natural reading order
HOWTO create or extract graphics
HOWTO create your own PDF Drawing
Rectangle inclusion & intersection
Metadata & bookmark maintenance