Create form fields⚓︎
The most common tool for creating PDF form fields is Adobe Acrobat, and a tutorial is available here. Alternative free tools like DocFly offer similar functionality.
PyPDFForm also allows creating PDF form fields on existing PDFs programmatically.
Field creation is done with PdfWrapper.bulk_create_fields, which accepts a list of field definitions. Use the same method whether you are adding one field or several:
from PyPDFForm import Fields, PdfWrapper
fields = [
Fields.TextField(
name="new_text_field_1",
page_number=1,
x=100,
y=100,
),
Fields.TextField(
name="new_text_field_2",
page_number=1,
x=100,
y=300,
),
Fields.CheckBoxField(
name="new_checkbox_1",
page_number=1,
x=300,
y=100,
),
Fields.CheckBoxField(
name="new_checkbox_2",
page_number=1,
x=300,
y=300,
),
]
new_form = PdfWrapper("dummy.pdf").bulk_create_fields(fields)
new_form.write("output.pdf")
Use the create field command with a JSON file that defines the fields to create:
{
"text": [
{
"name": "new_text_field_1",
"page_number": 1,
"x": 100,
"y": 100
},
{
"name": "new_text_field_2",
"page_number": 1,
"x": 100,
"y": 300
}
],
"check": [
{
"name": "new_checkbox_1",
"page_number": 1,
"x": 300,
"y": 100
},
{
"name": "new_checkbox_2",
"page_number": 1,
"x": 300,
"y": 300
}
]
}
This section of the documentation will primarily use this PDF as an example.
Understanding the PDF coordinate system is necessary for this section.
In the library examples, optional parameters are marked with # optional.
Create a text field⚓︎
Use Fields.TextField to create a text field:
from PyPDFForm import Fields, PdfWrapper
new_form = PdfWrapper("dummy.pdf").bulk_create_fields([
Fields.TextField(
name="new_text_field",
page_number=1,
x=57.5,
y=700.9,
required=False, # optional
tooltip="this is a text field", # optional
width=120.3, # optional
height=40.7, # optional
max_length=5, # optional, number of characters
comb=True, # optional, when set to True, max_length must also be set (1)
font="your_registered_font", # optional (2)
font_size=15, # optional
font_color=(1, 0, 0), # optional
bg_color=(0, 0, 1, 1), # optional, (r, g, b, alpha)
border_color=(1, 0, 0, 1), # optional, (r, g, b, alpha)
border_width=5, # optional
alignment=0, # optional, 0=left, 1=center, 2=right
multiline=True, # optional
),
])
new_form.write("output.pdf")
Put text field definitions under the text key in the JSON file:
{
"text": [
{
"name": "new_text_field",
"page_number": 1,
"x": 57.5,
"y": 700.9,
"required": false,
"tooltip": "this is a text field",
"width": 120.3,
"height": 40.7,
"max_length": 5,
"comb": true,
"font": "path_to_a_ttf_file",
"font_size": 15,
"font_color": [
1,
0,
0
],
"bg_color": [
0,
0,
1,
1
],
"border_color": [
1,
0,
0,
1
],
"border_width": 5,
"alignment": 0,
"multiline": true
}
]
}
Create a checkbox⚓︎
Use Fields.CheckBoxField to create a checkbox:
from PyPDFForm import Fields, PdfWrapper
new_form = PdfWrapper("dummy.pdf").bulk_create_fields([
Fields.CheckBoxField(
name="new_checkbox",
page_number=1,
x=57,
y=700,
required=False, # optional
tooltip="this is a checkbox", # optional
size=30, # optional
button_style="check", # optional (1)
tick_color=(0, 1, 0), # optional
bg_color=(0, 0, 1, 1), # optional, (r, g, b, alpha)
border_color=(1, 1, 0, 1), # optional, (r, g, b, alpha)
border_width=5, # optional
),
])
new_form.write("output.pdf")
- The
button_styleparameter currently supports three options:check,circle, andcross.
Put checkbox field definitions under the check key in the JSON file:
Bug
To remove the border of a checkbox, set the alpha channel of the border_color to 0, for example: border_color=(1, 0, 0, 0). Setting border_width to 0 may still render a border with a width of 1 due to a PDF specification quirk.
Create a radio button group⚓︎
Unlike other field types, radio buttons must be created as a group. Therefore, for the coordinate parameters x and y, you must specify a list of coordinates for each radio button within the group, and the list must contain more than one coordinate.
Otherwise, radio button creation shares almost the same parameters as a checkbox:
from PyPDFForm import Fields, PdfWrapper
new_form = PdfWrapper("dummy.pdf").bulk_create_fields([
Fields.RadioGroup(
name="new_radio_group",
page_number=1,
x=[50, 100, 150],
y=[50, 100, 150],
required=False, # optional
tooltip="this is a radio group", # optional
size=30, # optional
button_style="check", # optional (1)
shape="square", # optional, circle or square
tick_color=(0, 1, 0), # optional
bg_color=(0, 0, 1, 1), # optional, (r, g, b, alpha)
border_color=(1, 0, 1, 1), # optional, (r, g, b, alpha)
border_width=5, # optional
),
])
new_form.write("output.pdf")
- The
button_styleparameter currently supports three options:check,circle, andcross.
Put radio group definitions under the radio key in the JSON file:
{
"radio": [
{
"name": "new_radio_group",
"page_number": 1,
"x": [
50,
100,
150
],
"y": [
50,
100,
150
],
"required": false,
"tooltip": "this is a radio group",
"size": 30,
"button_style": "check",
"shape": "square",
"tick_color": [
0,
1,
0
],
"bg_color": [
0,
0,
1,
1
],
"border_color": [
1,
0,
1,
1
],
"border_width": 5
}
]
}
Bug
To remove the border of a group of radio buttons, set the alpha channel of the border_color to 0, for example: border_color=(1, 0, 0, 0). Setting border_width to 0 may still render a border with a width of 1 due to a PDF specification quirk.
Create a dropdown field⚓︎
A dropdown field shares many parameters with a text field. The main difference is that you must specify a list of options:
from PyPDFForm import Fields, PdfWrapper
new_form = PdfWrapper("dummy.pdf").bulk_create_fields([
Fields.DropdownField(
name="new_dropdown",
page_number=1,
x=57,
y=700,
options=[
"foo",
"bar",
"foobar",
],
required=False, # optional
tooltip="this is a dropdown", # optional
width=120, # optional
height=40, # optional
font="your_registered_font", # optional (1)
font_size=15, # optional
font_color=(1, 0, 0), # optional
bg_color=(0, 0, 1, 1), # optional, (r, g, b, alpha)
border_color=(0, 1, 0, 1), # optional, (r, g, b, alpha)
border_width=5, # optional
),
])
new_form.write("output.pdf")
- To use a custom font, see how to register it here.
To use export values that differ from the displayed options, specify a list of tuples for the options parameter. The first value in each tuple is the displayed option, and the second value is the export value:
from PyPDFForm import Fields, PdfWrapper
new_form = PdfWrapper("dummy.pdf").bulk_create_fields([
Fields.DropdownField(
name="new_dropdown",
page_number=1,
x=57,
y=700,
options=[
("option_1", "option_1_export_value"),
("option_2", "option_2_export_value"),
("option_3", "option_3_export_value"),
],
),
])
new_form.write("output.pdf")
Put dropdown field definitions under the dropdown key in the JSON file:
{
"dropdown": [
{
"name": "new_dropdown",
"page_number": 1,
"x": 57,
"y": 700,
"options": [
"foo",
"bar",
"foobar"
],
"required": false,
"tooltip": "this is a dropdown",
"width": 120,
"height": 40,
"font": "path_to_a_ttf_file",
"font_size": 15,
"font_color": [
1,
0,
0
],
"bg_color": [
0,
0,
1,
1
],
"border_color": [
0,
1,
0,
1
],
"border_width": 5
}
]
}
Create a signature field⚓︎
A signature field is only interactive in tools that support it. Otherwise, it is displayed as a rectangle, and clicking it will not trigger any action:
from PyPDFForm import Fields, PdfWrapper
new_form = PdfWrapper("dummy.pdf").bulk_create_fields([
Fields.SignatureField(
name="new_signature",
page_number=1,
x=100,
y=100,
required=False, # optional
tooltip="this is a signature", # optional
width=410, # optional
height=100, # optional
),
])
new_form.write("output.pdf")
Put signature field definitions under the signature key in the JSON file:
Create an image field⚓︎
Similar to a signature field, an image field is also only interactive in tools that support it:
from PyPDFForm import Fields, PdfWrapper
new_form = PdfWrapper("dummy.pdf").bulk_create_fields([
Fields.ImageField(
name="new_image",
page_number=1,
x=100,
y=100,
required=False, # optional
tooltip="this is an image", # optional
width=192, # optional
height=108, # optional
),
])
new_form.write("output.pdf")