Modify PDF & form fields⚓︎
PyPDFForm lets you modify PDF-level metadata and form field settings in code. You can update document titles, field appearances, and field behavior without altering the PDF form template.
The examples in this section primarily use this PDF.
Change PDF title⚓︎
Set the PDF title during PdfWrapper instantiation or via the .title property. Accessing it retrieves the current title.
Change text field font⚓︎
Before changing a text field's font, you must first register the desired font.
After registration, you can apply the registered font to any text field:
from PyPDFForm import PdfWrapper, Widgets
form = PdfWrapper("sample_template.pdf")
# change globally by iterating each text field
for field in form.widgets.values():
if isinstance(field, Widgets.Text):
field.font = "your_registered_font"
# or change at each field's widget level
form.widgets["test"].font = "your_registered_font"
form.fill(
{
"test": "test_1",
"check": True,
"test_2": "test_2",
"check_2": False,
"test_3": "test_3",
"check_3": True,
},
)
form.write("output.pdf")
Change text field font size⚓︎
You can change the font size using a numeric value in PyPDFForm:
from PyPDFForm import PdfWrapper, Widgets
form = PdfWrapper("sample_template.pdf")
# change globally by iterating each text field
for field in form.widgets.values():
if isinstance(field, Widgets.Text):
field.font_size = 20
# or change at each field's widget level
form.widgets["test"].font_size = 30.5
form.fill(
{
"test": "test_1",
"check": True,
"test_2": "test_2",
"check_2": False,
"test_3": "test_3",
"check_3": True,
},
)
form.write("output.pdf")
Change text field font color⚓︎
You can change the font color using an RGB tuple:
from PyPDFForm import PdfWrapper, Widgets
form = PdfWrapper("sample_template.pdf")
# change globally by iterating each text field
for field in form.widgets.values():
if isinstance(field, Widgets.Text):
field.font_color = (1, 0, 0)
# or change at each field's widget level
form.widgets["test"].font_color = (0.2, 0, 0.5)
form.fill(
{
"test": "test_1",
"check": True,
"test_2": "test_2",
"check_2": False,
"test_3": "test_3",
"check_3": True,
},
)
form.write("output.pdf")
Change text field alignment⚓︎
You can change the alignment of the text filled into a text field by setting its alignment property to an integer value: 0 for left, 1 for center, and 2 for right.
from PyPDFForm import PdfWrapper, Widgets
form = PdfWrapper("sample_template.pdf")
# change globally by iterating each text field
for field in form.widgets.values():
if isinstance(field, Widgets.Text):
field.alignment = 1 # center
# or change at each field's widget level
form.widgets["test"].alignment = 2 # right
form.fill(
{
"test": "test_1",
"check": True,
"test_2": "test_2",
"check_2": False,
"test_3": "test_3",
"check_3": True,
},
)
form.write("output.pdf")
Change text field max length⚓︎
You can change the maximum number of characters allowed in a text field:
from PyPDFForm import PdfWrapper, Widgets
form = PdfWrapper("sample_template.pdf")
# change globally by iterating each text field
for field in form.widgets.values():
if isinstance(field, Widgets.Text):
field.max_length = 4
# or change at each field's widget level
form.widgets["test"].max_length = 2
form.fill(
{
"test": "te",
"check": True,
"test_2": "test",
"check_2": False,
"test_3": "test",
"check_3": True,
},
)
form.write("output.pdf")
Enable text field character spacing (combs)⚓︎
To enable character spacing in a text field, set its .comb property to True. This spaces out the filled text evenly across the field.
from PyPDFForm import PdfWrapper, Widgets
form = PdfWrapper("sample_template.pdf")
# change globally by iterating each text field
for field in form.widgets.values():
if isinstance(field, Widgets.Text):
field.max_length = 4
field.comb = True
# or change at each field's widget level
form.widgets["test"].max_length = 2
form.widgets["test"].comb = True
form.fill(
{
"test": "te",
"check": True,
"test_2": "test",
"check_2": False,
"test_3": "test",
"check_3": True,
},
)
form.write("output.pdf")
Warning
This property only takes effect when the text field also has a max_length set.
Enable multiline text field⚓︎
To enable multiline input for a text field, set its .multiline property to True. This lets it accept paragraph-style input:
Change checkbox/radio button size⚓︎
You can change the size of a checkbox or a group of radio buttons using a numeric value:
Change dropdown field choices⚓︎
To modify the options available in a dropdown field, assign a new list of strings to the .choices attribute of the corresponding field. For example, the following snippet updates the dropdown_1 field in this PDF form with a new set of choices:
If the export values should differ from the displayed options, specify a list of tuples for the .choices attribute. The first value of each tuple is the displayed option, and the second value is the export value:
from PyPDFForm import PdfWrapper
form = PdfWrapper("sample_template_with_dropdown.pdf")
form.widgets["dropdown_1"].choices = [
("", "blank_export_value"),
("apple", "apple_export_value"),
("banana", "banana_export_value"),
("cherry", "cherry_export_value"),
("dates", "dates_export_value"),
]
form.write("output.pdf")
Put the replacement dropdown choices in JSON. Use strings for default export values or two-item arrays for custom export values:
Change dropdown field font⚓︎
Before changing a dropdown field's font, you must first register the desired font.
After registration, you can apply the registered font to any dropdown field:
Change dropdown field font size⚓︎
You can change a dropdown field's font size using a numeric value in PyPDFForm:
Change dropdown field font color⚓︎
You can change a dropdown field's font color using an RGB tuple:
Change field name⚓︎
PyPDFForm lets you rename existing fields by updating their keys.
To change the key of the first text field from test to test_text and the second text field from test_2 to test_text_2 in this PDF, use:
If multiple fields share the same key, use the index parameter to choose which one to update. For example, to change the second row's text field from Description[0] to Description[1] in this PDF, use:
Provide the old field keys and their new_key values to update rename:
Change field editability⚓︎
The readonly property of each form field controls its editability. Setting readonly to True flattens the field, making it uneditable, while setting it to False unflattens it, making it editable. The following example makes different form fields editable in this PDF form after they have been flattened:
from PyPDFForm import PdfWrapper
form = PdfWrapper("sample_template_with_dropdown.pdf")
form.fill(
{
"test_1": "test_1",
"test_2": "test_2",
"test_3": "test_3",
"check_1": True,
"check_2": True,
"check_3": True,
"radio_1": 1,
"dropdown_1": 0,
},
flatten=True,
)
form.widgets["test_2"].readonly = False # text
form.widgets["check_3"].readonly = False # checkbox
form.widgets["radio_1"].readonly = False # radio button group
form.widgets["dropdown_1"].readonly = False # dropdown
form.write("output.pdf")
The following example starts with a filled, flattened form and then makes selected fields editable again by setting readonly to false:
Change field visibility⚓︎
The hidden property of each form field controls its visibility. Setting hidden to True hides the field, while setting it to False makes it visible: