Add watermark to PDF in Python

In various cases, a watermark is applied to the pages in PDF documents. The watermark could be a piece of text or an image. The purpose of watermarking PDF files could be to insert the status of the document, e.g. draft, or to protect confidential information and so on. In this article, you will learn how to add a watermark to a PDF in Python.

We will go through the step-by-step process and then the code samples of applying the watermarks to PDF. Also, the text and image watermarking for PDF in Python will be covered explicitly.

Python Library to Add Watermark in PDF

To add a watermark in PDF files, we will use Aspose.PDF for Python. The library provides a complete set of features to create, manipulate, and protect PDF files. It lets you apply text and image watermarks to PDF pages in a few simple steps. You can install the library using the following pip command.

pip install aspose-pdf

Add Watermark to PDF in Python

There are two commonly used types of watermarks: image and text. Image watermarks in PDFs are usually used to insert a logo, trademark, etc. Whereas, a text watermark is applied to indicate the status of the document, put the company’s name or a note, etc. The following sections demonstrate how to add image and text watermarks in PDF with Python.

Insert Text Watermark in PDF using Python

The following are the steps to apply a text watermark in PDF in Python.

  • First, load the PDF file using Document class.
  • Then, create an instance of TextStamp class and initialize it with watermark text.
  • Set properties of the watermark including ID, position, size, and font.
  • Add TextStamp to the page using Document.pages[pageNumber].add_stamp(TextStamp) method.
  • Finally, save the watermarked PDF using Document.save(string) method.

The below code sample shows how to apply a text watermark to PDF in Python.

Apply Image Watermark to PDF in Python

The following are the steps to insert an image watermark in PDF using Python.

  • First, load the PDF file using Document class.
  • Then, create an instance of ImageStamp class and initialize it with the watermark image’s path.
  • Set properties of the watermark including ID, position, and size.
  • Add ImageStamp to the page using Document.pages[pageNumber].add_stamp(ImageStamp) method.
  • At the end, save the watermarked PDF using Document.save(string) method.

The following code sample shows how to add an image watermark to PDF in Python.

Watermark PDF Files Online

We have also developed a free online tool to watermark PDF files. You can use this tool to add watermarks to your PDF files without creating an account.

Get a Free License

You can get a free temporary license and evaluate the PDF watermarking without any limitations.

Explore Python PDF Library

Visit the documentation of our Python PDF library to explore other PDF manipulation features. In case you would have any suggestions, questions, or queries, share them with us via our forum.

Conclusion

In this article, you have learned how to watermark PDF files in Python. The guidelines and code samples have explicitly covered how to add text and image watermarks to PDF files. In addition, we have provided you with a free web-based tool to watermark PDF files online. You can simply install Aspose.PDF for Python in your application and enjoy its amazing features.

See Also