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