文档管理中心
我的

您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录

指南应用服务PDF Kit(PDF服务)pdfService能力打开和保存PDF文档

打开和保存PDF文档

对PDF文档添加内容、页眉页脚、水印、背景图片或书签等操作前,需要打开文档,并且在文档操作完成后,保存PDF文档。

pdfService和PdfView都可实现打开和保存文档,使用场景上有如下区别:

  • 需要对PDF文档做相关的编辑和操作,建议使用pdfService的能力打开和保存文档。

  • 需要预览、搜索关键字、监听PDF文档回调和批注等操作,推荐使用PdfView打开。

接口说明

展开
接口名 描述
loadDocument(path: string, password?: string, onProgress?: (progress: number) => number): ParseResult 加载指定文档路径。
saveDocument(path: string, onProgress?: (progress: number) => number): boolean 保存文档。

示例代码

  1. 调用loadDocument方法,加载PDF文档。

  2. 在【Save As】和【Save】两个按钮中调用saveDocument方法,分别实现了另存为PDF文档和保存覆盖源PDF文档的两种方式。

收起
自动换行
深色代码主题
复制
  1. import { pdfService } from '@kit.PDFKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. import { fileIo } from '@kit.CoreFileKit';
  4. import { BusinessError } from '@kit.BasicServicesKit';
  5. // ...
  6. @Entry
  7. @Component
  8. struct OpenDocument {
  9. private pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
  10. private context = this.getUIContext().getHostContext() as Context;
  11. private filePath = '';
  12. @State saveEnable: boolean = false;
  13. aboutToAppear(): void {
  14. this.filePath = this.context.filesDir + '/input.pdf';
  15. try {
  16. let res = fileIo.accessSync(this.filePath);
  17. if(!res) {
  18. // 确保在工程目录src/main/resources/resfile里有input.pdf文档
  19. let content: Uint8Array = this.context.resourceManager.getRawFileContentSync('resfile/input.pdf');
  20. let fdSand =
  21. fileIo.openSync(this.filePath, fileIo.OpenMode.WRITE_ONLY | fileIo.OpenMode.CREATE | fileIo.OpenMode.TRUNC);
  22. fileIo.writeSync(fdSand.fd, content.buffer);
  23. fileIo.closeSync(fdSand.fd);
  24. }
  25. this.pdfDocument.loadDocument(this.filePath);
  26. } catch (e) {
  27. let error: BusinessError = e as BusinessError;
  28. hilog.error(0x0000, 'OpenDocument', `Failed to loadDocument. Code: ${error.code}, message: ${error.message} `);
  29. }
  30. }
  31. build() {
  32. Column() {
  33. Row() {
  34. // ...
  35. Button('Save As').onClick(() => {
  36. let outPdfPath = this.context.filesDir + '/testSaveAsPdf.pdf';
  37. let result = this.pdfDocument.saveDocument(outPdfPath);
  38. this.saveEnable = true;
  39. hilog.info(0x0000, 'OpenSavePage', 'saveAsPdf %{public}s!', result ? 'success' : 'fail');
  40. })
  41. Button('Save').enabled(this.saveEnable).onClick(() => {
  42. // 这里可以对PDF文档添加内容、页眉页脚、水印、背景等一些内容,然后保存文档
  43. let tempDir = this.context.tempDir;
  44. let tempFilePath = tempDir + `/temp${Math.random()}.pdf`;
  45. try {
  46. fileIo.copyFileSync(this.filePath, tempFilePath);
  47. } catch (e) {
  48. let error: BusinessError = e as BusinessError;
  49. hilog.error(0x0000, 'OpenSavePage', `Failed to copyFileSync. Code: ${error.code}, message: ${error.message} `);
  50. }
  51. let pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
  52. // 加载临时文档
  53. let loadResult = pdfDocument.loadDocument(tempFilePath, '');
  54. if (loadResult === pdfService.ParseResult.PARSE_SUCCESS) {
  55. let result = pdfDocument.saveDocument(this.filePath);
  56. hilog.info(0x0000, 'OpenSavePage', 'savePdf %{public}s!', result ? 'success' : 'fail');
  57. }
  58. })
  59. }
  60. }
  61. }
  62. }