Skip to main content

WatermarkAnnotation Class

Defines a watermark annotation in a PDF document.

Declaration

public class WatermarkAnnotation extends BaseAnnotation

Remarks

A watermark annotation displays text or graphics behind or in front of page content. Use watermark annotations to identify document status, ownership, confidentiality, or draft versions.

To create a watermark annotation, do the following:

  1. Create a FormTemplate that defines the watermark appearance. You can display text, images, vector graphics, or any combination of page fragments.
  2. Create a WatermarkAnnotation.
  3. Assign the form template to the annotation’s normal appearance. A watermark annotation uses an AnnotationAppearance to define its visual content.
  4. Add the annotation to the page’s annotation collection.

The following code snippet implements the addTextWatermark method, which creates a diagonal text watermark and adds it to a page:

 Watermark Annotation, DevExpress PDF Document API for Java

import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;

import java.io.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Load the PDF document.
        try (InputStream documentStream =
                     Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(documentStream)) {

            // Add the watermark to each page.
            for(Page page : pdfDocument.getPages()) {
                addTextWatermark(page, "CONFIDENTIAL");
            }

            // Save the updated document.
            pdfDocument.save(documentStream);
        }
    }
    static void addTextWatermark(Page page, String watermarkText) {
        // Get page boundaries.
        RectangleF pageBounds = page.getMediaBox();

        // Create a reusable form template that defines the watermark appearance.
        FormTemplate template = new FormTemplate();
        template.setBounds(pageBounds);

        // Create and configure the watermark text.
        TextFragment fragment = new TextFragment();
        fragment.setText(watermarkText);
        fragment.setLocation(new PointF(180, 120));
        fragment.setRotationAngle(45);
        fragment.setFont(new TextFont("Segoe UI"));
        fragment.setFontSize(72);
        fragment.setForegroundFill(new SolidFill(PdfColor.getRed(), .5));

        // Add the text fragment to the template.
        template.getFragments().add(fragment);

        // Create a watermark annotation.
        WatermarkAnnotation watermark =  new WatermarkAnnotation(pageBounds);

        // Create the annotation appearance from the template.
        AnnotationAppearance appearance = new AnnotationAppearance(template);

        // Assign the normal appearance to the annotation.
        AnnotationAppearances appearances = new AnnotationAppearances();
        appearances.setNormal(appearance);

        watermark.setAppearance(appearances);

        // Add the watermark annotation to the page.
        page.getAnnotations().add(watermark);
    }
}

Refer to the following help topic for additional information: Watermark Annotations.

Inheritance

Object
JavaObject
BaseAnnotation
WatermarkAnnotation

Constructors

WatermarkAnnotation(RectangleF bounds, InputStream documentStream, int pageIndex) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds, document stream, and page index.

Declaration

public WatermarkAnnotation(RectangleF bounds, InputStream documentStream, int pageIndex)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

documentStream InputStream

The document stream that contains the page used as the watermark appearance.

pageIndex int

The zero-based page index.

WatermarkAnnotation(RectangleF bounds, InputStream documentStream) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds and document stream.

Declaration

public WatermarkAnnotation(RectangleF bounds, InputStream documentStream)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

documentStream InputStream

The document stream that contains the page used as the watermark appearance.

WatermarkAnnotation(RectangleF bounds, Page page) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds and document page.

Declaration

public WatermarkAnnotation(RectangleF bounds, Page page)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

page Page

The page used as the watermark appearance.

WatermarkAnnotation(RectangleF bounds, PdfDocument document, int pageIndex) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds, PDF document, and page index.

Declaration

public WatermarkAnnotation(RectangleF bounds, PdfDocument document, int pageIndex)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

document PdfDocument

The PDF document that contains the page used as the watermark appearance.

pageIndex int

The zero-based page index.

WatermarkAnnotation(RectangleF bounds, PdfDocument document) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds and PDF document.

Declaration

public WatermarkAnnotation(RectangleF bounds, PdfDocument document)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

document PdfDocument

The document that contains the page used as the watermark appearance.

WatermarkAnnotation(RectangleF bounds, ReadableByteChannel documentStream, int pageIndex) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds, readable byte channel, and page index.

Declaration

public WatermarkAnnotation(RectangleF bounds, ReadableByteChannel documentStream, int pageIndex)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

documentStream ReadableByteChannel

The readable byte channel that contains the page used as the watermark appearance.

pageIndex int

The zero-based page index.

WatermarkAnnotation(RectangleF bounds, ReadableByteChannel documentStream) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds and readable byte channel.

Declaration

public WatermarkAnnotation(RectangleF bounds, ReadableByteChannel documentStream)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

documentStream ReadableByteChannel

The readable byte channel that contains the page used as the watermark appearance.

WatermarkAnnotation(RectangleF bounds, VisualFragment fragment) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds and visual fragment.

Declaration

public WatermarkAnnotation(RectangleF bounds, VisualFragment fragment)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

fragment VisualFragment

The visual fragment that defines the watermark appearance.

WatermarkAnnotation(RectangleF bounds) Constructor

Initializes a new instance of the WatermarkAnnotation class with the specified bounds.

Declaration

public WatermarkAnnotation(RectangleF bounds)

Parameters

Name Type Description
bounds RectangleF

The annotation bounds.

Methods

getHorizontalTranslationPercent() Method

Returns the horizontal offset of the watermark annotation.

Declaration

public double getHorizontalTranslationPercent()

Returns

Type Description
double

The horizontal offset of the watermark annotation, as a percentage.

getVerticalTranslationPercent() Method

Returns the vertical offset of the watermark annotation.

Declaration

public double getVerticalTranslationPercent()

Returns

Type Description
double

The vertical translation percentage, as a percentage.

setAppearance(InputStream stream, int pageIndex) Method

Sets the watermark annotation appearance from a document stream page.

Declaration

public void setAppearance(InputStream stream, int pageIndex)

Parameters

Name Type Description
stream InputStream

The document stream that contains the page used as the watermark appearance.

pageIndex int

The zero-based page index.

setAppearance(Page page) Method

Sets the watermark annotation appearance from the specified page.

Declaration

public void setAppearance(Page page)

Parameters

Name Type Description
page Page

The page used as the watermark appearance.

setAppearance(PdfDocument document, int pageIndex) Method

Sets the watermark annotation appearance from a document page.

Declaration

public void setAppearance(PdfDocument document, int pageIndex)

Parameters

Name Type Description
document PdfDocument

The document that contains the page used as the watermark appearance.

pageIndex int

The zero-based page index.

setAppearance(ReadableByteChannel stream, int pageIndex) Method

Sets the watermark annotation appearance from a readable byte channel page.

Declaration

public void setAppearance(ReadableByteChannel stream, int pageIndex)

Parameters

Name Type Description
stream ReadableByteChannel

The readable byte channel that contains the page used as the watermark appearance.

pageIndex int

The zero-based page index.

setAppearance(VisualFragment fragment) Method

Sets the watermark annotation appearance based on a visual fragment.

Declaration

public void setAppearance(VisualFragment fragment)

Parameters

Name Type Description
fragment VisualFragment

The visual fragment that defines the watermark appearance.

setHorizontalTranslationPercent(double value) Method

Sets the horizontal offset of the watermark annotation.

Declaration

public void setHorizontalTranslationPercent(double value)

Parameters

Name Type Description
value double

The horizontal offset of the watermark annotation, as a percentage.

setVerticalTranslationPercent(double value) Method

Sets the vertical offset of the watermark annotation.

Declaration

public void setVerticalTranslationPercent(double value)

Parameters

Name Type Description
value double

The vertical translation percentage, as a percentage.