CrazySabbath
CrazySabbath

Reputation: 1334

Setting custom font to aspose words java does not work

Im trying to set a custom font, but it does not work. What am I missing here?

/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 font folder: enter image description here

Aspose words version: 15.6.0

Upvotes: 0

Views: 2296

Answers (1)

Alexey Noskov
Alexey Noskov

Reputation: 1960

With the latest version of Aspose.Words, you should use FontSettings.getDefaultInstance(). Please see the following code example:

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");

Follow the link for more information. https://docs.aspose.com/words/java/specify-truetype-fonts-location/

Also I have checked the following code with the old 15.6.0 version of Aspose.Words and the output PDF document looks correct. See the screenshot.

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");

enter image description here

Upvotes: 1

Related Questions