On this page
Guides
Add Image Watermark
The steps to add an image watermark are as follows:
- Initialize a
CPDFWatermark
object, specifying the watermark type as an image. - Create a Bitmap based on the image file, setting the image source and scaling for the image watermark.
- Set the general properties for the watermark.
- 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();
readerView.reloadPages();
kotlin
val watermark = document.createWatermark(CPDFWatermark.Type.WATERMARK_TYPE_IMG)
watermark.apply {
setImage(bitmap, 100, 200) // Set the image for the watermark (not applicable for text watermarks).
opacity = 0.5F // Watermark opacity, ranging from 0 to 1, with a default of 1.
isFront = true // Position the watermark in front of the content.
vertalign = CPDFWatermark.Vertalign.WATERMARK_VERTALIGN_CENTER // Vertical alignment of the watermark.
horizalign = CPDFWatermark.Horizalign.WATERMARK_HORIZALIGN_CENTER // Horizontal alignment of the watermark.
vertOffset = 0F // Translation offset relative to the vertical position. Positive values move downward, while negative values move upward.
horizOffset = 0F // Translation offset relative to the horizontal position. Positive values move to the right, while negative values move to the left.
scale = 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.
pages = "3,4,5" // Set the watermark for pages 3, 4, and 5.
isFullScreen = true // Enable watermark tiling (not applicable for image watermarks).
horizontalSpacing = 100F // Set the horizontal spacing for tiled watermarks.
verticalSpacing = 100F // Set the vertical spacing for tiled watermarks.
update()
release()
}
readerView.reloadPages()