特定のUnicode文字のみのフォントを変更してPDFに保存する
ユーザー指定のフォントで表示できない Unicode 文字がいくつかあります。そのような Unicode 文字の1つが ノンブレーキングハイフン (U+2011) で、Unicode 番号は 8209 です。この文字は Times New Roman では表示できませんが、 Arial Unicode MS のような他のフォントでは表示できます。
このような文字が特定のフォントである単語や文章内にある場合、Aspose.Cells は、Arial Unicode MS のようにこの文字を表示できるフォントに変更し、その単語や文章全体のフォントを変更します。
ただし、これは一部のユーザーにとって望ましくない動作であり、単語や文章全体のフォントを変更するのではなく、特定の文字のフォントのみを変更したいと考えています。
この問題に対処するため、Aspose.Cells では、PdfSaveOptions.IsFontSubstitutionCharGranularity プロパティが true に設定されている必要があります。これにより、表示できない特定の文字のフォントだけが表示可能なフォントに変更され、単語や文章の残りの部分は元のフォントのままになります。
例
以下のスクリーンショットは、以下のサンプルコードによって生成された2つの出力 PDF を比較しています。
1つは、PdfSaveOptions.IsFontSubstitutionCharGranularity プロパティを設定しないまま生成されたもので、もう1つは、PdfSaveOptions.IsFontSubstitutionCharGranularity プロパティを true に設定した後に生成されたものです。
最初の PDF では、ノンブレーキングハイフンのために全文のフォントが Times New Roman から Arial Unicode MS に変更されたことがわかります。一方、2番目の PDF では、ノンブレーキングハイフンのフォントのみが変更されています。
最初の PDF ファイル |
---|
![]() |
2 番目の PDF ファイル |
---|
![]() |
サンプルコード
// 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); |