Skip to content

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:

test:
  font_size: 20
  alignment: 1
pypdfform update field sample_template.pdf -f data.yaml -o output.pdf

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.

from PyPDFForm import PdfWrapper

pdf = PdfWrapper("sample_template.pdf", title="My PDF")
from PyPDFForm import PdfWrapper

pdf = PdfWrapper("sample_template.pdf")
pdf.title = "My PDF"
from PyPDFForm import PdfWrapper

pdf = PdfWrapper("sample_template.pdf", title="My PDF")
print(pdf.title)

Set or inspect the title from the command line:

pypdfform update title sample_template.pdf -t "My PDF" -o output.pdf
pypdfform inspect title output.pdf

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.

In the CLI, provide a .ttf file path for each text field's font value:

test:
  font: path_to_a_ttf_file
test_2:
  font: path_to_a_ttf_file
test_3:
  font: path_to_a_ttf_file
pypdfform update field sample_template.pdf -f data.yaml -o 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")

Reading font_size gives the text field's current font size. A size of 0 means the text is sized to fit the field.

Set each text field's font_size in YAML:

test:
  font_size: 30.5
test_2:
  font_size: 20
test_3:
  font_size: 20
pypdfform update field sample_template.pdf -f data.yaml -o 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")

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.

Express each text field's RGB font color as a three-item YAML sequence:

test:
  font_color: [0.2, 0, 0.5]
test_2:
  font_color: [1, 0, 0]
test_3:
  font_color: [1, 0, 0]
pypdfform update field sample_template.pdf -f data.yaml -o 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. 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")

Set each text field's alignment value in YAML, using 0 for left, 1 for center, and 2 for right:

test:
  alignment: 2
test_2:
  alignment: 1
test_3:
  alignment: 1
pypdfform update field sample_template.pdf -f data.yaml -o 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")

Set each text field's max_length in YAML:

test:
  max_length: 2
test_2:
  max_length: 4
test_3:
  max_length: 4
pypdfform update field sample_template.pdf -f data.yaml -o 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")

Set comb alongside the required max_length values in YAML:

test:
  max_length: 2
  comb: true
test_2:
  max_length: 4
  comb: true
test_3:
  max_length: 4
  comb: true
pypdfform update field sample_template.pdf -f data.yaml -o 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:

from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")

form.widgets["test"].multiline = True

form.fill(
    {
        "test": "test_1\ntest_1",
        "check": True,
        "test_2": "test_2\ntest_2",
        "check_2": False,
        "test_3": "test_3\ntest_3",
        "check_3": True,
    },
)

form.write("output.pdf")

Set multiline to true for fields that should accept paragraph-style input:

test:
  multiline: true
pypdfform update field sample_template.pdf -f data.yaml -o output.pdf

Change checkbox/radio button size⚓︎

You can change the size of a checkbox or a group of radio buttons using a numeric value:

from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")

form.widgets["check"].size = 50
form.widgets["check_2"].size = 40
form.widgets["check_3"].size = 60

form.fill(
    {
        "check": True,
        "check_2": True,
        "check_3": True,
    },
)

form.write("output.pdf")

Set each checkbox's size, or each radio button group's size, in YAML:

check:
  size: 50
check_2:
  size: 40
check_3:
  size: 60
pypdfform update field sample_template.pdf -f data.yaml -o output.pdf

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:

from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template_with_dropdown.pdf")

form.widgets["dropdown_1"].choices = ["", "apple", "banana", "cherry", "dates"]

form.write("output.pdf")

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:

dropdown_1:
  choices: ['', apple, banana, cherry, dates]
dropdown_1:
  choices:
    - ['', blank_export_value]
    - [apple, apple_export_value]
    - [banana, banana_export_value]
    - [cherry, cherry_export_value]
    - [dates, dates_export_value]
pypdfform update field sample_template_with_dropdown.pdf -f data.yaml -o output.pdf

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.

In the CLI, provide a .ttf file path for the dropdown field's font value:

dropdown_1:
  font: path_to_a_ttf_file
pypdfform update field sample_template_with_dropdown.pdf -f data.yaml -o output.pdf

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.

Set the dropdown field's font_size in YAML:

dropdown_1:
  font_size: 30
pypdfform update field sample_template_with_dropdown.pdf -f data.yaml -o output.pdf

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.

Express the dropdown field's RGB font color as a three-item YAML sequence:

dropdown_1:
  font_color: [1, 0, 0]
pypdfform update field sample_template_with_dropdown.pdf -f data.yaml -o output.pdf

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:

from PyPDFForm import PdfWrapper

form = (
    PdfWrapper("sample_template.pdf")
    .update_widget_key("test", "test_text")
    .update_widget_key("test_2", "test_text_2")
)
form.commit_widget_key_updates()

form.write("output.pdf")

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:

from PyPDFForm import PdfWrapper

form = PdfWrapper("733.pdf").update_widget_key(
    "Description[0]", "Description[1]", index=1
)
form.commit_widget_key_updates()

form.write("output.pdf")

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:

pypdfform update rename sample_template.pdf \
    --field test \
    --new_key test_text \
    -o output.pdf
- test:
    new_key: test_text
- test_2:
    new_key: test_text_2
pypdfform update rename sample_template.pdf -f data.yaml -o output.pdf
- Description[0]:
    new_key: Description[1]
    index: 1
pypdfform update rename 733.pdf -f data.yaml -o output.pdf

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:

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
test_2:
  readonly: false
check_3:
  readonly: false
radio_1:
  readonly: false
dropdown_1:
  readonly: false
pypdfform fill sample_template_with_dropdown.pdf -f fill.yaml -o readonly.pdf --readonly
pypdfform update field readonly.pdf -f editable.yaml -o output.pdf

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:

from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].hidden = True

form.write("output.pdf")

Set hidden to true or false for the fields whose visibility should change:

test:
  hidden: true
pypdfform update field sample_template.pdf -f data.yaml -o output.pdf

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:

from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.remove_fields(["test", "test_2", "check_2"])

form.write("output.pdf")

Repeat --field for each form field to remove:

pypdfform remove field sample_template.pdf \
    --field test \
    --field test_2 \
    --field check_2 \
    -o output.pdf