Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Support qualified names in update_page_form_field_values #1695

Merged
merged 5 commits into from
Mar 14, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,23 @@ def appendPagesFromReader(
)
self.append_pages_from_reader(reader, after_page_append)

def _get_qualified_field_name(self, parent: DictionaryObject) -> Optional[str]:
if "/TM" in parent:
return cast(str, parent["/TM"])
elif "/T" not in parent:
return None
elif "/Parent" in parent:
qualified_parent = self._get_qualified_field_name(
cast(DictionaryObject, parent["/Parent"])
)
if qualified_parent is not None:
return (
qualified_parent
+ "."
+ cast(str, parent["/T"])
)
return cast(str, parent["/T"])

def update_page_form_field_values(
self,
page: PageObject,
Expand Down Expand Up @@ -795,11 +812,14 @@ def update_page_form_field_values(
for j in range(len(page[PG.ANNOTS])): # type: ignore
writer_annot = page[PG.ANNOTS][j].get_object() # type: ignore
# retrieve parent field values, if present
writer_parent_annot = {} # fallback if it's not there
writer_parent_annot = cast(DictionaryObject, {}) # fallback if it's not there
xi marked this conversation as resolved.
Show resolved Hide resolved
if PG.PARENT in writer_annot:
writer_parent_annot = writer_annot[PG.PARENT]
for field in fields:
if writer_annot.get(FieldDictionaryAttributes.T) == field:
if (
writer_annot.get(FieldDictionaryAttributes.T) == field
or self._get_qualified_field_name(writer_annot) == field
):
if writer_annot.get(FieldDictionaryAttributes.FT) == "/Btn":
writer_annot.update(
{
Expand All @@ -823,7 +843,10 @@ def update_page_form_field_values(
)
}
)
elif writer_parent_annot.get(FieldDictionaryAttributes.T) == field:
elif (
writer_parent_annot.get(FieldDictionaryAttributes.T) == field
or self._get_qualified_field_name(writer_parent_annot) == field
):
writer_parent_annot.update(
{
NameObject(FieldDictionaryAttributes.V): TextStringObject(
Expand Down