You can add text or image watermarks to a specified page.
Set attributes such as font, size, rotation, and position of the text watermark.
Set attributes such as scaling, rotation, opacity, and position for image watermarks.

| API Name | Description |
|---|---|
| addWatermark(info: WatermarkInfo, startIndex: number, endIndex: number, oddPages: boolean, evenPages: boolean): void | Inserts a watermark to a PDF document. To insert an image, see ImageFormat for the supported image formats. There is no restriction on text characters. |
| removeWatermark(): boolean | Removes a watermark from a PDF document. |
The addWatermark method is time-consuming as it involves iterating through each page to add watermarks. For documents with many pages, it is recommended that you process them in a thread.
Adding a text watermark
Call the loadDocument method to load a PDF document.
Instantiate the TextWatermarkInfo class and set related attributes, including the font, size, rotation, and position.
Call the addWatermark method to add a text watermark.
Save the PDF document to the app sandbox.
Deleting a text watermark
Call the loadDocument method to load a PDF document.
Call the removeWatermark method to delete a text watermark.
Save the PDF document to the app sandbox.
import { pdfService } from '@kit.PDFKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { Font } from '@kit.ArkUI';
// ...
@Entry
@Component
struct WatermarkPage {
private pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
private context = this.getUIContext().getHostContext() as Context;
build() {
Column() {
// ...
Button('addTextWatermark').onClick(async () => {
// Ensure that the input.pdf document exists in the src/main/resources/resfile project directory.
let filePath = this.context.resourceDir + '/input.pdf';
let res = this.pdfDocument.loadDocument(filePath);
if (res === pdfService.ParseResult.PARSE_SUCCESS) {
let wminfo: pdfService.TextWatermarkInfo = new pdfService.TextWatermarkInfo();
wminfo.watermarkType = pdfService.WatermarkType.WATERMARK_TEXT;
wminfo.content = 'This is Watermark';
wminfo.textSize = 30;
wminfo.textColor = 200;
wminfo.fontInfo = new pdfService.FontInfo();
// Ensure that the font path exists.
let font: Font = new Font()
wminfo.fontInfo.fontPath = font.getFontByName('HarmonyOS Sans').path;
wminfo.opacity = 0.5;
wminfo.isOnTop = true;
wminfo.rotation = 45;
wminfo.scale = 1.5;
wminfo.verticalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_TOP;
wminfo.horizontalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_LEFT;
wminfo.horizontalSpace = 1.0;
wminfo.verticalSpace = 1.0;
this.pdfDocument.addWatermark(wminfo, 0, 5, true, true);
let outPdfPath = this.context.filesDir + '/testTextWatermark.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'WatermarkPage', 'addTextWatermark %{public}s!', result ? 'success' : 'fail');
}
this.pdfDocument.releaseDocument();
})
Button('removeTextWatermark').onClick(async () => {
let filePath = this.context.filesDir + '/testTextWatermark.pdf';
let res = this.pdfDocument.loadDocument(filePath);
if (res === pdfService.ParseResult.PARSE_SUCCESS && this.pdfDocument.hasWatermark()) {
let removeResult = this.pdfDocument.removeWatermark();
if (removeResult) {
let outPdfPath = this.context.filesDir + '/removeWatermark.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'WatermarkPage', 'removeWatermark %{public}s!', result ? 'success' : 'fail');
}
}
this.pdfDocument.releaseDocument();
})
// ...
// ...
}
}
} Adding an image watermark
Call the loadDocument method to load a PDF document.
Instantiate the ImageWatermarkInfo class and set related attributes, including scaling, rotation, opacity, and position.
Call the addWatermark method to add an image watermark.
Save the PDF document to the app sandbox.
Deleting an image watermark
Call the loadDocument method to load a PDF document.
Call the removeWatermark method to delete an image watermark.
Save the PDF document to the app sandbox.
import { pdfService } from '@kit.PDFKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { Font } from '@kit.ArkUI';
// ...
@Entry
@Component
struct WatermarkPage {
private pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
private context = this.getUIContext().getHostContext() as Context;
build() {
Column() {
// ...
Button('addImageWatermark').onClick(async () => {
let doc: pdfService.PdfDocument = new pdfService.PdfDocument();
let filePath: string = this.context.resourceDir + '/input.pdf';
let outPdfPath = this.context.filesDir + '/testImageWatermark.pdf';
doc.loadDocument(filePath);
let wminfo: pdfService.ImageWatermarkInfo = new pdfService.ImageWatermarkInfo();
wminfo.watermarkType = pdfService.WatermarkType.WATERMARK_IMAGE;
wminfo.imagePath = this.context.resourceDir + '/img.jpg';
wminfo.opacity = 1;
wminfo.isOnTop = true;
wminfo.rotation = 0;
wminfo.scale = 0.1;
wminfo.verticalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_HCENTER;
wminfo.horizontalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_VCENTER;
wminfo.horizontalSpace = 1.0;
wminfo.verticalSpace = 1.0;
doc.addWatermark(wminfo, 0, 18, true, true);
let result = doc.saveDocument(outPdfPath);
hilog.info(0x0000, 'WatermarkPage', 'addImageWatermark %{public}s!', result ? 'success' : 'fail');
doc.releaseDocument();
})
Button('removeImageWatermark').onClick(async () => {
let filePath = this.context.filesDir + '/testImageWatermark.pdf';
let res = this.pdfDocument.loadDocument(filePath);
if (res === pdfService.ParseResult.PARSE_SUCCESS && this.pdfDocument.hasWatermark()) {
let removeResult = this.pdfDocument.removeWatermark();
if (removeResult) {
let outPdfPath = this.context.filesDir + '/removeImageWatermark.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'WatermarkPage', 'removeImageWatermark %{public}s!', result ? 'success' : 'fail');
}
}
this.pdfDocument.releaseDocument();
})
// ...
}
}
}