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

Contents
[ ]

例子

下面的屏幕截图比较了下面示例代码生成的两个输出 PDF。一个没有设置就生成了PdfSaveOptions.setFontSubstitutionCharGranularity()属性,另一个是在设置后生成的PdfSaveOptions.setFontSubstitutionCharGranularity()财产给真的.正如您在第一个 PDF 中所看到的,由于 Non-Breaking Hyphen,整个句子的字体已从 Times New Roman 更改为 Arial Unicode MS。而在第二个 PDF 中,只有 Non-Breaking Hyphen 的字体发生了变化。

待办事项:图片_替代_文本

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ChangeFontonspecificUnicodecharacters.class);
// Create workbook object
Workbook workbook = new Workbook();
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Access cells
Cell cell1 = worksheet.getCells().get("A1");
Cell cell2 = worksheet.getCells().get("B1");
// Set the styles of both cells to Times New Roman
Style 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" + (char) (8209) + " with Non-Breaking Hyphen");
// Autofit the columns
worksheet.autoFitColumns();
// Save to Pdf without setting PdfSaveOptions.IsFontSubstitutionCharGranularity
workbook.save(dataDir + "output.pdf");
// Save to Pdf after setting PdfSaveOptions.IsFontSubstitutionCharGranularity to true
PdfSaveOptions opts = new PdfSaveOptions();
opts.setFontSubstitutionCharGranularity(true);
workbook.save(dataDir + "output2.pdf", opts);