Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert annotation into pdf with Python

Tags:

python

pdf

I want to add text or annotation in the exsting pdf file to interpret some key words.

At first I tried the pyPdf & reportlib to merge t he original pdf file & new generated interpretion pdf file, but it doesn't work. Because the original file keep out all the words of interpretation pdf and make new pdf file invisible. Don't know why? If I test to merge two new generated interpretion pdf file into one, it works well.

So I am thinking to try to use another way to insert just annotation into existing pdf file by python. Anybody have related experience can give me suggestion? Thanks!

like image 425
Greatvia Avatar asked Nov 12 '22 13:11

Greatvia


1 Answers

Adding a watermark to existing pdf using PyPDF certainly works for me:

template = PdfFileReader(file("template.pdf", "rb")) #template pdf
output=PdfFileWriter() #writer for the merged pdf
for i in range(new.getNumPages()):
    page=template.getPage(i)
    page.mergePage(new.getPage(i))
    output.addPage(page)

Read my other SO answer for reference.

Read my complete article to know more about pdf generation and merging in python.

like image 114
Prahlad Yeri Avatar answered Nov 15 '22 05:11

Prahlad Yeri