DIY Tutorial to Add Watermark to PDF Using Python

Discover how to add watermark to PDF using Python easily. This tutorial teaches you step-by-step how to use the PyPDF2 library to automate the process. You'll learn how to brand your documents or print with date and time PDF in just a few lines of code. Start enhancing your PDFs today!

You can use libraries like PyPDF2 or PyMuPDF to add watermark to PDF programmatically. Start by opening the PDF file and creating a watermark. Then, go through each page and add watermark to PDF using Python. Make sure the watermark's transparency and position are what you want. Save the changed PDF and close the files when you're done. That's it!

Throughout this write-up, we will learn these steps in detail. Apart from this, if you are wondering about a better and easier solution to add draft watermark to PDF, image, or date & time. Then within this, you will find the best utility for the same, which can be used even by naive users. 

So, what are you waiting for? Let’s have a quick look at the instructions to insert a watermark in the following section.

How to Add Watermark to PDF Using Python? - Quick Steps

At present, to add watermark to PDF programmatically, there are two most preferred approaches: Python and Java. In this part of the phase, we will learn about them in detail, with their guidelines. 

# Technique 1. Python

Import PyPDF2

def add_watermark(input_pdf, output_pdf, watermark_text):

    # Make a PDF reader object

    pdf_reader = PyPDF2.PdfFileReader(open(input_pdf, 'rb'))

   # Generate a PDF writer object

    pdf_writer = PyPDF2.PdfFileWriter()

    # Repeat through each page of the input PDF

    for page_number in range(pdf_reader.numPages):

        page = pdf_reader.getPage(page_number)

        # Develop a watermark object

        watermark = PyPDF2.pdf.PageObject.createBlankPage(width=page.mediaBox.getWidth(), height=page.mediaBox.getHeight())

        # Design a PDF content stream for the watermark

        watermark.mergePage(page)

        # Create a PDF content stream for the text watermark e.g., “Confidential”

        watermark_content = PyPDF2.pdf.ContentStream([watermark_text])

        watermark.mergePage(watermark_content)

        # Embed the watermarked page to the PDF writer

        pdf_writer.addPage(watermark)

    # Compose the output PDF with the watermark

    with open(output_pdf, 'wb') as output_file:

        pdf_writer.write(output_file)

if __name__ == "__main__":

    input_pdf = 'input.pdf'  # Input PDF file path

    output_pdf = 'output.pdf'  # Output PDF file path

    watermark_text = 'CONFIDENTIAL'  # Watermark text

    add_watermark(input_pdf, output_pdf, watermark_text)

Remember: You have to change 'input.pdf' and 'output.pdf' to the locations of your PDF files.

# Technique 2. Add Watermark to PDF Via Java 

Add watermark to PDF using Python can be simpler but if you need to execute the task with another programming language( Java). Then, you first have to get and include the PDFBox library using a pom.xml file. 

<dependency>

    <groupId>org.apache.pdfbox</groupId>

    <artifactId>pdfbox</artifactId>

    <version>2.0.24</version> <!-- Make sure to use the latest version -->

</dependency>

After that, similar to add watermark to PDF using Python, you have to follow the below instructions for Java:

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.pdmodel.PDPage;

import org.apache.pdfbox.pdmodel.PDPageContentStream;

import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.File;

import java.io.IOException;

public class AddWatermarkToPDF {

    public static void main(String[] args) {

        String inputPdfPath = "input.pdf";

        String outputPdfPath = "output.pdf";

        String watermarkText = "Draft";

        try {

            // Open the existing PDF

            PDDocument document = PDDocument.load(Current File(inputPdfPath));

            // Acquire the number of pages in the files

            int pageCount = document.getNumberOfPages();

            // Embed the watermark to every page

            for (int i = 0; i < pageCount; i++) {

                PDPage page = document.getPage(i);

                PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);

                // Adjust font and font size, according to your needs

                contentStream.setFont(PDType1Font.HELVETICA_BOLD, 48);

                // Select color

                contentStream.setNonStrokingColor(200, 200, 200);

                // Set the position of the watermark

                float x = (page.getMediaBox().getWidth() - contentStream.getStringWidth(watermarkText) / 2) / 2;

                float y = (page.getMediaBox().getHeight() - contentStream.getFont().getFontDescriptor().getFontBoundingBox().getHeight()) / 2;

                // Insert the watermark

                contentStream.beginText();

                contentStream.showText(watermarkText);

                contentStream.endText();

                contentStream.close();

            }

            // Save the PDFs

            document.save(outputPdfPath);

            document.close();

            

            System.out.println("Watermark added successfully.");

        } catch (IOException e) {

            e.printStackTrace();

     }

    }

}

Easiest Way to Insert Watermark in Numerous PDFs 

As we discussed at the beginning of our discussion, both programming language seems complex and time-consuming whether add watermark to PDF using Python or with Java. However, there are many other manual approaches on the internet like Adobe Acrobat Pro, MS Word, and many more. But, they are not up to the mark, as well as they are expensive to buy for many users. 

Therefore, to overcome any complications and tediousness, you can opt for PDF Watermark Creator Software. This is the best utility to insert watermarks in different types like text, date & time, and images in multiple PDFs concurrently. Plus, using this you can embed watermark even in password-protected PDFs. On top of that, with this, you won’t face any compatibility issues as you can operate this in every Windows OS. 

Instructions of the Specified Solution

Let’s go through the steps to embed watermarks in portable document format:

Step 1. Open the software and choose to add PDF files using either the "Add Files" or "Add Folder" option.

Step 2. Find where your PDF files are stored on your computer and select the ones you want to use. Then, click "Open."

Step 3. Decide if you want to add text, an image, or the date and time as a watermark.

Step 4. Pick where you want the watermark to go on the page: at the top, in the middle, or at the bottom.

Step 5. Adjust how visible the watermark is by changing its opacity level.

Step 6. Take a look at how the PDF page will appear with the watermark using the "Preview" feature.

Step 7. Lastly, click "Generate" to complete the process.

Read Also: How to Remove Watermark From Locked PDF?

So there You Have it

This guide explains i.e. how to add watermark to PDF using Python or add watermark to PDF programmatically via Java. Additionally, in this, we covered the most reliable solution to complete our task simultaneously. Not only this, we learned about the step-by-step instructions of both manual and automated solutions. So, finally, we conclude that the mentioned advanced utility is efficient for this task, as compared to any other solution.  

20 Feb 2024


Creating portfolio made simple for

Trusted by 39000+ Generalists. Try it now, free to use

Start making more money