Adding tiled watermarks to PDF documents is an effective way to prevent documents from being pirated. A tiled watermark lets you have tiles of your watermark all over a PDF page. You can specify either a text to be used as a tile, or you can specify a picture for that. This article will demonstrate how to add a tiling watermark effect to PDF documents using Free Spire.PDF for Java.
Before start, you should learn about two important concepts in Spire.PDF – tiling brush and coordinate system. A tiling brush, represented by PdfTitlingBrush class, is used to define the contents of the tile that you use to paint a region. A coordinate system is used to determine where the contents (text or/and images) shoule be placed on a tile.
How is the coordinate system defined?
The origin of the coordinate system (0, 0) represents the top-left corner of a graphics in Spire.PDF.
The x-axis extends to the right and the y-axis extends downward.
How to create a tiling brush?
Step 1. Create a PdfTilingBrush object. Set its width and height to half of the width and height of a PDF page respectively. You can change the ratio according to the actual situation.
Step 2: Translate coordinate system to the lower right.
Step 3: Rotate coordinate system 45 degrees. Step 2 and step 3 enable that the text watermark lies in the center of the brush and is tilted at 45 degrees.
Step 4: Draw text from the starting point (0, 0) of the new coordinate system.
If you want to place an image at the center of a brush, you do not need to transform the coordinate system but calculate the X, Y coordinates directly. After a tiling brush is created, you can use it to paint the whole area of a page to get a tiling background effect.
The following sections provide detailed code snippets showing you how to add text and image watermarks to PDF.
Adding tiled text watermark Output:
Adding tiled image watermark
What a show! What atmosphere! What fun! What a tiny ad!