Tuesday 29 July 2014

Aspose.Words problem with fonts (encoding)

Summary
When using Aspose.Words in order to produce a pdf I had problem with some characters not renderer properly (e.g. ȗ) --  see the attached image where the created PDF does not render properly the character ȗ and it has embedded the Gentium fonts. The problem was that fonts were missing from my Operating System and the default fronts that Aspose.Words uses in this case don't support all the characters.



More Info
Aspose.Words when creates PDF uses True Type fonts. If it doesn't find True Type fonts it uses Gentium font; actually it embeds the fonts in the PDF (see the attached image). Gentium font doesn't support ȗ.

Therefore make sure that you have intalled on your Operating system True Type fonts. The algorithm that Aspose looks for fonts is the following (from here):
  1. Aspose.Words tries to find a font on the file system with an exact font name match.
  2. Next, Aspose.Words tries to find the required font among the fonts embedded in the original document. Some document formats such as DOCX can contain embedded fonts
  3. If Aspose.Words cannot find a font with the exact name match, it will attempt to to use the default font specified under FontSettings.DefaultFontName. If the user has not chosen their own default font then "Times New Roman" is the selected default font that is used. See the How to Set the Default Font used when Rendering topic for further information on setting default font.
  4. If Aspose.Words is unable to locate the font defined under FontSettings.DefaultFontName, it attempts to select the most suitable font from all of the available fonts.
  5. Finally, if Aspose.Words cannot find any fonts on the file system, it renders the document using the free Gentium font that is embedded into the Aspose.Words assembly.


Programmatically setting the fonts path has worked for me:


import com.aspose.words.FolderFontSource;


final String pathToFonts = "PATH TO TRUE TYPE FONTS";

FolderFontSource folderFontSource = new FolderFontSource(pathToFonts, true);
fontSources.add(folderFontSource);
FontSourceBase[] f
ontSourceBase = (FontSourceBase[])fontSources.toArray(new FontSourceBase[fontSources.size()]);
FontSettings.setFontsSources(
fontSourceBase);


Default font locations for various Operating Systems

Unix (Linux, Solaris)
/usr/share/fonts
/usr/local/share/fonts
/usr/X11R6/lib/X11/fonts 

Windows
Start | Run --> Fonts

No comments:

Post a Comment