Adds or deletes pages in a PDF document, including:
Adds one or more blank pages to a PDF document.
Deletes one or more specified pages from a PDF document.
Adds PDF pages from other documents to the current PDF document.
| API Name | Description |
|---|---|
| insertBlankPage(index: number, width: number, height: number): PdfPage | Inserts a blank PDF page at a specified position. |
| getPage(index: number): PdfPage | Obtains an object of the specified page. |
| insertPageFromDocument(document: PdfDocument, fromIndex: number, pageCount: number, index: number): PdfPage | Adds a page of another document to the current document. |
| deletePage(index: number, count: number): void | Deletes a specified PDF page. |
Call the loadDocument method to load a PDF document.
Call the getPage method to obtain the width and height of the current page.
Call the insertBlankPage and insertPageFromDocument methods to implement the following functions:
Call the deletePage method to delete one or more index pages.
import { pdfService } from '@kit.PDFKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// ...
@Entry
@Component
struct PageManagementPage {
private pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
private context = this.getUIContext().getHostContext() as Context;
aboutToAppear(): void {
// Ensure that the input.pdf document exists in the src/main/resources/resfile project directory.
let filePath = this.context.resourceDir + '/input.pdf';
this.pdfDocument.loadDocument(filePath);
}
build() {
Column() {
// ...
Button('insertBlankPage').onClick(async () => {
let page: pdfService.PdfPage = this.pdfDocument.getPage(0);
let page2: pdfService.PdfPage = this.pdfDocument.insertBlankPage(2, page.getWidth(), page.getHeight());
let outPdfPath = this.context.filesDir + '/testInsertBlankPage.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'PageManagementPage', 'insertBlankPage %{public}s!', result ? 'success' : 'fail');
})
// Inserts multiple blank pages.
Button('insertSomeBlankPage').onClick(async () => {
let page: pdfService.PdfPage = this.pdfDocument.getPage(0);
for (let i = 0; i < 3; i++) {
this.pdfDocument.insertBlankPage(2, page.getWidth(), page.getHeight());
}
let outPdfPath = this.context.filesDir + '/testInsertSomeBlankPage.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'PageManagementPage', 'insertSomeBlankPage %{public}s!', result ? 'success' : 'fail');
})
// Insert pages with indexes 1, 2, and 3 of the **input2.pdf** document to the page with index 0 of the **input.pdf** document and save the document as a new document.
Button('insertPageFromDocument').onClick(async () => {
let pdfDoc: pdfService.PdfDocument = new pdfService.PdfDocument();
// Ensure that the input2.pdf document exists in the src/main/resources/resfile project directory.
pdfDoc.loadDocument(this.context.resourceDir + '/input2.pdf');
this.pdfDocument.insertPageFromDocument(pdfDoc, 1, 3, 0);
let outPdfPath = this.context.filesDir + '/testInsertPageFromDocument.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'PageManagementPage', 'insertPageFromDocument %{public}s!', result ? 'success' : 'fail');
})
// Delete one or more index pages.
Button('deletePage').onClick(async () => {
this.pdfDocument.deletePage(2, 2);
let outPdfPath = this.context.filesDir + '/testDeletePage.pdf';
let result = this.pdfDocument.saveDocument(outPdfPath);
hilog.info(0x0000, 'PageManagementPage', 'deletePage %{public}s!', result ? 'success' : 'fail');
})
// ...
}
}
}