保存到 PDF 时仅更改特定 Unicode 字符的字体

例子

下面的屏幕截图比较了下面示例代码生成的两个输出 PDF。

一个是在未设置 PdfSaveOptions.IsFontSubstitutionCharGranularity 属性的情况下生成的,另一个是在将 PdfSaveOptions.IsFontSubstitutionCharGranularity 属性设置为 true 后生成的。

正如您在第一个 Pdf 中看到的那样,由于不间断连字符,整个句子的字体已从 Times New Roman 更改为 Arial Unicode MS。而在第二个 Pdf 中,只有 Non-Breaking Hyphen 的字体发生了变化。

第一个 PDF 文件
待办事项:图片_替代_文本
第二个 PDF 文件
待办事项:图片_替代_文本

示例代码

// 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);
// Create workbook object
Workbook workbook = new Workbook();
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Access cells
Cell cell1 = worksheet.Cells["A1"];
Cell cell2 = worksheet.Cells["B1"];
// Set the styles of both cells to Times New Roman
Style style = cell1.GetStyle();
style.Font.Name = "Times New Roman";
cell1.SetStyle(style);
cell2.SetStyle(style);
// Put the values inside the cell
cell1.PutValue("Hello without Non-Breaking Hyphen");
cell2.PutValue("Hello" + Convert.ToChar(8209) + " with Non-Breaking Hyphen");
// Autofit the columns
worksheet.AutoFitColumns();
// Save to Pdf without setting PdfSaveOptions.IsFontSubstitutionCharGranularity
workbook.Save(dataDir + "SampleOutput_out.pdf");
// Save to Pdf after setting PdfSaveOptions.IsFontSubstitutionCharGranularity to true
PdfSaveOptions opts = new PdfSaveOptions();
opts.IsFontSubstitutionCharGranularity = true;
workbook.Save(dataDir + "SampleOutput2_out.pdf", opts);