将自定义字体设置为单词 java 不起作用

Setting custom font to aspose words java does not work

我正在尝试设置自定义字体,但它不起作用。我在这里错过了什么?

/src/main/resources/fonts/fonts
  -> Helvetica
  -> OpenArrow


String path = Thread.currentThread().getContextClassLoader().getResource("fonts").getPath();
FontSettings.setFontsSources(
        new FontSourceBase[] {
                new FolderFontSource(path, true, 1),
                new SystemFontSource(0)
        }
);
FontSettings.setDefaultFontName("Helvetica");


Document outputDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(outputDoc);
builder.getFont().setName("Helvetica");

Helvetica 字体文件夹:

Aspose的话版本: 15.6.0

使用最新版本的 Aspose.Words,您应该使用 FontSettings.getDefaultInstance()。请看下面的代码示例:

FontSettings.getDefaultInstance().setFontsSources(
        new FontSourceBase[] {
            new FolderFontSource("C:\Temp\fonts", true, 1),
            new SystemFontSource(0)
        }
);

Document outputDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(outputDoc);
builder.getFont().setName("Helvetica");
builder.writeln("This is Helvetica text.");
outputDoc.save("C:\Temp\out.pdf");

关注link了解更多信息。 https://docs.aspose.com/words/java/specify-truetype-fonts-location/

此外,我还用 Aspose.Words 的旧 15.6.0 版本检查了以下代码,输出的 PDF 文档看起来是正确的。看截图。

FontSettings.setFontsSources(
        new FontSourceBase[] {
            new FolderFontSource("C:\Temp\fonts", true, 1),
            new SystemFontSource(0)
        }
);
FontSettings.setDefaultFontName("Helvetica");

Document outputDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(outputDoc);
builder.getFont().setName("Helvetica");
builder.writeln("This is Helvetica text.");
outputDoc.save("C:\Temp\out.pdf");