特定のUnicode文字のみのフォントを変更してPDFに保存する
一部のUnicode文字は、ユーザー指定のフォントでは表示されません。そのようなUnicode文字の1つが Non-breaking Hyphen (U+2011) で、Unicode番号は8209です。この文字は Times New Roman では表示できませんが、Arial Unicode MS のような他のフォントでは表示できます。
そのような文字が特定のフォント(例: Times New Roman)で単語や文章の中に含まれている場合、Aspose.CellsはArial Unicode MSなどでこの文字を表示できるフォントに全体の単語や文章のフォントを変更します。
しかし、これは一部のユーザーにとって望ましくない動作です。彼らは特定の文字のフォントのみを変更したいとし、単語や文章全体のフォントを変更したくありません。
この問題に対処するために、Aspose.Cellsは PdfSaveOptions.setFontSubstitutionCharGranularity() プロパティを提供しており、表示できない特定の文字のフォントだけを変更し、単語や文章の残りの部分のフォントを変更しないように true に設定する必要があります。
例
以下のスクリーンショットは、PdfSaveOptions.setFontSubstitutionCharGranularity() プロパティを設定せずに生成された2つの出力PDFを比較しています。1つは PdfSaveOptions.setFontSubstitutionCharGranularity() プロパティを true に設定した後に生成されたものです。最初のPDFでは、非改行ハイフンのために、全体の文のフォントがTimes New RomanからArial Unicode MSに変更されています。一方、2番目のPDFでは、非改行ハイフンのみのフォントが変更されています。
// 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); | |