Install True Type Fonts on Linux & Convert Word DOCX ODT Documents to PDF File Format using Java | Insert HTML

Aspose Version: 16.5.0

We are inserting text via documentbuiler - the font is "Times New Roman".
However, if the fonts are not installed (e.g on Linux), Times New Roman is used all the time.

On my linux system where "Times New Roman" is not installed, "getFont" will also return "Times new Roman".

Now we are saving the document as a pdf, now the font is "Liberation Sans".

In the UnitTest below we are using pdfbox to show you the font.

Expected:
- the word / aspose document should also use the "Liberation Sans"-Font. Because "Times New Roman" is not installed.

@Test
public void TestPDF() throws Exception {
Document document = new Document();

DocumentBuilder documentBuilder = new DocumentBuilder(document);
documentBuilder.moveToDocumentEnd();
documentBuilder.write("Hello World");

Run runWrite = (Run) document.getChild(NodeType.RUN, 0, true);

String font = "Times New Roman";

Assert.assertEquals(runWrite.getFont().getName(), font);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

document.save(byteArrayOutputStream, SaveFormat.PDF);

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

PDDocument load = PDDocument.load(byteArrayInputStream);

List pages = load.getDocumentCatalog().getAllPages();

for (PDPage page : pages) {
Map pageFonts = page.getResources().getFonts();

for (String key : pageFonts.keySet()) {
Assert.assertTrue(pageFonts.get(key).getBaseFont().contains(font), "Did not find the font '" + font+ "' in the document.");
}
}
}

Output:
java.lang.AssertionError: Did not find the font 'Times New Roman' in the document. expected [true] but found [false]
Expected :true
Actual :false

Hi Alexander,

Thanks for your inquiry. Aspose.Words only uses these fonts only when rendering document from memory/file to fixed-page formats such as PDF, XPS, images etc. Please refer to the following article:

Font substitution and Font fallback in Aspose.Words

In Aspose.Words there are two different mechanisms - font substitution and font fallback. Font substitution is used when font specified in the document couldn’t be found among the font sources. It is described in above section.Font fallback is used when font is resolved but it doesn’t contain a specific character. In this case Aspose.Words tries to use one of the fallback fonts for the character. The list of fallback fonts is predefined for each Unicode range. Currently for number of ranges Aspose.Words uses only “Arial Unicode MS” font. If there are no fallback fonts available then character is rendered as a missing glyph from the original font.

Best regards,

Hi Awais,


thanks for the link, good article. Now I know what to do.

Greetings

Hi Awais,


I was working on this issue again. We want to have your “Gentium” Font as default font. Is there any best pratices to this?

We do something like this:

FontSettings fontSettings = FontSettings.getDefaultInstance();
fontsettings.setDefaultFontName(“Gentium Basic”);

asposeDocument.setFontSettings(fontSettings);

DocumentBuilder documentBuilder = new DocumentBuilder(asposeDocument);

documentBuilder.insertHTML(html-String, true);

But Linux and Windows are using not the Gentium Font, because it is not in the font-directory. What do we have to do, to get the font as default?

Thanks for your help

Hi Alexander,


Thanks for your inquiry. If the default font defined via FontSettings.DefaultFontName Property cannot be found during rendering, then the closest font on the machine is used instead. Please install “Gentium Basic” font or copy it in your font directory. Hope, this helps.

Best regards,

Hi Awais,


thanks for your answer. I was hoping, that we can access the font via aspose because the sheet says it’s embedded. But okay, than we will solve the problem differently.

Thanks

Hi Alexander,


In case you have further inquiries or need any help about Aspose.Words , please let us know.

Best regards,