pdfrw icon indicating copy to clipboard operation
pdfrw copied to clipboard

Annotate a pdf

Open jancoow opened this issue 6 years ago • 3 comments

Hi. I'm trying to fill some fields in a pdf file and write it to a new file. It works in most PDF readers, however the fields dissapear in Adobe Reader.

When printing all the annotations of the template file with this code:

annotations = template_pdf.pages[0]['/Annots']
for annotation in annotations:
    print(annotation)
    print("--")

The output is:

{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (189, 0), '/Subtype': '/Widget', '/T': '(company_name)', '/Type': '/Annot'}
--
{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (194, 0), '/Subtype': '/Widget', '/T': '(first_name)', '/Type': '/Annot'}
--
{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (199, 0), '/Subtype': '/Widget', '/T': '(street_name)', '/Type': '/Annot'}
--
{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (204, 0), '/Subtype': '/Widget', '/T': '(zip_code)', '/Type': '/Annot'}
...

This looks like a normaly array with every annotation in a different object.

And after I write the values to the fields with:

for annotation in annotations:
    annotation.V = "TEST"
    print(annotation)
    print("--")

The output is:

{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (189, 0), '/Subtype': '/Widget', '/T': '(company_name)', '/Type': '/Annot', '/V': 'TEST'}
--
{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (194, 0), '/Subtype': '/Widget', '/T': '(first_name)', '/Type': '/Annot', '/V': 'TEST'}
--
{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (199, 0), '/Subtype': '/Widget', '/T': '(street_name)', '/Type': '/Annot', '/V': 'TEST'}
--
{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (16, 0), '/Rect': (204, 0), '/Subtype': '/Widget', '/T': '(zip_code)', '/Type': '/Annot', '/V': 'TEST'}
...

Which should be correct according the pdf documentation. However, after writing this to a new file with pdfrw.PdfWriter("tmp.pdf", trailer=template_pdf).write()

And reopening the file again with pdfrw.pdfReader, the annotation looks like this:

{'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': {'/Annots': [{...}, {'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': {...}, '/Rect': (525, 0), '/Subtype': '/Widget', '/T': '(first_name)', '/Type': '/Annot', '/V': '(TEST)'}, {'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': {...}, '/Rect': (520, 0), '/Subtype': '/Widget', '/T': '(street_name)', '/Type': '/Annot', '/V': '(TEST)'}, {'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': {...}, '/Rect': (515, 0), '/Subtype': '/Widget', '/T': '(zip_code)', '/Type': '/Annot', '/V': '(TEST)'}, {'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': {...}, '/Rect': (510, 0), '/Subtype': '/Widget', '/T': '(insertion)', '/Type': '/Annot', '/V': '(TEST)'}, {'/DA': '(/Poppins 10 Tf 0 g)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': {...}, '/Rect': (505, 0), '/Subtype': '/Widget', '/T': '(last_name)', '/Type': '/Annot', '/V': '(TEST)'}, 

I've cut the output, because the output is about 300 lines long. This suddenly looks like an object in an object in an object..

How could this be? It looks like the annotations are totally written wrong to the pdf file. I just want to write a new value of the fields and have them visible in every reader. But the way pdfrw is writing the pdf files looks totally wrong.

jancoow avatar Nov 15 '18 11:11 jancoow

Hi jancoow, Check the following post How to Populate Fillable PDF's with Python. It uses pdfrw to populate fields.

PeterSlezak avatar Nov 29 '18 21:11 PeterSlezak

@PeterSlezak Hi. This is exactly what my code is doing. I wrote the values directly with the given key instead of using annotation.update(). However, I tested the exact same code as your example and this is having the same issue. The written values doesn't appear in Adobe Reader or in the slack PDF viewer, while the Chrome PDF viewer displays the PDF correctly.

jancoow avatar Dec 04 '18 13:12 jancoow

This is an old post, but it is still open. In post #84 , TLK3's comment solved my problem.

AnderssonDavid avatar Apr 18 '20 08:04 AnderssonDavid