Updating field in XFA PDF #2809
-
I am attempting to update some fields in an XFA enabled pdf. So far the closest I've come to this is finding /XFA and accessing the dataset portion via This returns the XML that corresponds to all the field values. From there I update the xml with my desired values and then attempt to set it via This throws an Exception: I am extremely new to PDF manipulation and wouldn't be surprised if I'm going entirely in the wrong direction, but does anyone have any advice on how to achieve this? Edit: import pypdf
def findInDict(needle, haystack):
for key in haystack.keys():
try:
value=haystack[key]
except:
continue
if key==needle:
return value
if isinstance(value,dict):
x=findInDict(needle,value)
if x is not None:
return x
pdfobject=open('Test.pdf','rb')
pdf=pypdf.PdfReader(pdfobject)
xfa=findInDict('/XFA',pdf.resolved_objects)
xml=xfa[7].get_object().get_data()
write_xml = xml.replace(b'101101101</ce:PartyIdentificationNumberText\n>', b'202202202</ce:PartyIdentificationNumberText\n>')
xfa[7].get_object().set_data(write_xml)
writer = pypdf.PdfWriter(clone_from=pdf)
outputstream = open('write_file.pdf', 'wb')
writer.write(outputstream)
outputstream.close()
pdf.close() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Please provide some complete code example and an example PDF for further analysis. The error message indicates that the object you are trying to replace does not use the FlateDecode filter. pypdf currently does not know how to encode data with other filters, thus your approach does not work here at the moment. |
Beta Was this translation helpful? Give feedback.
-
Figured out the problem. The issue from the fact that the /Filter here was an |
Beta Was this translation helpful? Give feedback.
Figured out the problem. The issue from the fact that the /Filter here was an
ArrayObject
of["/FlateDecode"]
. inEncodedStreamObject.set_data
the line to check Decode statusif self.get(SA.FILTER, "") == FT.FLATE_DECODE:
returns false.I was able to work around this by setting /Filter to
_base.NameObject("/FlateDecode")
From there the rest of my code ran and the field was updated as expected.