Prepare a PDF form

The most common tool to create a PDF form is Adobe Acrobat. A tutorial can be found here. There are other free alternatives like DocFly that support similar functionalities.

Given a PDF that's not a form yet, PyPDFForm also supports creating a subset of PDF form widgets on it through coding.

This section of the documentation will use this PDF as an example.

This section of the documentation requires a basic understanding of the PDF coordinate system.

All optional parameters will have a comment # optional after each of them.

NOTE: For some PDF prep tools, creating widgets on their PDF forms may result in their original widgets getting flattened (e.g., this PDF which was prepared using Sejda). So it is advised that you fill them first before creating any widget using PyPDFForm.

Create a text field widget

A text field widget can be created by downloading the PDF and running the following snippet:

from PyPDFForm import PdfWrapper

new_form = PdfWrapper("dummy.pdf").create_widget(
    widget_type="text",
    name="new_text_field_widget",
    page_number=1,
    x=57,
    y=700,
    width=120,  # optional
    height=40,  # optional
    max_length=5,   # optional
    font="Courier", # optional
    font_size=15,   # optional
    font_color=(1, 0, 0),   # optional
    bg_color=(0, 0, 1), # optional
    border_color=(1, 0, 0), # optional
    border_width=5  # optional
)

with open("output.pdf", "wb+") as output:
    output.write(new_form.read())

Create a checkbox widget

A checkbox widget can be created using the same method with some changes to the parameters:

from PyPDFForm import PdfWrapper

new_form = PdfWrapper("dummy.pdf").create_widget(
    widget_type="checkbox",
    name="new_checkbox_widget",
    page_number=1,
    x=57,
    y=700,
    size=30,    # optional
    button_style="check",   # optional
    tick_color=(0, 1, 0),   # optional
    bg_color=(0, 0, 1), # optional
    border_color=(1, 0, 0), # optional
    border_width=5  # optional
)

with open("output.pdf", "wb+") as output:
    output.write(new_form.read())

The button_style parameter currently supports three options: check, circle, and cross.

Create a dropdown widget

A dropdown widget shares a similar set of parameters as a text field, with the only significant difference being a list of options needs to be specified:

from PyPDFForm import PdfWrapper

new_form = PdfWrapper("dummy.pdf").create_widget(
    widget_type="dropdown",
    name="new_dropdown_widget",
    page_number=1,
    x=57,
    y=700,
    options=[
        "foo",
        "bar",
        "foobar",
    ],
    width=120,  # optional
    height=40,  # optional
    font="Courier", # optional
    font_size=15,   # optional
    font_color=(1, 0, 0),   # optional
    bg_color=(0, 0, 1), # optional
    border_color=(1, 0, 0), # optional
    border_width=5  # optional
)

with open("output.pdf", "wb+") as output:
    output.write(new_form.read())