配置字体以渲染电子表格
可能的使用场景
Aspose.Cells API 提供了在图像格式中渲染电子表格以及将其转换为 PDF 和 XPS 格式的功能。为了最大程度地保持转换的准确性,电子表格中使用的字体应该存储在操作系统的默认字体目录中。如果所需字体不可用,则 Aspose.Cells API 将尝试使用可用的字体来替代所需字体。
选择字体
以下是 Aspose.Cells API 在幕后执行的过程。
- API试图在文件系统中找到与电子表格中使用的确切字体名称匹配的字体。
- 如果API无法找到具有相同名称的精确字体,则尝试使用工作簿的DefaultStyle.Font属性下指定的默认字体。
- 如果API无法找到工作簿的DefaultStyle.Font属性下定义的字体,则尝试使用PdfSaveOptions.DefaultFont或ImageOrPrintOptions.DefaultFont属性下指定的字体。
- 如果API无法找到PdfSaveOptions.DefaultFont或ImageOrPrintOptions.DefaultFont属性下定义的字体,则尝试使用FontConfigs.DefaultFontName属性下指定的字体。
- 如果API无法找到FontConfigs.DefaultFontName属性下定义的字体,则尝试从所有可用字体中选择最合适的字体。
- 最后,如果API在文件系统中找不到任何字体,则使用Arial呈现电子表格。
设置自定义字体文件夹
Aspose.Cells APIs搜索操作系统的默认字体目录以获取所需的字体。如果系统字体目录中没有所需的字体,则API将通过自定义(用户定义)目录进行搜索。FontConfigs类已公开了多种设置自定义字体目录的方法,如下所述。
- FontConfigs.setFontFolder:如果只有一个要设置的文件夹,则此方法很有用。
- FontConfigs.setFontFolders:当字体存在于多个文件夹中,而用户希望将所有文件夹分开设置而不是将所有字体合并到一个文件夹中时,此方法很有用。
- FontConfigs.setFontSources:当用户希望从多个文件夹加载字体或从字节数组加载单个字体文件或字体数据时,此机制很有用。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SetCustomFontFolders.class); | |
// Defining string variables to store paths to font folders & font file | |
String fontFolder1 = dataDir + "/Arial"; | |
String fontFolder2 = dataDir + "/Calibri"; | |
String fontFile = dataDir + "/Arial/arial.ttf"; | |
// Setting first font folder with setFontFolder method | |
// Second parameter directs the API to search the sub folders for font files | |
FontConfigs.setFontFolder(fontFolder1, true); | |
// Setting both font folders with setFontFolders method | |
// Second parameter prohibits the API to search the sub folders for font files | |
FontConfigs.setFontFolders(new String[] { fontFolder1, fontFolder2 }, false); | |
// Defining FolderFontSource | |
FolderFontSource sourceFolder = new FolderFontSource(fontFolder1, false); | |
// Defining FileFontSource | |
FileFontSource sourceFile = new FileFontSource(fontFile); | |
// Defining MemoryFontSource | |
byte[] bytes = Files.readAllBytes(new File(fontFile).toPath()); | |
MemoryFontSource sourceMemory = new MemoryFontSource(bytes); | |
// Setting font sources | |
FontConfigs.setFontSources(new FontSourceBase[] { sourceFolder, sourceFile, sourceMemory }); |
字体替换机制
Aspose.Cells APIs还提供了指定替代字体以进行呈现的功能。当需要的字体在进行转换的机器上不可用时,这种机制非常有用。用户可以提供一系列字体名称作为原始要求的字体的替代品。为了实现这一点,Aspose.Cells APIs公开了FontConfigs.setFontSubstitutes方法,该方法接受2个参数。第一个参数是String类型,应该是需要被替换的字体的名称。第二个参数是String类型的数组。用户可以提供作为原始字体的替代品的字体名称列表(指定在第一个参数中)。
这里是一个简单的使用场景。
//Substituting the Arial font with Times New Roman & Calibri
FontConfigs.setFontSubstitutes("Arial", new String[] { "Times New Roman", "Calibri" });
信息收集
除上述方法外,Aspose.Cells APIs还提供了收集已设置的来源和替换信息的手段。
- FontConfigs.getFontSources:此方法返回一个包含指定字体源列表的 FontSourceBase 类型的数组。如果未设置任何源,FontConfigs.getFontSources 方法将返回一个空数组。
- FontConfigs.getFontSubstitutes:此方法接受String类型的参数,允许指定已设置替代的字体名称。如果为指定的字体名称未设置替代,则 FontConfigs.getFontSubstitutes 方法将返回 null。