[HarmonyOS Next] PDF Kit使用 原创

第一小趴菜
发布于 2024-12-13 13:52
浏览
0收藏

PDF Kit(PDF服务)提供了打开PDF文件、页面添加批注、页眉页脚、水印、背景及书签等相关的功能,便于用户对PDF文档操作的更多的应用场景。其次提供了PdfView组件的文件预览功能,便于用户对PDF文档操作更多的预览应用场景,如:PDF文件保存、页面转化为图片、文档搜索等场景。

基础用法

当需要预览PDF文档或对PDF文档添加页眉页脚、水印、背景或书签,需要实现此功能。
1.导入模块

// 导入PDFKit 模块
import { pdfService } from '@kit.PDFKit';
  1. 加载PDF文档。
let filePath = String.raw`/data/storage/el2/base/haps/View/files/input.pdf`;  
let document: pdfService.PdfDocument = new pdfService.PdfDocument();
document.loadDocument(filePath);

格式转换

当需要把PDF文档转换为图片时,或将图片转换为PDF文档,可以分别使用convertToImage或addImageObject方法。

convertToImage

import { pdfService } from '@kit.PDFKit';
import { fileIo } from '@kit.CoreFileKit';

// 将测试文件上传至应用沙箱路径
let tempFilePath = '/data/storage/el2/base/temp/test.pdf';
let pdfDocument = new pdfService.PdfDocument();
// 加载临时文件
let loadResult = pdfDocument.loadDocument(tempFilePath);
let dir = getContext().filesDir + '/output/';
fileIo.mkdir(dir);
let result = pdfDocument.convertToImage(dir, 0);

addImageObject

import { pdfService } from '@kit.PDFKit';
import { common } from '@kit.AbilityKit';

let tempFilePath = '/data/storage/el2/base/temp/test.pdf';
let pdfDocument = new pdfService.PdfDocument();
let loadResult = pdfDocument.loadDocument(tempFilePath);
let pdfPage: pdfService.PdfPage = pdfDocument.getPage(1);
let context = getContext() as common.UIAbilityContext;
let dir = context.filesDir;
let imgPath = dir + "/img.jpg";
pdfPage.addImageObject(imgPath, 0, 0, 200, 200);
pdfDocument.saveDocument("/data/storage/el2/base/haps/entry/files/testAddImageToDocument.pdf");

添加页眉页脚

当需要在PDF文档的页眉页脚添加页码、时间及文本相关的信息时使用。

该方法属于耗时业务,需要遍历每一页去添加页眉页脚,添加页面较多时建议放到线程里去处理

// 导入PDFKit 模块
import { pdfService } from '@kit.PDFKit';
// 加载系统字体
import { font } from '@kit.ArkUI';
let filePath = String.raw`/data/storage/el2/base/haps/View/files/input.pdf`;  
let document: pdfService.PdfDocument = new pdfService.PdfDocument();
document.loadDocument(filePath);
let hfInfo: pdfService.HeaderFooterInfo = new pdfService.HeaderFooterInfo();
hfInfo.fontInfo = new pdfService.FontInfo();
hfInfo.fontInfo.fontPath = font.getFontByName("HarmonyOS Sans")?.path;  // 确保字体路径存在
hfInfo.fontInfo.fontName = '';
hfInfo.textSize = 10;
hfInfo.charset = pdfService.CharsetType.PDF_FONT_DEFAULT_CHARSET;
hfInfo.underline = false;
hfInfo.textColor = 0x00000000;
hfInfo.leftMargin = 1.0;
hfInfo.topMargin = 40.0;
hfInfo.rightMargin = 1.0;
hfInfo.bottomMargin = 40.0;
hfInfo.headerLeftText = "left H <<dd.mm.yyyy>> <<1/n>>";
hfInfo.headerCenterText = "center H <<m/d/yyyy>> <<1/n>>";
hfInfo.headerRightText = "right H <<m/d>><<1>>";
hfInfo.footerLeftText = "left F <<m/d>><<1>>";
hfInfo.footerCenterText = "center F <<m/d>><<1>>";
hfInfo.footerRightText = "right F <<dd.mm.yyyy>><<1>>";
document.addHeaderFooter(hfInfo, 1, 5, true, true);
document.saveDocument("/data/storage/el2/base/haps/View/files/output.pdf");

添加水印

通过索引指定PDF页面添加文本水印或图片水印,并对文本字体、大小、旋转,位置等相关设置,也可对图片水印的缩放、旋转、透明度和位置等设置。

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();
// 确保字体路径存在
wminfo.fontInfo.fontPath = font.getFontByName("HarmonyOS Sans").path; 
wminfo.opacity = 0.5;
wminfo.isOnTop = true;
wminfo.rotation = 45;
wminfo.scale = 1.5;
wminfo.opacity = 0.5;
wminfo.verticalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_TOP;
wminfo.horizontalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_LEFT;
wminfo.horizontalSpace = 1.0;
wminfo.verticalSpace = 1.0;
pdfDocument.addWatermark(wminfo, 0, 18, true, true);
pdfDocument.saveDocument("/data/storage/el2/base/haps/View/files/output.pdf");
// 添加水印
let wminfo: pdfService.ImageWatermarkInfo = new pdfService.ImageWatermarkInfo();
wminfo.watermarkType = pdfService.WatermarkType.WATERMARK_IMAGE;
wminfo.imagePath = "/data/storage/el2/base/haps/View/files/img.jpg";
wminfo.opacity = 0.5;
wminfo.isOnTop = true;
wminfo.rotation = 45;
wminfo.scale = 1.5;
wminfo.opacity = 0.5;
wminfo.verticalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_TOP;
wminfo.horizontalAlignment = pdfService.WatermarkAlignment.WATERMARK_ALIGNMENT_LEFT;
wminfo.horizontalSpace = 1.0;
wminfo.verticalSpace = 1.0;
pdfDocument.addWatermark(wminfo, 0, 18, true, true);
pdfDocument.saveDocument("/data/storage/el2/base/haps/View/files/output.pdf");

添加背景

通过索引指定PDF页面添加背景图片,并对图片大小缩放、旋转、透明度和位置等设置,支持图片格式PNG、BMP、JPEG,暂不支持文本或其他图片格式作为背景。
当需要在PDF文档的页面添加背景图片时使用。

该方法属于耗时业务,需要遍历每一页去添加背景,添加页面较多时建议放到线程里去处理。

// 导入PDFKit 模块
import { pdfService } from '@kit.PDFKit';
let filePath = String.raw`/data/storage/el2/base/haps/View/files/input.pdf`;  
let pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
pdfDocument.loadDocument(filePath);
let bginfo: pdfService.BackgroundInfo = new pdfService.BackgroundInfo();
bginfo.imagePath = "/data/storage/el2/base/haps/View/files/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;
pdfDocument.addBackground(bginfo, 2, 18, true, true);
pdfDocument.saveDocument("/data/storage/el2/base/haps/View/files/output.pdf");

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
收藏
回复
举报
回复
    相关推荐