使用 C++ via Node.js 在保存为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

示例代码

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Create workbook object
const workbook = new AsposeCells.Workbook();

// Access the first worksheet
const worksheet = workbook.getWorksheets().get(0);

// Access cells
const cell1 = worksheet.getCells().get("A1");
const cell2 = worksheet.getCells().get("B1");

// Set the styles of both cells to Times New Roman
let style = cell1.getStyle();
style.getFont().setName("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" + String.fromCharCode(8209) + " with Non-Breaking Hyphen");

// Autofit the columns
worksheet.autoFitColumns();

// Save to Pdf without setting PdfSaveOptions.IsFontSubstitutionCharGranularity
workbook.save(path.join(dataDir, "SampleOutput_out.pdf"));

// Save to Pdf after setting PdfSaveOptions.IsFontSubstitutionCharGranularity to true
const opts = new AsposeCells.PdfSaveOptions();
opts.setIsFontSubstitutionCharGranularity(true);
workbook.save(path.join(dataDir, "SampleOutput2_out.pdf"), opts);