呈现 Excel 文件时获取字体替换警告
Contents
[
Hide
]
有时,将 Microsoft Excel 文件呈现为 PDF 时,Aspose.Cells 会替换字体。 Aspose.Cells 提供了一项功能,可让开发人员通过发出警告来了解已替换的特定字体。这是一个有用的功能,可以帮助您确定为什么呈现为 PDF 的 Aspose.Cells 看起来与原始 Microsoft Excel 文件不同,以便您可以采取适当的措施。例如,安装缺少的字体以使渲染结果看起来相同。
要在将 Excel 文件呈现为 PDF 时获取字体替换警告,请实施 IWarningCallback 接口并使用您实施的接口设置 PdfSaveOptions.WarningCallback 属性。
下面的屏幕截图显示了我们将在以下代码中使用的源 Excel 文件。它在单元格 A6 和 A7 中有一些文本,其字体无法由 Microsoft Excel 正确呈现。
并非所有字体都能正确呈现 |
---|
![]() |
Aspose.Cells 会将单元格 A6 和 A7 中的字体替换为合适的字体,如下所示。 |
替换字体 |
---|
![]() |
下载源文件和输出 PDF
您可以从以下链接下载源 Excel 文件和输出 PDF
代码
以下代码实现 IWarningCallback 并使用实现的接口设置 PdfSaveOptions.WarningCallback 属性。现在,无论何时在任何单元格中替换任何字体,Aspose.Cells 都会在 WarningCallback.Warning() 方法中发出警告。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public class GetWarningsForFontSubstitution : IWarningCallback | |
{ | |
public void Warning(WarningInfo info) | |
{ | |
if (info.WarningType == WarningType.FontSubstitution) | |
{ | |
Debug.WriteLine("WARNING INFO: " + info.Description); | |
} | |
} | |
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.WarningCallback = new GetWarningsForFontSubstitution(); | |
dataDir = dataDir + "output_out.pdf"; | |
workbook.Save(dataDir, options); | |
} | |
} |
输出
将源 Excel 文件转换为 PDF 后,警告输出到调试控制台,如下所示:
WARNING INFO: Font substitution: Font [ Athene Logos; Regular ]has been substituted in Cell [ A6 ]in Sheet [ Sheet1 ].
WARNING INFO: Font substitution: Font [ B Traffic; Regular ]has been substituted in Cell [ A7 ]in Sheet [ Sheet1 ].
如果您的电子表格包含公式,最好在将电子表格呈现为 PDF 格式之前调用 Workbook.CalculateFormula 方法。这样做将确保重新计算公式相关值,并在 PDF 中呈现正确的值。