com.aspose.words

Class FontSettings

  • java.lang.Object
    • com.aspose.words.FontSettings
public class FontSettings 
extends java.lang.Object

Specifies font settings for a document.

Aspose.Words uses font settings to resolve the fonts in the document. Fonts are resolved mostly when building document layout or rendering to fixed page formats. But when loading some formats, Aspose.Words also may require to resolve the fonts. For example, when loading HTML documents Aspose.Words may resolve the fonts to perform font fallback. So it is recommended that you set the font settings in LoadOptions when loading the document. Or at least before building the layout or rendering the document to the fixed-page format.

By default all documents uses single static font settings instance. It could be accessed by DefaultInstance property.

Changing font settings is safe at any time from any thread. But it is recommended that you do not change the font settings while processing some documents which uses this settings. This can lead to the fact that the same font will be resolved differently in different parts of the document.

Example:

Demonstrates how to set the folder Aspose.Words uses to look for TrueType fonts during rendering or embedding of fonts.
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.getDefaultInstance().setFontsFolder("C:\\MyFonts\\", false);

doc.save(getMyDir() + "\\Artifacts\\Rendering.SetFontsFolder.pdf");

Example:

Demonstrates how to set Aspose.Words to look in multiple folders for TrueType fonts when rendering or embedding fonts.
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.getDefaultInstance().setFontsFolders(new String[] { "C:\\MyFonts\\", "D:\\Misc\\Fonts\\" }, true);

doc.save(getMyDir() + "\\Artifacts\\Rendering.SetFontsFolders.pdf");

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.
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.getDefaultInstance().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.getDefaultInstance().setFontsSources(updatedFontSources);

doc.save(getMyDir() + "\\Artifacts\\Rendering.SetFontsFolders.pdf");

Constructor Summary
 
Property Getters/Setters Summary
java.lang.StringgetDefaultFontName()
void
setDefaultFontName(java.lang.Stringvalue)
           Gets or sets the default font name.
static FontSettingsgetDefaultInstance()
Static default font settings.
booleangetEnableFontSubstitution()
void
           Specifies whether to enable or disable font substitution.
FontFallbackSettingsgetFallbackSettings()
Settings related to font fallback mechanism.
 
Method Summary
voidaddFontSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames)
Adds substitute (alternative) font names for given original font name.
FontSourceBase[]getFontsSources()
Gets a copy of the array that contains the list of sources where Aspose.Words looks for TrueType fonts.
java.lang.String[]getFontSubstitutes(java.lang.String originalFontName)
Returns array containing alternative font names to be used if original font is not presented in system.
voidresetFontSources()
Resets the fonts sources to the system default.
voidsetFontsFolder(java.lang.String fontFolder, boolean recursive)
Sets the folder where Aspose.Words looks for TrueType fonts when rendering documents or embedding fonts. This is a shortcut to setFontsFolders(java.lang.String[],boolean) for setting only one font directory.
voidsetFontsFolders(java.lang.String[] fontsFolders, boolean recursive)
Sets the folders where Aspose.Words looks for TrueType fonts when rendering documents or embedding fonts.
voidsetFontsSources(FontSourceBase[] sources)
Sets the sources where Aspose.Words looks for TrueType fonts when rendering documents or embedding fonts.
voidsetFontSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames)
Override substitute (alternative) font names for given original font name.
 

    • Constructor Detail

      • FontSettings

        public FontSettings()
    • Property Getters/Setters Detail

      • getDefaultFontName/setDefaultFontName

        public java.lang.String getDefaultFontName() / public void setDefaultFontName(java.lang.String value)
        
        Gets or sets the default font name.

        The default font name is used when Aspose.Words can't find requested font in specified sources.

        The default value is 'Times New Roman'.

        Example:

        Demonstrates how to specify what font to substitute for a missing font during rendering.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // If the default font defined here cannot be found during rendering then the closest font on the machine is used instead.
        FontSettings.getDefaultInstance().setDefaultFontName("Arial Unicode MS");
        
        // Now the set default font is used in place of any missing fonts during any rendering calls.
        doc.save(getMyDir() + "\\Artifacts\\Rendering.SetDefaultFont.pdf");
        doc.save(getMyDir() + "\\Artifacts\\Rendering.SetDefaultFont.xps");
      • getDefaultInstance

        public static FontSettings getDefaultInstance()
        
        Static default font settings. This instance is used by default in a document unless Document.FontSettings is specified.
      • getEnableFontSubstitution/setEnableFontSubstitution

        public boolean getEnableFontSubstitution() / public void setEnableFontSubstitution(boolean value)
        
        Specifies whether to enable or disable font substitution.

        If font substitution is enabled, Aspose.Words evaluates all the related fields in FontInfo (Panose, Sig etc) for the missing font and finds the closest match among the available font sources. Note that font substitution mechanism will override the DefaultFontName in cases when FontInfo for the missing font is available in the document.

        If font substitution is disabled, Aspose.Words uses the DefaultFontName for the substitution of missing fonts.

        The default value is true.

      • getFallbackSettings

        public FontFallbackSettings getFallbackSettings()
        
        Settings related to font fallback mechanism.
    • Method Detail

      • addFontSubstitutes

        public void addFontSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames)
        Adds substitute (alternative) font names for given original font name.
        Parameters:
        originalFontName - Original font name.
        substituteFontNames - List of alternative font names to be used if original font is not presented in system.
      • getFontsSources

        public FontSourceBase[] getFontsSources()
        Gets a copy of the array that contains the list of sources where Aspose.Words looks for TrueType fonts.

        The returned value is a copy of the data that Aspose.Words uses. If you change the entries in the returned array, it will have no effect on document rendering. To specify new font sources use the setFontsSources(com.aspose.words.FontSourceBase[]) method.

        Returns:
        A copy of the current font sources.

        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.
        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.getDefaultInstance().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.getDefaultInstance().setFontsSources(updatedFontSources);
        
        doc.save(getMyDir() + "\\Artifacts\\Rendering.SetFontsFolders.pdf");
      • getFontSubstitutes

        public java.lang.String[] getFontSubstitutes(java.lang.String originalFontName)
        Returns array containing alternative font names to be used if original font is not presented in system.
        Parameters:
        originalFontName - Original font name.
      • resetFontSources

        public void resetFontSources()
        Resets the fonts sources to the system default.
      • setFontsFolder

        public void setFontsFolder(java.lang.String fontFolder, boolean recursive)
        Sets the folder where Aspose.Words looks for TrueType fonts when rendering documents or embedding fonts. This is a shortcut to setFontsFolders(java.lang.String[],boolean) for setting only one font directory.
        Parameters:
        fontFolder - The folder that contains TrueType fonts.
        recursive - True to scan the specified folders for fonts recursively.

        Example:

        Demonstrates how to set the folder Aspose.Words uses to look for TrueType fonts during rendering or embedding of fonts.
        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.getDefaultInstance().setFontsFolder("C:\\MyFonts\\", false);
        
        doc.save(getMyDir() + "\\Artifacts\\Rendering.SetFontsFolder.pdf");
      • setFontsFolders

        public void setFontsFolders(java.lang.String[] fontsFolders, boolean recursive)
        Sets the folders where Aspose.Words looks for TrueType fonts when rendering documents or embedding fonts.

        By default, Aspose.Words looks for fonts installed to the system.

        Setting this property resets the cache of all previously loaded fonts.

        Parameters:
        fontsFolders - An array of folders that contain TrueType fonts.
        recursive - True to scan the specified folders for fonts recursively.

        Example:

        Demonstrates how to set Aspose.Words to look in multiple folders for TrueType fonts when rendering or embedding fonts.
        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.getDefaultInstance().setFontsFolders(new String[] { "C:\\MyFonts\\", "D:\\Misc\\Fonts\\" }, true);
        
        doc.save(getMyDir() + "\\Artifacts\\Rendering.SetFontsFolders.pdf");
      • setFontsSources

        public void setFontsSources(FontSourceBase[] sources)
        Sets the sources where Aspose.Words looks for TrueType fonts when rendering documents or embedding fonts.

        By default, Aspose.Words looks for fonts installed to the system.

        Setting this property resets the cache of all previously loaded fonts.

        Parameters:
        sources - An array of sources that contain TrueType fonts.

        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.
        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.getDefaultInstance().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.getDefaultInstance().setFontsSources(updatedFontSources);
        
        doc.save(getMyDir() + "\\Artifacts\\Rendering.SetFontsFolders.pdf");
      • setFontSubstitutes

        public void setFontSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames)
        Override substitute (alternative) font names for given original font name.
        Parameters:
        originalFontName - Original font name.
        substituteFontNames - List of alternative font names to be used if original font is not presented in system.

        Example:

        Shows how to define alternative fonts if original does not exist
        FontSettings fontSettings = new FontSettings();
        fontSettings.setFontSubstitutes("Times New Roman","Slab", "Arvo" );