Change the Font on just the specific Unicode characters while saving to PDF
Some Unicode characters are not displayable by the user specified font. One such Unicode character is Non-breaking Hyphen (U+2011) and its Unicode number is 8209. This character cannot be displayed with Times New Roman, but it can be displayed with other fonts like Arial Unicode MS.
When such a character occurs inside some word or sentence which is in some specific font like Times New Roman, then Aspose.Cells changes the font of entire word or sentence to font which could display this character like Arial Unicode to MS.
However, this is undesirable behavior for some users and they want only that specific character’s font must be changed instead of changing the font of entire word or sentence.
To deal with this problem, Aspose.Cells provides PdfSaveOptions.IsFontSubstitutionCharGranularity property which should be set true so that only the font of specific character which is not displayable to be changed to displayable font and rest of the word or sentence should remain in original font.
Example
The following screenshot compares the two output PDFs generated by the sample code below.
One is generated without setting PdfSaveOptions.IsFontSubstitutionCharGranularity property and the other was generated after setting the PdfSaveOptions.IsFontSubstitutionCharGranularity property to true.
As you can see in the first Pdf, the font of entire sentence has changed from Times New Roman to Arial Unicode MS because of Non-Breaking Hyphen. While in the second Pdf, only the font of Non-Breaking Hyphen has changed.
First Pdf File |
---|
![]() |
Second Pdf File |
---|
![]() |
Sample Code
// 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); |