配置字体以渲染电子表格

可能的使用场景

Aspose.Cells API 提供了在图像格式中渲染电子表格以及将其转换为 PDF 和 XPS 格式的功能。为了最大程度地保持转换的准确性,电子表格中使用的字体应该存储在操作系统的默认字体目录中。如果所需字体不可用,则 Aspose.Cells API 将尝试使用可用的字体来替代所需字体。

选择字体

以下是 Aspose.Cells API 在幕后执行的过程。

  1. API试图在文件系统中找到与电子表格中使用的确切字体名称匹配的字体。
  2. 如果API无法找到具有相同名称的精确字体,则尝试使用工作簿的DefaultStyle.Font属性下指定的默认字体。
  3. 如果API无法找到工作簿的DefaultStyle.Font属性下定义的字体,则尝试使用PdfSaveOptions.DefaultFontImageOrPrintOptions.DefaultFont属性下指定的字体。
  4. 如果API无法找到PdfSaveOptions.DefaultFontImageOrPrintOptions.DefaultFont属性下定义的字体,则尝试使用FontConfigs.DefaultFontName属性下指定的字体。
  5. 如果API无法找到FontConfigs.DefaultFontName属性下定义的字体,则尝试从所有可用字体中选择最合适的字体。
  6. 最后,如果API在文件系统中找不到任何字体,则使用Arial呈现电子表格。

设置自定义字体文件夹

Aspose.Cells API会搜索操作系统的默认字体目录以获取所需的字体。如果系统的字体目录中没有所需的字体,则API将通过自定义(用户定义的)目录进行搜索。 FontConfigs 类已公开了许多设置自定义字体目录的方式。

  1. FontConfigs.SetFontFolder:如果只有一个要设置的文件夹,则此方法很有用。
  2. FontConfigs.SetFontFolders:当字体存在于多个文件夹中,而用户希望将所有文件夹分开设置而不是将所有字体合并到一个文件夹中时,此方法很有用。
  3. FontConfigs.SetFontSources:当用户希望从多个文件夹加载字体或从字节数组加载单个字体文件或字体数据时,此机制很有用。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Defining string variables to store paths to font folders & font file
string fontFolder1 = dataDir + "Arial";
string fontFolder2 = dataDir + "Calibri";
string fontFile = dataDir + "arial.ttf";
// Setting first font folder with SetFontFolder method
// Second parameter directs the API to search the subfolders for font files
FontConfigs.SetFontFolder(fontFolder1, true);
// Setting both font folders with SetFontFolders method
// Second parameter prohibits the API to search the subfolders 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
MemoryFontSource sourceMemory = new MemoryFontSource(System.IO.File.ReadAllBytes(fontFile));
// Setting font sources
FontConfigs.SetFontSources(new FontSourceBase[] { sourceFolder, sourceFile, sourceMemory });

字体替换机制

Aspose.Cells API 还提供了指定替代字体以进行渲染的能力。 当需要的字体在进行转换的机器上不可用时,这种机制非常有用。 用户可以提供字体名称列表作为原始所需字体的替代。 为了实现这一点,Aspose.Cells API 公开了一个接受 2 个参数的 FontConfigs.SetFontSubstitutes 方法。 第一个参数是 string 类型,应该是需要替换的字体的名称。 第二个参数是 string 类型的数组。 用户可以提供字体名称列表作为原始字体名称(指定在第一个参数中)的替代。

这里是一个简单的使用场景。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Substituting the Arial font with Times New Roman & Calibri
FontConfigs.SetFontSubstitutes("Arial", new string[] { "Times New Roman", "Calibri" });

信息收集

除上述方法外,Aspose.Cells APIs还提供了收集已设置的来源和替换信息的手段。

  1. FontConfigs.GetFontSources 方法返回一个 FontSourceBase 类型的数组,其中包含指定字体源的列表。 如果没有设置源,则 FontConfigs.GetFontSources 方法将返回一个空数组。
  2. FontConfigs.GetFontSubstitutes 方法接受一个 string 类型的参数,允许指定已设置替代字体的字体名称。 如果为指定的字体名称设置了替换,则 FontConfigs.GetFontSubstitutes 方法将返回 null。

高级主题