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.
Changes to a field's properties are written to the PDF the next time it is read, for example by read() or write(). A value the PDF can't store, such as None for readonly or a coordinate that isn't a number, raises an error right away and leaves the field unchanged. If writing the changes fails, for example because a font size is set on a field whose default appearance has no font, all pending changes are discarded before the error is raised, so the fields still match the PDF.
The examples in this section primarily use this PDF.
CLI input methods⚓︎
The update field command can update one or more fields from a YAML or JSON file supplied with --file / -f:
To update a single field without creating a data file, identify it with --field and pass its properties as command-line options:
pypdfform update field sample_template.pdf \
--field test \
--font_size 20 \
--alignment 1 \
-o output.pdf
When --file is supplied, it takes precedence over --field and the field property options.
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")
Reading font gives the name the PDF uses for the text field's font, such as /Helv. Assigning a name the PDF already uses for one of its fonts selects that font, even when a registered font has the same name. Otherwise, the name of a registered font or a font in the PDF, as fonts lists them, selects that font. Any other name starting with / is written as it is, without checking that the PDF has a font with that name. Assigning the font the field already uses keeps it, even when the PDF writes it without a /, such as Courier, or a registered font has the same name. Any other name raises an error when the changes are written.
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")
Reading font_size gives the text field's current font size. A size of 0 means the text is sized to fit the field.
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")
Reading font_color gives the text field's current font color as an RGB tuple. A gray or CMYK color in the PDF is converted to RGB.
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. Set it to None to use the default alignment.
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. The maximum must be at least 1. Set it to None to remove the limit:
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. Set it to False to disable it.
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. Set it to False to disable it:
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")
Reading .choices returns the same format: a string for each option whose displayed text and export value are equal, and a tuple for each option where they differ.
Put the replacement dropdown choices in YAML. Use strings for default export values or two-item sequences 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:
from PyPDFForm import PdfWrapper
form = PdfWrapper("sample_template_with_dropdown.pdf")
form.widgets["dropdown_1"].font = "your_registered_font"
form.write("output.pdf")
Reading font gives the name the PDF uses for the dropdown field's font, such as /Helv. Assigning a name the PDF already uses for one of its fonts selects that font, even when a registered font has the same name. Otherwise, the name of a registered font or a font in the PDF, as fonts lists them, selects that font. Any other name starting with / is written as it is, without checking that the PDF has a font with that name. Assigning the font the field already uses keeps it, even when the PDF writes it without a /, such as Courier, or a registered font has the same name. Any other name raises an error when the changes are written.
Change dropdown field font size⚓︎
You can change a dropdown field's font size using a numeric value in PyPDFForm:
from PyPDFForm import PdfWrapper
form = PdfWrapper("sample_template_with_dropdown.pdf")
form.widgets["dropdown_1"].font_size = 30
form.write("output.pdf")
Reading font_size gives the dropdown field's current font size. A size of 0 means the text is sized to fit the field.
Change dropdown field font color⚓︎
You can change a dropdown field's font color using an RGB tuple:
from PyPDFForm import PdfWrapper
form = PdfWrapper("sample_template_with_dropdown.pdf")
form.widgets["dropdown_1"].font_color = (1, 0, 0)
form.write("output.pdf")
Reading font_color gives the dropdown field's current font color as an RGB tuple. A gray or CMYK color in the PDF is converted to RGB.
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:
Rename one field without creating a data file by identifying it with --field:
Change field editability⚓︎
The readonly property of each form field controls its editability. Setting readonly to True makes the field read-only, while setting it to False makes it editable. The following example makes different form fields editable in this PDF form after they have been made read-only:
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,
},
readonly=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, read-only 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:
Remove form fields⚓︎
You can also remove fields that aren't on any page, such as hidden fields. Removing a field doesn't guarantee that its data is erased from the PDF file, so don't use it to redact information. Reset, submit, and hide actions no longer name removed fields. An action left with no field to act on does nothing, and is removed unless other actions run with it. JavaScript that names them isn't changed.
In a damaged PDF, the parent fields of a field can loop back on themselves, for example when two fields are each other's parent. If that's the case for any field to remove, no fields are removed: remove_fields() raises a ValueError, and the CLI exits with an error without writing a PDF.
Use PdfWrapper.remove_fields() to remove form fields by their keys: