保存到 PDF 时仅更改特定 Unicode 字符的字体
Contents
[
Hide
]
某些 Unicode 字符无法通过用户指定的字体显示。一个这样的 Unicode 字符是不间断连字符(U+2011) 其 Unicode 编号为 8209。此字符无法显示为英语字体格式一种 但它可以用其他字体显示,如宋体 Unicode MS.
当这样的字符出现在某些特定字体(如 Times New Roman)的单词或句子中时,Aspose.Cells 将整个单词或句子的字体更改为可以显示此字符的字体,如 Arial Unicode 到 MS。
然而,这对某些用户来说是不受欢迎的行为,他们只希望必须更改特定字符的字体,而不是更改整个单词或句子的字体。
针对这个问题,Aspose.Cells提供PdfSaveOptions.setFontSubstitutionCharGranularity()应该设置的属性真的这样只有无法显示的特定字符的字体发生变化,而其余单词或句子的字体保持不变。
例子
下面的屏幕截图比较了下面示例代码生成的两个输出 PDF。一个没有设置就生成了PdfSaveOptions.setFontSubstitutionCharGranularity()属性,另一个是在设置后生成的PdfSaveOptions.setFontSubstitutionCharGranularity()财产给真的.正如您在第一个 PDF 中所看到的,由于 Non-Breaking Hyphen,整个句子的字体已从 Times New Roman 更改为 Arial Unicode MS。而在第二个 PDF 中,只有 Non-Breaking Hyphen 的字体发生了变化。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |