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:
watermark.pdf

Screenshot of the file is below:

add watermark to pdf file in Python

We will be adding the above-mentioned watermark to the pdf file “doc.pdf”:
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.
    import PyPDF2
  • 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.
    pdf_page.mergePage(watermark_page)
  • 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”.
merged.pdf

Screenshot:

watermarking to pdf

16 responses to “How to Add Watermark to a PDF File Using Python”

  1. Shyok Mutsuddi says:

    Nice explaination!

  2. Sulaiman says:

    any idea how to make watermark background transparent?

  3. HeIsRealMagic says:

    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?

  4. Asif Fazal says:

    The above code can only watermark one page… Here is the improved one with unlimited page compatability..

    import PyPDF2

    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)
    page.mergePage(watermark.getPage(0))
    output.addPage(page)

    file = open(“waterMarked_PDF.pdf”, ‘wb’)
    output.write(file)

  5. Sale says:

    input_file = open(pdf_file,’rb’)
    input_pdf = PyPDF2.PdfFileReader(pdf_file)
    ——————————————————————

    its input file at reader !

  6. Harrison says:

    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?

  7. lin says:

    Explained Beautifully, thank you so much

  8. Not_the_radbrad says:

    Hi, can you make the same but with a user interface allowing users to select logbook from the Gui

  9. dan says:

    this doesn’t add any transparency to the watermark, so it will cover the existing text

  10. niclas says:

    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?

  11. Adarsh M Jatti says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *