On this page
Guides
Add Text Watermark
The steps to add a text watermark are as follows:
- Initialize a
CPDFWatermark
object, specifying the watermark type as text. - Set the properties required for the text watermark, including content, font, color, and font size.
- Set the general properties for the watermark.
- Create the watermark in the document.
This example shows how to add a text watermark:
java
CPDFWatermark watermark = (CPDFWatermark) document.createWatermark(CPDFWatermark.Type.WATERMARK_TYPE_TEXT);
watermark.setText("Hello World");// Text for the watermark (not applicable for image watermarks).
watermark.setFontName("Helvetica");// Text font for the watermark (not applicable for image watermarks).
watermark.setTextRGBColor(Color.decode(color).getRGB());// Text color for the watermark (not applicable for image watermarks).
watermark.setFontSize(30);// Default font: Helvetica 24.
watermark.setOpacity(0.5f);// Opacity of the watermark, ranging from 0 to 1, with a default of 1.
watermark.setFront(true);// Position the watermark in front of the content.
watermark.setVertalign(CPDFWatermark.Vertalign.WATERMARK_VERTALIGN_CENTER);// Vertical alignment of the watermark.
watermark.setHorizalign(CPDFWatermark.Horizalign.WATERMARK_HORIZALIGN_CENTER);// Horizontal alignment of the watermark.
watermark.setVertOffset(0);// Translation offset relative to the vertical position. Positive values move downward, while negative values move upward.
watermark.setHorizOffset(0);// Translation offset relative to the horizontal position. Positive values move to the right, while negative values move to the left.
watermark.setScale(1.3f);// Scaling factor for the watermark, with a default value of 1. If it is an image watermark, 1 represents the original size of the image. If it is a text watermark, 1 represents the `textFont` font size.
watermark.setPages("3,4,5");// Set the watermark for pages 3, 4, and 5.
watermark.setFullScreen(true);// Enable watermark tiling (not applicable for image watermarks).
watermark.setHorizontalSpacing(100);// Set the horizontal spacing for tiled watermarks.
watermark.setVerticalSpacing(100);// Set the vertical spacing for tiled watermarks.
watermark.update();
watermark.release();