Skip to content

Commit

Permalink
Merge pull request #564 from chinapandaman/PPF-558
Browse files Browse the repository at this point in the history
PPF-558: fix checkbox not display checked on Adobe Acrobat
  • Loading branch information
chinapandaman authored Apr 12, 2024
2 parents 13196b2 + 00109c8 commit 1101bb4
Show file tree
Hide file tree
Showing 25 changed files with 30 additions and 7 deletions.
10 changes: 5 additions & 5 deletions PyPDFForm/filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,20 @@ def simple_fill(
key = get_widget_key(annot.get_object())

widget = widgets.get(key)
if widget is None:
if widget is None or widget.value is None:
continue

if type(widget) is Checkbox and widget.value is True:
simple_update_checkbox_value(annot)
if type(widget) is Checkbox:
simple_update_checkbox_value(annot, widget.value)
elif isinstance(widget, Radio):
if key not in radio_button_tracker:
radio_button_tracker[key] = 0
radio_button_tracker[key] += 1
if widget.value == radio_button_tracker[key] - 1:
simple_update_radio_value(annot)
elif isinstance(widget, Dropdown) and widget.value is not None:
elif isinstance(widget, Dropdown):
simple_update_dropdown_value(annot, widget)
elif isinstance(widget, Text) and widget.value:
elif isinstance(widget, Text):
simple_update_text_value(annot, widget)

if flatten:
Expand Down
5 changes: 3 additions & 2 deletions PyPDFForm/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@
]


def simple_update_checkbox_value(annot: DictionaryObject) -> None:
def simple_update_checkbox_value(annot: DictionaryObject, check: bool = False) -> None:
"""Patterns to update values for checkbox annotations."""

for each in annot[AP][D]: # noqa
if str(each) != Off:
if (check and str(each) != Off) or (not check and str(each) == Off):
annot[NameObject(AS)] = NameObject(each)
annot[NameObject(V)] = NameObject(each)
break


Expand Down
Binary file modified pdf_samples/simple/dropdown/dropdown_four.pdf
Binary file not shown.
Binary file modified pdf_samples/simple/dropdown/dropdown_one.pdf
Binary file not shown.
Binary file modified pdf_samples/simple/dropdown/dropdown_three.pdf
Binary file not shown.
Binary file modified pdf_samples/simple/dropdown/dropdown_two.pdf
Binary file not shown.
Binary file modified pdf_samples/simple/dropdown/dropdown_two_simple.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified pdf_samples/simple/paragraph/sample_filled_sejda_complex.pdf
Binary file not shown.
Binary file not shown.
Binary file modified pdf_samples/simple/sample_filled.pdf
Binary file not shown.
Binary file modified pdf_samples/simple/sample_filled_sejda.pdf
Binary file not shown.
Binary file not shown.
Binary file modified pdf_samples/simple/scenario/issues/437_expected.pdf
Binary file not shown.
Binary file modified pdf_samples/simple/scenario/tools/docfly_expected.pdf
Binary file not shown.
Binary file modified pdf_samples/simple/scenario/tools/pdf_escape_expected.pdf
Binary file not shown.
Binary file not shown.
Binary file added pdf_samples/simple/undo/test_undo_checkbox.pdf
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/test_functional_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,25 @@ def test_fill_complex_fonts(sample_template_with_complex_fonts, pdf_samples, req
if os.name != "nt":
assert len(obj.read()) == len(expected)
assert obj.stream == expected


def test_undo_checkbox(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "simple", "undo", "test_undo_checkbox.pdf")
with open(expected_path, "rb+") as f:
obj = FormWrapper(
os.path.join(pdf_samples, "simple", "undo", "sample_template_filled.pdf")
).fill(
{
"check": False,
"check_2": False,
"check_3": False,
},
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.stream) == len(expected)
assert obj.stream == expected

0 comments on commit 1101bb4

Please sign in to comment.