How to Add Watermark to a PDF File Using Python
Hey there everyone, Today we are going to learn how to add a watermark to a pdf file using Python.
We will be using the PyPDF2 library of Python which is capable of merging two pdf files.
Add Watermark to a PDF file in Python
We have two pdf files one of which contains only text(can also have images) and the other one contains the watermark to be added.
The basic idea behind this would be merging the two pdf files.
Our watermark file “watermark.pdf” is:
Screenshot of the file is below:
We will be adding the above-mentioned watermark to the pdf file “doc.pdf”:
After merging the above two pdf files we will get our output file containing the contents of both “watermark.pdf” and “doc.pdf”.
LET’S DISCUSS THE STEPS INVOLVED :
- Importing the PyPDF2 module.
- Storing the contents of the pdf file and the watermark file.
pdf_file = "doc.pdf" watermark = "watermark.pdf" merged_file = "merged.pdf"
- Open and Read the pdf file and the watermark file.
input_file = open(pdf_file,'rb') input_pdf = PyPDF2.PdfFileReader(pdf_file) watermark_file = open(watermark,'rb') watermark_pdf = PyPDF2.PdfFileReader(watermark_file)
- Accessing the pages of the pdf file and the watermark file to be merged, Index 0 is used to access the first page.
pdf_page = input_pdf.getPage(0) watermark_page = watermark_pdf.getPage(0)
- Merging the pages.
- Saving our file in the output.
output = PyPDF2.PdfFileWriter() output.addPage(pdf_page)
- The final pdf file after adding the watermark is stored in merged_file.
merged_file = open(merged_file,'wb') output.write(merged_file)
- closing the files.
merged_file.close() watermark_file.close() input_file.close()
Python program to add watermark to pdf
import PyPDF2 pdf_file = "doc.pdf" watermark = "watermark.pdf" merged_file = "merged.pdf" input_file = open(pdf_file,'rb') input_pdf = PyPDF2.PdfFileReader(input_file) watermark_file = open(watermark,'rb') watermark_pdf = PyPDF2.PdfFileReader(watermark_file) pdf_page = input_pdf.getPage(0) watermark_page = watermark_pdf.getPage(0) pdf_page.mergePage(watermark_page) output = PyPDF2.PdfFileWriter() output.addPage(pdf_page) merged_file = open(merged_file,'wb') output.write(merged_file) merged_file.close() watermark_file.close() input_file.close()
After the successful execution of this code, we will have our output pdf file named “merged.pdf”.
any idea how to make watermark background transparent?
The transparency of the watermark can be adjusted while creating it.
how can i do that? Because when I overlay like this code suggest it covers my text
So I am doing this with a 6 page input pdf and want all the pages to have the watermark on them. The loop seems to work as I get the input pdf out but none of the pages have the watermark. The watermark pdf is a blank page with an image covering the whole page with no margins. Any ideas why it would not show up?
The above code can only watermark one page… Here is the improved one with unlimited page compatability..
template = PyPDF2.PdfFileReader(open(“inputPDF”, ‘rb’))
watermark = PyPDF2.PdfFileReader(open(“WaterMarkPDF”, ‘rb’))
output = PyPDF2.PdfFileWriter()
for i in range(template.getNumPages()):
page = template.getPage(i)
file = open(“waterMarked_PDF.pdf”, ‘wb’)
input_file = open(pdf_file,’rb’)
input_pdf = PyPDF2.PdfFileReader(pdf_file)
its input file at reader !
Thank you very much. The code has been updated now.
Is there any way I could add a position for the watermark to be placed?
If for example I was to use a 1200x500px PDF as the watermark, could I tell it to place it over the bottom right corner of the PDF?
set the position in the watermark.pdf file
Explained Beautifully, thank you so much
Hi, can you make the same but with a user interface allowing users to select logbook from the Gui
this doesn’t add any transparency to the watermark, so it will cover the existing text
Helped me a lot, thank you!
Is it possible to also add custom text to the pdfs?
Like Adding additionally a unique number to every page?
I copied this code to watermark a pdf file, I followed all the steps that u mentioned, but when I do it and create the output file, the text in the input file is not visible and only watermark is visible, but when I extract the content of the output file all the text and watermark content is printed in terminal.Can u please guide me with this.