HarmonyOS鸿蒙Next中使用addTextObject在pdf文件中添加文本时,可以使用自定义字体吗

HarmonyOS鸿蒙Next中使用addTextObject在pdf文件中添加文本时,可以使用自定义字体吗

let textStyle = new pdfService.TextStyle(); let fontInfo = new pdfService.FontInfo(); fontInfo.fontPath = “/font/XXX.ttf” //此处是否可以使用自定义字体的路径,路径有什么要求 textStyle.fontInfo = fontInfo; textStyle.textSize = 11; textStyle.textColor = 234; textStyle.isBold = true; textStyle.isItalic = false; textStyle.isUnderline = true; let textObj = pdfPage.addTextObject(‘a’, 20, 20, textStyle);


更多关于HarmonyOS鸿蒙Next中使用addTextObject在pdf文件中添加文本时,可以使用自定义字体吗的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

你可以参考一下示例demo:

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

let tempFilePath = '/data/storage/el2/base/temp/test.pdf';
let pdfDocument = new pdfService.PdfDocument();
let loadResult = pdfDocument.loadDocument(tempFilePath, '');
if(pdfService.ParseResult.PARSE_SUCCESS === loadResult) {
  let pdfPage: pdfService.PdfPage = pdfDocument.getPage(0);
  let textStyle = new pdfService.TextStyle();
  let fontInfo = new pdfService.FontInfo();
  fontInfo.fontPath = "/system/fonts/HarmonyOS_Sans_SC_Black.ttf"
  textStyle.fontInfo = fontInfo;
  textStyle.textSize = 11;
  textStyle.textColor = 234;
  textStyle.isBold = true;
  textStyle.isItalic = false;
  textStyle.isUnderline = true;
  let textObj = pdfPage.addTextObject('a', 20, 20, textStyle);
}

更多关于HarmonyOS鸿蒙Next中使用addTextObject在pdf文件中添加文本时,可以使用自定义字体吗的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


用系统字体可以添加,但是自定义字体添加失败。

有报错信息吗,

我这里测试的任何字体展示的都是 宋体, 从font.getSystemFontList()看到的结果只有一个 “书宋-简”, 不知道是不是个系统bug,

在HarmonyOS鸿蒙Next中使用addTextObject在PDF文件中添加文本时,可以支持自定义字体。鸿蒙Next的PDF处理能力允许开发者通过addTextObject方法将文本添加到PDF文件中,并且可以通过指定字体文件路径或使用系统已注册的字体来实现自定义字体的应用。开发者需要确保字体文件格式支持(如TTF、OTF等),并在代码中正确引用字体文件路径或字体名称。

在HarmonyOS鸿蒙Next中,使用addTextObject在PDF文件中添加文本时,支持自定义字体。可以通过Font类加载自定义字体文件(如TTF或OTF格式),然后在调用addTextObject时指定该字体。确保字体文件路径正确,并且字体文件格式兼容。这样即可实现使用自定义字体在PDF中添加文本。

回到顶部