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

示例

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

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

如您在第一个PDF中看到的,整个句子的字体已从Times New Roman更改为Arial Unicode MS,因为有非断开连字符。 而在第二个PDF中,只更改了非断开连字符的字体。

第一个PDF文件
todo:image_alt_text
第二个PDF文件
todo:image_alt_text

示例代码

// 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);