Add watermark to your PDF file with Python

Add watermark to your PDF file with Python

Today I will show you how you can simply add a watermark to all pages within a PDF file.
I’ll use PyPDF2

A Pure-Python library built as a PDF toolkit. It is capable of:

extracting document informa…


This content originally appeared on DEV Community and was authored by Stokry

Add watermark to your PDF file with Python

Today I will show you how you can simply add a watermark to all pages within a PDF file.
I'll use PyPDF2

A Pure-Python library built as a PDF toolkit. It is capable of:

  • extracting document information (title, author, …)
  • splitting documents page by page
  • merging documents page by page
  • cropping pages
  • merging multiple pages into a single page
  • encrypting and decrypting PDF files
  • and more!

By being Pure-Python, it should run on any Python platform without any dependencies on external libraries. It can also work entirely on StringIO objects rather than file streams, allowing for PDF manipulation in memory. It is therefore a useful tool for websites that manage or manipulate PDFs.

Let's jump to the code

from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter

pdf_file = r'C:\Users\Stokry\Desktop\python\pdf\test1.pdf'
watermark = r'C:\Users\Stokry\Desktop\python\pdf\logopdf.pdf'
merged = r'C:\Users\Stokry\Desktop\python\pdf\merged.pdf'

with open(pdf_file, "rb") as input_file, open(watermark, "rb") as watermark_file:
    input_pdf = PdfFileReader(input_file)
    watermark_pdf = PdfFileReader(watermark_file)
    watermark_page = watermark_pdf.getPage(0)

    output = PdfFileWriter()

    for i in range(input_pdf.getNumPages()):
        pdf_page = input_pdf.getPage(i)
        pdf_page.mergePage(watermark_page)
        output.addPage(pdf_page)

    with open(merged, "wb") as merged_file:
        output.write(merged_file)

The code will put a watermark on all pages within the PDF.

That's all. Keep coding.


This content originally appeared on DEV Community and was authored by Stokry


Print Share Comment Cite Upload Translate Updates
APA

Stokry | Sciencx (2021-04-08T07:58:31+00:00) Add watermark to your PDF file with Python. Retrieved from https://www.scien.cx/2021/04/08/add-watermark-to-your-pdf-file-with-python/

MLA
" » Add watermark to your PDF file with Python." Stokry | Sciencx - Thursday April 8, 2021, https://www.scien.cx/2021/04/08/add-watermark-to-your-pdf-file-with-python/
HARVARD
Stokry | Sciencx Thursday April 8, 2021 » Add watermark to your PDF file with Python., viewed ,<https://www.scien.cx/2021/04/08/add-watermark-to-your-pdf-file-with-python/>
VANCOUVER
Stokry | Sciencx - » Add watermark to your PDF file with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/08/add-watermark-to-your-pdf-file-with-python/
CHICAGO
" » Add watermark to your PDF file with Python." Stokry | Sciencx - Accessed . https://www.scien.cx/2021/04/08/add-watermark-to-your-pdf-file-with-python/
IEEE
" » Add watermark to your PDF file with Python." Stokry | Sciencx [Online]. Available: https://www.scien.cx/2021/04/08/add-watermark-to-your-pdf-file-with-python/. [Accessed: ]
rf:citation
» Add watermark to your PDF file with Python | Stokry | Sciencx | https://www.scien.cx/2021/04/08/add-watermark-to-your-pdf-file-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.