在呈现Excel文件为PDF时,有时会进行字体替换。Aspose.Cells提供了一个功能,让开发人员知道特定字体已经被替换,从而发出警告。这是一个有用的功能,可以帮助您确定为什么Aspose.Cells呈现的PDF与实际Excel文件不同,并且您可以采取相应的措施。例如,您可以安装缺少的字体,以便呈现结果看起来一致。

要在将Excel文件呈现为PDF时获取字体替换的警告,请实现 IWarningCallback 接口,并使用您实现的接口设置 PdfSaveOptions.WarningCallback 属性。

下面的屏幕截图显示了我们将在以下代码中使用的源Excel文件。在单元格A6和A7中有一些文本,微软Excel无法正确呈现其中的字体。

并非所有字体都能正确呈现
todo:image_alt_text
Aspose.Cells将使用合适的字体替换单元格A6和A7中的字体,如下所示。
替换字体
todo:image_alt_text

下载源文件和输出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 ].