I'm writing data to a PDF with named fields and then changing the attributes of those fields to make them readonly. This is great, but I'd like to be able to manipulate the text as well, change the font size, maybe even the font itself.
According to the PDF docs, /DA should control the text so I've attempted to set;
NameObject('/DA'): TextStringObject("font: bold italic Courier 80pt;")
However this doesn't manipulate the text at all.
Below is the code used to add the data & then manipulate the fields, which works perfectly other than the setting of the font.
I've also tried to use the /DS flag to set the font & this also had no impact.
pdf_reader = PdfFileReader(
open(full_certificate_path, "rb"), strict=False
)
pdf_writer = PdfFileWriter()
data_dict = {
'field1': event.title,
'field2': user.name,
'field3': strfdelta(
completion_time,
"{hours}:{minutes}:{seconds}"
),
}
pdf_writer.addPage(
pdf_reader.getPage(0)
)
try:
# Add data to a page
page = pdf_writer.getPage(0)
pdf_writer.updatePageFormFieldValues(page, data_dict)
for j in range(0, len(page['/Annots'])):
writer_annot = page['/Annots'][j].getObject()
writer_annot.update({
# Q: Text justification
# 0: left
# 1: centre
# 2: right
NameObject("/Q"): NumberObject(1),
# Default: '/DA' /Helv 12 Tf 0 g
NameObject('/DA'): TextStringObject(
"font: bold italic Courier 80pt;"
),
# Ff: Set field flags
# 1: ReadOnly
NameObject("/Ff"): NumberObject(1),
})
except KeyError:
print("No annotations/fields in the doc")
output_stream = StringIO()
pdf_writer.write(output_stream)