This topic describes:
· The default behavior of Aspose.Words when searching for TrueType fonts. This behavior depends on the operating system.
· How to explicitly specify TrueType font folders for Aspose.Words.
Aspose.Words looks for fonts in the %windir%\Fonts folder. This setting will work for you most of the time. You only need to specify your own fonts folders if you need to.
Different Linux distributions store fonts in different folders. Aspose.Words looks for fonts in several well-known locations.
By default, Aspose.Words looks for the fonts in the following locations:
· /usr/share/fonts
· /usr/local/share/fonts
· /usr/X11R6/lib/X11/fonts
This default behavior will work for most Linux distributions, but not guaranteed to work all of the time. You might need to specify the location of true type fonts explicitly. To do this, you need to know where TrueType fonts are installed on your Linux distribution.
Aspose.Words looks for fonts in the /Library/Fonts folder which is the standard location for TrueType fonts on Mac OS X. This setting will work for you most of the time. You only need to specify your own fonts folders if you need to.
The FontSettings.SetFontsFolder method is used to indicate where Aspose.Words should look for fonts. When a valid path is passed to this method, Aspose.Words no longer looks in the registry or the Windows\Font folder to look for fonts and only scans for fonts within the specified folder.
When a custom font folder is set using this method, the folder is checked that it exists and is accessible. If the folder cannot be found an exception is thrown.
Example
Demonstrates how to set the folder Aspose.Words uses to look for TrueType fonts during rendering or embedding of fonts.
[Java]
Document doc = new Document(getMyDir() + "Rendering.doc");
// Note that this setting will override any default font sources that are being searched by default. Now only these folders will be searched for
// fonts when rendering or embedding fonts. To add an extra font source while keeping system font sources then use both FontSettings.GetFontSources and
// FontSettings.SetFontSources instead.
FontSettings.setFontsFolder("C:\\MyFonts\\", false);
doc.save(getMyDir() + "Rendering.SetFontsFolder Out.pdf");
The FontSettings class also provides a method to specify multiple folders which are scanned for fonts during rendering. Using the FontSettings.SetFontsFolders method, an array of folder paths can be specified. An extra boolean parameter controls if fonts are scanned recursively through all folders, that is, all child folders of the specified folders are scanned.
Example
Demonstrates how to set Aspose.Words to look in multiple folders for TrueType fonts when rendering or embedding fonts.
[Java]
Document doc = new Document(getMyDir() + "Rendering.doc");
// Note that this setting will override any default font sources that are being searched by default. Now only these folders will be searched for
// fonts when rendering or embedding fonts. To add an extra font source while keeping system font sources then use both FontSettings.GetFontSources and
// FontSettings.SetFontSources instead.
FontSettings.setFontsFolders(new String[] {"C:\\MyFonts\\", "D:\\Misc\\Fonts\\"}, true);
doc.save(getMyDir() + "Rendering.SetFontsFolders Out.pdf");
It is common to want to specify fonts to be read from both the default system folders (e.g Windows\Font directory on a Window’s machine) and from a folder filled with your own fonts. A combination of the FontSettings.GetFontsFolders and FontSettings.SetFontsFolders method can also be used to achieve this.
The steps to achieve this involve:
1. Retrieving an array of the default font folders using the FontSettings.GetFontsFolders method.
2. Appending your custom folder onto this list.
3. Pass the new array of font folders to the FontSettings.SetFontsFolders for changes to take effect.
Example
Demonstrates how to set Aspose.Words to look for TrueType fonts in system folders as well as a custom defined folder when scanning for fonts.
[Java]
Document doc = new Document(getMyDir() + "Rendering.doc");
// Retrieve the array of environment-dependent font sources that are searched by default. For example this will contain a "Windows\Fonts\" source on a Windows machines.
// We add this array to a new ArrayList to make adding or removing font entries much easier.
ArrayList fontSources = new ArrayList(Arrays.asList(FontSettings.getFontsSources()));
// Add a new folder source which will instruct Aspose.Words to search the following folder for fonts.
FolderFontSource folderFontSource = new FolderFontSource("C:\\MyFonts\\", true);
// Add the custom folder which contains our fonts to the list of existing font sources.
fontSources.add(folderFontSource);
// Convert the Arraylist of source back into a primitive array of FontSource objects.
FontSourceBase[] updatedFontSources = (FontSourceBase[])fontSources.toArray(new FontSourceBase[fontSources.size()]);
// Apply the new set of font sources to use.
FontSettings.setFontsSources(updatedFontSources);
doc.save(getMyDir() + "Rendering.SetFontsFolders Out.pdf");