呈现 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() 方法中发出警告。

// 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 ].