A repeated (tiled) watermark fills the whole page with copies of a text or an image. Such a pattern is much harder to crop out or cover than a single watermark, so it suits both branding and document security. You can configure the spacing between the copies, their rotation and the layout of the pattern.
Text repeated watermark
The code snippet below demonstrates how to add a tiled watermark to a document. To configure tiling, use the TileOptions class and pass it to the setTileOptions method of the watermark. In this example, the line spacing (the spacing between parallel rows of tiles) is 12 percent of the page size, and the watermark spacing (the spacing between watermarks in a row) is 10 percent of the page size. Both values are MeasureValue objects. The following image explains the purpose of the line spacing and watermark spacing settings:
The watermark is also rotated by 30 degrees counterclockwise (a negative angle).
importcom.groupdocs.watermark.Watermarker;importcom.groupdocs.watermark.watermarks.Color;importcom.groupdocs.watermark.watermarks.Font;importcom.groupdocs.watermark.watermarks.FontStyle;importcom.groupdocs.watermark.watermarks.MeasureValue;importcom.groupdocs.watermark.watermarks.TextWatermark;importcom.groupdocs.watermark.watermarks.TileMeasureType;importcom.groupdocs.watermark.watermarks.TileOptions;try(Watermarkerwatermarker=newWatermarker("sample.pdf")){// Initialize the font to be used for watermark
Fontfont=newFont("Arial",19,FontStyle.Bold|FontStyle.Italic);// Create the watermark object
TextWatermarkwatermark=newTextWatermark("Watermark",font);watermark.setForegroundColor(Color.getRed());// Configure tile options
MeasureValuelineSpacing=newMeasureValue();lineSpacing.setMeasureType(TileMeasureType.Percent);lineSpacing.setValue(12);MeasureValuewatermarkSpacing=newMeasureValue();watermarkSpacing.setMeasureType(TileMeasureType.Percent);watermarkSpacing.setValue(10);TileOptionstileOptions=newTileOptions();tileOptions.setLineSpacing(lineSpacing);tileOptions.setWatermarkSpacing(watermarkSpacing);watermark.setTileOptions(tileOptions);// Set watermark properties
watermark.setOpacity(0.2);watermark.setRotateAngle(-30);// Add watermark
watermarker.add(watermark);watermarker.save("result.pdf");}
The output of the code above shows repeated text watermarks:
TileMeasureType sets the unit of a MeasureValue: Pixel, Percent (of the page size) or Points. The value must be greater than 0; setValue throws an exception with the message “Value must be greater than 0.” for zero and negative values.
Rotation around the origin
In the image above, each row of tiles is rotated relative to the left edge of the document. You can rotate the whole pattern around the bottom left corner of the document instead. To do this, set setRotateAroundOrigin of the TileOptions class to true:
In this case, the repeated watermark looks like this:
Note
setRotateAroundOrigin was added in version 26.11. It replaces setRotateAroundCenter, which is deprecated and calls the new method.
Tile patterns
The setTileType method of the TileOptions class sets the tile pattern. It takes one of the constants of the com.groupdocs.watermark.watermarks.TileType class:
Constant
Pattern
TileType.Straight
The default. Rows of tiles are aligned with each other.
TileType.Offset
Every second row is shifted by a half of the tile step.
TileType.OneThirdOffset
Every second row is shifted by two thirds of the tile step.
TileType.BasketWeave
Tiles are laid out in alternating groups of three horizontal and three vertical watermarks. The rotation angle of the watermark is ignored.
For example, add the following line to the previous example to enable the offset pattern:
With the offset pattern, each row of tiles is shifted relative to the previous one. Instead of a strict grid, the watermarks form a more dynamic pattern that is harder to remove:
The following line enables the basket weave pattern:
TileType.OneThirdOffset and TileType.BasketWeave are available since version 26.11.
Image repeated watermark
Repeated watermarks also work with images. The following code adds rotated repeated image watermarks to a PDF document.
importcom.groupdocs.watermark.Watermarker;importcom.groupdocs.watermark.watermarks.ImageWatermark;importcom.groupdocs.watermark.watermarks.MeasureValue;importcom.groupdocs.watermark.watermarks.TileMeasureType;importcom.groupdocs.watermark.watermarks.TileOptions;try(Watermarkerwatermarker=newWatermarker("sample.pdf");// Create the image watermark object
ImageWatermarkwatermark=newImageWatermark("logo.png")){// Configure tile options
MeasureValuelineSpacing=newMeasureValue();lineSpacing.setMeasureType(TileMeasureType.Percent);lineSpacing.setValue(12);MeasureValuewatermarkSpacing=newMeasureValue();watermarkSpacing.setMeasureType(TileMeasureType.Percent);watermarkSpacing.setValue(10);TileOptionstileOptions=newTileOptions();tileOptions.setLineSpacing(lineSpacing);tileOptions.setWatermarkSpacing(watermarkSpacing);watermark.setTileOptions(tileOptions);// Set watermark properties
watermark.setOpacity(0.3);watermark.setRotateAngle(-30);// Add watermark
watermarker.add(watermark);watermarker.save("result.pdf");}
This is the resulting appearance of the document after applying tiled image watermarks:
More examples
The GroupDocs.Watermark for Java examples repository contains runnable tiled watermark examples: basic_usage.AddTextTiledWatermark and basic_usage.AddImageTiledWatermark.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Ask AI
Ask anything about this page — the answer is based on its content.
Searching the docs and writing an answer…
Something went wrong while getting an answer. Please try again in a moment.