Skip to content

Watermarks ​

Foxit PDF SDK for Web supports adding watermarks to documents or pages.

Common APIs that write into the PDF:

  • PDFDoc.addWatermark()
  • PDFDoc.removeAllWatermarks()
  • PDFPage.addWatermark()

To show a watermark in the Viewer temporarily without writing to the PDF, use PDFDocRender.setWatermarkConfig() or PDFPageRender.setWatermarkConfig().

NOTE

Write APIs change the document; export the file to persist changes.

Viewer-only (render) watermarks ​

For display-only watermarks, configure at render level. To apply one watermark across the current document render:

javascript
const docRender = pdfViewer.getPDFDocRender();

docRender.setWatermarkConfig({
    type: 'text',
    content: 'Confidential',
    watermarkSettings: {
        position: 'Center',
        rotation: 45,
        opacity: 40
    },
    watermarkTextProperties: {
        font: 'Microsoft Yahei',
        fontSize: 36,
        color: '#999999',
        fontStyle: 'normal'
    }
});

await pdfViewer.redraw(true);

For a single page:

javascript
const pageRender = pdfViewer.getPDFPageRender(0);

pageRender.setWatermarkConfig({
    type: 'text',
    content: 'Draft',
    watermarkSettings: {
        position: 'TopRight',
        rotation: 0,
        opacity: 50
    },
    watermarkTextProperties: {
        fontSize: 18,
        color: '#ff0000'
    }
});

await pdfViewer.redraw(true);

Render-level watermarks also support images. Use type: 'image' and pass a Data URL in content.

javascript
pageRender.setWatermarkConfig({
    type: 'image',
    content: imageDataURL,
    watermarkSettings: {
        position: 'Center',
        scaleX: 0.5,
        scaleY: 0.5,
        opacity: 60
    }
});

await pdfViewer.redraw(true);

Common options:

OptionDescription
typetext or image.
contentWatermark content: string for text, Data URL for image.
isMultilineTiled watermark.
rowSpace / columnSpaceRow and column spacing for tiled watermarks.
watermarkSettings.positionPosition, e.g. TopLeft, Center, BottomRight.
watermarkSettings.offsetX / offsetYOffset; ignored for tiled watermarks.
watermarkSettings.scaleX / scaleYHorizontal and vertical scale.
watermarkSettings.rotationRotation in degrees.
watermarkSettings.opacityOpacity 0–100.
watermarkTextProperties.fontFont name (CSS font family).
watermarkTextProperties.fontSizeFont size.
watermarkTextProperties.colorColor, e.g. #000000.
watermarkTextProperties.fontStylenormal or underline.
watermarkTextProperties.lineSpaceLine spacing for multi-line text.
watermarkTextProperties.alignmentleft, center, or right.

Document watermarks ​

PDFDoc.addWatermark(data) adds a watermark to a page range.

javascript
await pdfDoc.addWatermark({
    type: 'text',
    text: 'Confidential',
    pageStart: 0,
    pageEnd: 2,
    watermarkSettings: {
        position: 'Center',
        offsetX: 0,
        offsetY: 0,
        scale: 1,
        rotation: 45,
        opacity: 30
    },
    watermarkTextProperties: {
        fontSize: 36,
        color: 0x999999,
        fontStyle: 'normal'
    }
});

Common options:

OptionDescription
typetext or bitmap.
textText watermark content.
bitmapImage data, usually Uint8Array.
pageStart / pageEndPage range; index starts at 0.
watermarkSettings.positionPosition.
watermarkSettings.rotationRotation in degrees.
watermarkSettings.opacityOpacity 0–100.

Image watermarks ​

javascript
const imageBuffer = await fetch('/assets/watermark.png')
    .then(response => response.arrayBuffer())
    .then(buffer => new Uint8Array(buffer));

await pdfDoc.addWatermark({
    type: 'bitmap',
    bitmap: imageBuffer,
    pageStart: 0,
    pageEnd: pdfDoc.getPageCount() - 1,
    absScale: 60,
    useRelativeScale: false,
    watermarkSettings: {
        position: 'Center',
        rotation: 0,
        opacity: 40
    }
});

Page-level watermarks ​

For a single page, use PDFPage.addWatermark(data).

javascript
const page = await pdfDoc.getPageByIndex(0);

await page.addWatermark({
    type: 'text',
    text: 'Draft',
    watermarkSettings: {
        position: 'TopRight',
        rotation: 0,
        opacity: 50
    },
    watermarkTextProperties: {
        fontSize: 18,
        color: 0xff0000
    }
});

Remove document watermarks ​

javascript
await pdfDoc.removeAllWatermarks();

Save changes ​

After adding or removing watermarks, export the document.

javascript
const file = await pdfDoc.getFile({
    fileName: 'watermarked.pdf'
});

Notes ​

  • PDFDoc.addWatermark() and PDFPage.addWatermark() modify PDF content.
  • PDFDocRender.setWatermarkConfig() and PDFPageRender.setWatermarkConfig() affect Viewer display only; exported files from getFile() do not include render-only watermarks.
  • Render config uses type and content; PDF write APIs use type with text or bitmap—do not mix the two shapes.
  • After render-level changes, call pdfViewer.redraw(true) to refresh the view.
  • Text and image watermarks use different fields; confirm type before configuring.