I followed this guide on how to use PyPDF2, and the text fields are filled. The only issue is that I see the blue overlay on the text field and the text written underneath it is only shown if I click on the blue overlay, please see this picture:
The Full Last Name field is if a user types in the field. The Full First Name field is if I click on the blue overlay to reveal the text written by PyPDF2, and the Full Middle Name field is how the PyPDF2-written-to-text field looks if I don't click on the field. Is there anyway to have the middle and right text fields look like the left one using PyPDF2 or a related library?
My code is below:
#!/usr/bin/python3
filename = "file.pdf"
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject
def set_need_appearances_writer(writer):
try:
catalog = writer._root_object
# get the AcroForm tree and add "/NeedAppearances attribute
if "/AcroForm" not in catalog:
writer._root_object.update(
{
NameObject("/AcroForm"): IndirectObject(
len(writer._objects), 0, writer
)
}
)
need_appearances = NameObject("/NeedAppearances")
writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
return writer
except Exception as e:
print("set_need_appearances_writer() catch : ", repr(e))
return writer
reader = PdfReader(filename, "rb")
if "/AcroForm" in reader.trailer["/Root"]:
reader.trailer["/Root"]["/AcroForm"].update(
{NameObject("/NeedAppearances"): BooleanObject(True)}
)
blankTextFields = reader.get_form_text_fields()
blankTextFieldsKeys = blankTextFields.keys()
pdfFillerDict = {}
for key in blankTextFieldsKeys:
if "BarCode" not in key:
value = input("What is your " + key[:-3] + "? ")
pdfFillerDict.update({key: value})
writer = PdfWriter()
set_need_appearances_writer(writer)
if "/AcroForm" in writer._root_object:
writer._root_object["/AcroForm"].update(
{NameObject("/NeedAppearances"): BooleanObject(True)}
)
writer.add_page(reader.pages[0])
print(blankTextFields)
print(pdfFillerDict)
writer.update_page_form_field_values(
writer.pages[0], pdfFillerDict
)
with open("filled-out.pdf", "wb") as output_stream:
writer.write(output_stream)
output_stream.close()
Blue is the readers default colour for Fields otherwise how would you know what is what on a transparent background?
Clearly this is a bad sample but the blue is true. Annotation including Fields are topmost so if the data is not within the field it does not appear. Thus it appears in your case the text is perhaps in the body and thus masked by the fields. Here the fields were badly filled using non FDF methods (you can see the FDF entries are imbedded in the data) and the file thus misbehaves.
The OP is attempting to fill fields in a cover sheet of an XFA scripted form which needs the JavaScript to be running when filled by users. Theses exotic XML templates are to be avoided at all costs as only designed to be filled by individuals as part of a personal commitment such as alienation or financial agreements, so cannot be agreed by machines. Such files usually have a cover sheet that states clearly they can only be used in Acrobat Reader or better.
These forms use a different Methodology best done via CSV imports such as in this case this column to field mapping
For me I just get an expected FAIL as the data structure I test cannot work that way and the Adobe Developer Support says use a Neutered XFA export to as a true PDF to enable PDF AcroForms population.
When filled by an individual the Scripting will rewrite the barcode and verify data submission, for attachment to the other parts of personalised submission.
I think in this SPECIFIC case since the form is plain (only the barcode needs considering as off-beat) that just print the background PDF (Flat no scripts, no fields) then add your own text, as an overlay, with somehow your own barcode (or leave as is & hope they dont notice it is the stock empty one.)
Many users may submit without unsecured scripts active.
Simpler means to bulk load FDF may seem to a beginner as counter intuitive. The way FDF import/export works is you draw the background template.pdf, you float the fields over the template, & export data only by submission. Then the reverse. you write the edits to data template & open data file which imports background.pdf it is simple using AcroReader double click the FDF which loads the PDF job done. For programmers the open of FDF & import PDF is easiest done using Adobe FDFtools or alternatives such as PDFtk (CLI) or PFDXedit (GUI/CLI) or https://stackoverflow.com/search?q=python+FDF