You can add a background image or background color to a specified page and set attributes such as size, rotation, opacity, and position. The following image formats are supported: .png, .bmp, and .jpeg.

| API Name | Description |
|---|---|
| addBackground(info: BackgroundInfo, startIndex: number, endIndex: number, oddPages: boolean, evenPages: boolean): void | Adds a background to a PDF document. |
| removeBackground(): boolean | Removes the background of a PDF document. |
The addBackground method is time-consuming as it involves iterating through each page to add backgrounds. For documents with many pages, it is recommended that you process them in a thread.
Adding a background:
Call the loadDocument method to load a PDF document.
Instantiate the BackgroundInfo class and set related attributes, including size, rotation, opacity, and position.
Call the addBackground method to add a background.
Save the PDF document to the app sandbox.
Deleting a background:
Call the loadDocument method to load a PDF document.
Call the removeBackground method to remove the background.
Save the PDF document to the app sandbox.
import { pdfService } from '@kit.PDFKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// ...
@Entry
@Component
struct BackgroundPage {
private pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
private context = this.getUIContext().getHostContext() as Context;
build() {
Column() {
// ...
Button('addBackground').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 bginfo: pdfService.BackgroundInfo = new pdfService.BackgroundInfo();
// Ensure that the img.jpg file exists in the src/main/resources/resfile project directory.
bginfo.imagePath = this.context.resourceDir + '/img.jpg';
bginfo.backgroundColor = 50;
bginfo.isOnTop = true;
bginfo.rotation = 45;
bginfo.scale = 0.5;
bginfo.opacity = 0.3;
bginfo.verticalAlignment = pdfService.BackgroundAlignment.BACKGROUND_ALIGNMENT_TOP;
bginfo.horizontalAlignment = pdfService.BackgroundAlignment.BACKGROUND_ALIGNMENT_LEFT;
bginfo.horizontalSpace = 1.0;
bginfo.verticalSpace = 1.0;
this.pdfDocument.addBackground(bginfo, 0, 2, true, true);
let outPdfPath = this.context.filesDir + '/testAddBackground.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'BackgroundPage', 'addBackground %{public}s!', result ? 'success' : 'fail');
}
this.pdfDocument.releaseDocument();
})
Button('removeBackground').onClick(async () => {
let filePath = this.context.filesDir + '/testAddBackground.pdf';
let res = this.pdfDocument.loadDocument(filePath);
if (res === pdfService.ParseResult.PARSE_SUCCESS && this.pdfDocument.hasBackground()) {
let removeResult = this.pdfDocument.removeBackground();
if (removeResult) {
let outPdfPath = this.context.filesDir + '/removeBackground.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'BackgroundPage', 'removeBackground %{public}s!', result ? 'success' : 'fail');
}
}
this.pdfDocument.releaseDocument();
})
// ...
}
}
}