Skip to content
Guides

Add Image Watermark

The steps to add an image watermark are as follows:

  1. Initialize a CPDFWatermark object, specifying the watermark type as an image.
  2. Create a Bitmap based on the image file, setting the image source and scaling for the image watermark.
  3. Set the general properties for the watermark.
  4. Create the watermark in the document.

This example shows how to add an image watermark:

Java
CPDFWatermark watermark = (CPDFWatermark) document.createWatermark(CPDFWatermark.Type.WATERMARK_TYPE_IMG);
watermark.setImage(bitmap, 100, 200);// Set the image for the watermark (not applicable for text watermarks).
watermark.setOpacity(0.5f);// Watermark opacity, 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();