您好,使用Aspose.Pdf在给PDF添加水印时,linux在系统未安装字体时,请问有类似下边Aspose.words的方式,在Aspose.Pdf中用户自定义字体吗?
附Aspose.words拓展字体方式
ArrayList<com.aspose.words.FolderFontSource> wordsFontSources = new ArrayList(Arrays.asList(com.aspose.words.FontSettings.getDefaultInstance().getFontsSources()));
com.aspose.words.FolderFontSource wordsFolderFontSource = new com.aspose.words.FolderFontSource(fontFolder, true);
wordsFontSources.add(wordsFolderFontSource);
com.aspose.words.FontSourceBase[] updatedWordsFontSources = wordsFontSources.toArray(new com.aspose.words.FontSourceBase[wordsFontSources.size()]);
com.aspose.words.FontSettings.getDefaultInstance().setFontsSources(updatedWordsFontSources);
查Aspose.Pdf文档,用 FontRepository.addLocalFontPath(fontFolder) 在全局新增拓展字体发现在linux下并未生效,能麻烦share下这方面应用吗?谢谢
@IhsanYang
请注意,如果要使用某些自定义字体文件夹,则需要在字体文件夹列表中添加文件夹,如下所示。
String path = “E:/Data/Fonts/”;
List fontPaths = com.aspose.pdf.Document.getLocalFontPaths();
fontPaths.add(path);
com.aspose.pdf.Document.setLocalFontPaths(fontPaths);
但是,您也可以直接从文件路径使用字体,如下所示
textState.setFont(FontRepository.openFont(myDir +“ mangal.ttf”));
@asad.ali
谢谢。尝试了发现第二种可以正常work。您提到的第一种没在Document上发现getLocalFontPaths, 所以我使用了:
List<String> localFontPaths = FontRepository.getLocalFontPaths();
localFontPaths.add(fontFolder);
FontRepository.setLocalFontPaths(localFontPaths);
FontRepository.loadFonts();
log.info("extendPDFFont finished !");
但是这种方式在初始化之后,在使用的时候FontRepository.findFont()依然会报字体无法找到
@IhsanYang
您能否将您的源PDF文档以及您要处理的完整代码段共享给您。我们将在我们的环境中测试该场景并相应地解决它。
@asad.ali
谢谢,下边附源码。在未安装中文的linux机器上测试时,中文的水印字体依然会是乱码。
public class TestAsposePDFFontInit {
public static void main(String[] args) throws Exception {
TestAsposePDFFontInit init = new TestAsposePDFFontInit();
String fontFolder = CommonUtil.getCurrentDirPath() + File.separator + "font" + File.separator;
System.out.println("=======fontFolder:==========" + fontFolder);
ClassPathResource cpr = new ClassPathResource("aspose.lic");
byte[] bytes = CommonUtil.decryptFile(cpr);
init.setPdfLic(bytes);
init.extendPDFFont(fontFolder);
init.watermark();
}
private void setPdfLic(byte[] bytes) throws Exception {
com.aspose.pdf.License pdfLic = new com.aspose.pdf.License();
pdfLic.setLicense(new ByteArrayInputStream(bytes));
System.out.println("setPdfLic finished !");
}
private void extendPDFFont(String fontFolder) {
//Aspose PDF
List<String> localFontPaths = FontRepository.getLocalFontPaths();
localFontPaths.add(fontFolder);
for (String path : localFontPaths) {
System.out.println("---------------------" + path);
}
FontRepository.setLocalFontPaths(localFontPaths);
FontRepository.loadFonts();
System.out.println("extendPDFFont finished !");
}
private void watermark() {
com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document("src/test/resources/Files/test.pdf");
TextStamp textStamp = new TextStamp("中文测试");
for (int Page_counter = 1; Page_counter <= pdfDoc.getPages().size(); Page_counter++) {
Page page = pdfDoc.getPages().get_Item(Page_counter);
page.addStamp(textStamp);
}
}
}
@IhsanYang
您可以尝试使用 OpenFont
方法而不是 FindFont
方法。
Font f = FontRepository.OpenFont(name);
然后与我们分享您的反馈意见。
@Farhan.Raza
谢谢。
如你所说,使用 FontRepository.OpenFont(“C:/xxxx/simhei.ttf”)直接打开字体文件是可以正常工作的。
但是像我上边提到的先FontRepository.setLocalFontPaths(“C:/xxxx/”),再使用FontRepository.OpenFont(“simhei.ttf”)或者FontRepository.FindFont 显示无法找到simhei.ttf字体,可能我这种使用方法有错。
@IhsanYang
请注意,FontRepository.FindFont(字体名称)可能导致自定义字体出现问题。 因此,请使用FontRepository.OpenFont(字体路径)来避免此问题。 它需要完整的字体路径。