目录

Adding and Deleting Watermarks

You can add text or image watermarks to a specified page.

API Description

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.

Sample Code

[h2]Adding or Deleting a Text Watermark

Adding a text watermark

  1. Call the loadDocument method to load a PDF document.

  2. Instantiate the TextWatermarkInfo class and set related attributes, including the font, size, rotation, and position.

  3. Call the addWatermark method to add a text watermark.

  4. Save the PDF document to the app sandbox.

Deleting a text watermark

  1. Call the loadDocument method to load a PDF document.

  2. Call the removeWatermark method to delete a text watermark.

  3. 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();
        })
        // ...
        // ...
    }
  }
}

[h2]Adding or Deleting an Image Watermark

Adding an image watermark

  1. Call the loadDocument method to load a PDF document.

  2. Instantiate the ImageWatermarkInfo class and set related attributes, including scaling, rotation, opacity, and position.

  3. Call the addWatermark method to add an image watermark.

  4. Save the PDF document to the app sandbox.

Deleting an image watermark

  1. Call the loadDocument method to load a PDF document.

  2. Call the removeWatermark method to delete an image watermark.

  3. 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();
        })
        // ...
    }
  }
}