java.lang.ObjectFontSubstitutionRule
com.aspose.words.DefaultFontSubstitutionRule
public class DefaultFontSubstitutionRule
Example:
// Create a blank document and a new FontSettings property
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Get the default substitution rule within FontSettings, which will be enabled by default and will substitute all missing fonts with "Times New Roman"
DefaultFontSubstitutionRule defaultRule = fontSettings.getSubstitutionSettings().getDefaultFontSubstitution();
Assert.assertTrue(defaultRule.getEnabled());
Assert.assertEquals(defaultRule.getDefaultFontName(), "Times New Roman");
// Set the default font substitute to "Courier New"
defaultRule.setDefaultFontName("Courier New");
// Using a document builder, add some text in a font that we don't have to see the substitution take place,
// and render the result in a PDF
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Missing Font");
builder.writeln("Line written in a missing font, which will be substituted with Courier New.");
doc.save(getArtifactsDir() + "Font.DefaultFontSubstitutionRule.pdf");
Property Getters/Setters Summary | ||
---|---|---|
java.lang.String | getDefaultFontName() | |
void | setDefaultFontName(java.lang.String value) | |
Gets or sets the default font name. | ||
boolean | getEnabled() | → inherited from FontSubstitutionRule |
void | setEnabled(boolean value) | |
Specifies whether the rule is enabled or not. |
Property Getters/Setters Detail |
---|
getDefaultFontName/setDefaultFontName | |
public java.lang.String getDefaultFontName() / public void setDefaultFontName(java.lang.String value) |
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().getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial Unicode MS"); // Now the set default font is used in place of any missing fonts during any rendering calls. doc.save(getArtifactsDir() + "Rendering.SetDefaultFont.pdf"); doc.save(getArtifactsDir() + "Rendering.SetDefaultFont.xps");
Example:
Shows how to set the default font substitution rule.// Create a blank document and a new FontSettings property Document doc = new Document(); FontSettings fontSettings = new FontSettings(); doc.setFontSettings(fontSettings); // Get the default substitution rule within FontSettings, which will be enabled by default and will substitute all missing fonts with "Times New Roman" DefaultFontSubstitutionRule defaultRule = fontSettings.getSubstitutionSettings().getDefaultFontSubstitution(); Assert.assertTrue(defaultRule.getEnabled()); Assert.assertEquals(defaultRule.getDefaultFontName(), "Times New Roman"); // Set the default font substitute to "Courier New" defaultRule.setDefaultFontName("Courier New"); // Using a document builder, add some text in a font that we don't have to see the substitution take place, // and render the result in a PDF DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setName("Missing Font"); builder.writeln("Line written in a missing font, which will be substituted with Courier New."); doc.save(getArtifactsDir() + "Font.DefaultFontSubstitutionRule.pdf");
getEnabled/setEnabled | → inherited from FontSubstitutionRule |
public boolean getEnabled() / public void setEnabled(boolean value) |
Example:
Shows OS-dependent font config substitution.// Create a new FontSettings object and get its font config substitution rule FontSettings fontSettings = new FontSettings(); FontConfigSubstitutionRule configSubstitution = fontSettings.getSubstitutionSettings().getFontConfigSubstitution(); // The FontConfigSubstitutionRule object works differently on Windows/non-Windows platforms final String OS = System.getProperty("os.name").toLowerCase(); // On Windows, it is unavailable if (OS == "win") { Assert.assertFalse(configSubstitution.getEnabled()); Assert.assertFalse(configSubstitution.isFontConfigAvailable()); } // On Linux/Mac, we will have access and will be able to perform operations if (OS == "nix") { Assert.assertTrue(configSubstitution.getEnabled()); Assert.assertTrue(configSubstitution.isFontConfigAvailable()); configSubstitution.resetCache(); }
Example:
Shows how to access a document's system font source and set font substitutes.Document doc = new Document(); // Create a font settings object for our document doc.setFontSettings(new FontSettings()); // By default we always start with a system font source Assert.assertEquals(doc.getFontSettings().getFontsSources().length, 1); SystemFontSource systemFontSource = (SystemFontSource) doc.getFontSettings().getFontsSources()[0]; Assert.assertEquals(systemFontSource.getType(), FontSourceType.SYSTEM_FONTS); Assert.assertEquals(systemFontSource.getPriority(), 0); if (System.getProperty("os.name").startsWith("Windows")) { Assert.assertEquals(SystemFontSource.getSystemFontFolders(), new String[]{"C:\\WINDOWS\\Fonts"}); } for (String systemFontFolder : SystemFontSource.getSystemFontFolders()) { System.out.println(systemFontFolder); } // Set a font that exists in the windows fonts directory as a substitute for one that doesn't doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true); doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes("Kreon-Regular", new String[]{"Calibri"}); long substituteSize = StreamSupport.stream(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular").spliterator(), false).count(); Assert.assertEquals(substituteSize, 1); Assert.assertTrue(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular").toString().contains("Calibri")); // Alternatively, we could add a folder font source in which the corresponding folder contains the font FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "MyFonts", false); doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource}); Assert.assertEquals(doc.getFontSettings().getFontsSources().length, 2); // Resetting the font sources still leaves us with the system font source as well as our substitutes doc.getFontSettings().resetFontSources(); Assert.assertEquals(doc.getFontSettings().getFontsSources().length, 1); Assert.assertEquals(doc.getFontSettings().getFontsSources()[0].getType(), FontSourceType.SYSTEM_FONTS); substituteSize = StreamSupport.stream(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular").spliterator(), false).count(); Assert.assertEquals(substituteSize, 1);