Watermark PDF – GNU/Linux.ch

Wed, March 15, 2023, Ralf Hersel

Yesterday there was a request in our TALK roomhow to watermark PDF files on all pages. I like to accept such requirements as a challenge, even if I don’t know what the solution is myself. As prompt suggestions were made ImageMagick or PDFTK called. As shown in this article, the solution is not as simple as was assumed.

It is often correct to analyze a requirement before dealing with possible solutions. So what is this regarding? You have a PDF file that you want to watermark on all pages. The watermark is diagonal text. The watermark should be in the background so that the PDF file is not covered by the watermark. The PDF file must therefore be transparent so that the underlying watermark is not covered by the PDF file.

After understanding the requirement, one can look for possible solutions.

I would like to start with an experiment that is very simple and works.

Attempt 1 – LibreOffice

Open the PDF file in LibreOffice Draw. Then you export the file as a PDF file with the menu item File, Export As, Export as PDF. Select the dot in the export options Watermarked and enter a text. The result looks like this:

The watermark text is inserted vertically, with a fixed size and color as the background. You have no influence on the alignment, size and color. Only the text can be specified.

Attempt 2 – PDFTK (background)

While the first attempt is simple and apparently works, it doesn’t quite meet the requirements. These required diagonal text in the background. The PDF Toolkit (pdf) offers two options that might be good for a solution: background and stamp. For my examples there are two files: Statuten.pdf and Wasserzeichen.pdf. The watermark should be at the bottom and transparently covered by the statutes. And that’s where the problem lies. This obvious command does not produce the desired result:

pdftk Statuten.pdf background Wasserzeichen.pdf output Statuten_mit_Wasserzeichen.pdf

The result is a file that corresponds exactly to the statutes.pdf because it is not transparent and therefore completely covers the watermark. You might turn the command around, but for the same reasons that doesn’t lead to the desired result, because the Watermark.pdf file isn’t transparent either. Now the question arises how to make a PDF file transparent. I haven’t found a solution to this (although there certainly is).

Attempt 3 – PDFTK (stamp)

In this attempt I try it with stamp:

you see it: stamp irons the watermark over the PDF file. Since the watermark.pdf is not transparent either, parts of the original are covered. I created the watermark in LibreOffice Writer by inserting and rotating a Fontwork text. Unfortunately, when exporting to PDF, there is no option to save the file with a transparent background.

Attempt 4 – Transparent watermark

The direction is now clear; either the watermark or the statutes.pdf must be transparent. In this attempt we will make the watermark transparent. To do this, we save the watermark file that we created with LibreOffice Writer as a PNG file. As already announced in the introduction, ImageMagick is now used:

convert Wasserzeichen.png -transparent white -background none Wasserzeichen.pdf
pdftk Statuten.pdf stamp Wasserzeichen.pdf output Statuten_mit_Wasserzeichen.pdf

The first line creates a PDF file with a transparent background from the PNG. The second command stamps the now transparent watermark over the Statutes.pdf. The result looks like this:

That looks workable, although it’s not optimal yet. The watermark is transparent in its white areas, which allows you to read the underlying text. The borders of the watermark are not transparent and cover the text below. This can be desirable because it makes it more difficult to remove the watermark or scan the articles of association.

In order to achieve an optimal solution, the statute text would have to be transparent and the watermark with pdftk background be inserted. I don’t know how that’s supposed to work.

Conclusion

The four attempts produced two viable solutions. If you want it easy, open the PDF file in LibreOffice Draw and save it with a watermark. The fourth attempt serves as an alternative, in which a transparent watermark.png is stamped over the PDF file. It is important to ensure that the watermark text only consists of thin contour lines (LibreOffice Fontwork), otherwise too much of the underlying text will be covered.

Perhaps our readers will find a better solution.

Those: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

Leave a Replay