Cambia il Font solo per i caratteri Unicode specifici durante il salvataggio in PDF
Alcuni caratteri Unicode non sono visualizzabili con il font specificato dall’utente. Un tale carattere Unicode è il Trattino Non Separabile (U+2011) e il suo numero Unicode è 8209. Questo carattere non può essere visualizzato con Times New Roman, ma può essere visualizzato con altri font come Arial Unicode MS.
Quando un tale carattere compare all’interno di una parola o frase che si trova in un determinato font come Times New Roman, allora Aspose.Cells cambia il font di tutta la parola o frase al font che può visualizzare questo carattere come Arial Unicode MS.
Tuttavia, questo è un comportamento indesiderabile per alcuni utenti e vogliono solo che il font di quel personaggio specifico venga cambiato invece di cambiare il font dell’intera parola o frase.
Per risolvere questo problema, Aspose.Cells fornisce la proprietà PdfSaveOptions.IsFontSubstitutionCharGranularity che dovrebbe essere impostata su true in modo che venga cambiato solo il font di un carattere specifico che non è visualizzabile con un font visualizzabile e il resto della parola o frase dovrebbe rimanere nel font originale.
Esempio
Lo screenshot seguente confronta i due file PDF generati dal codice di esempio qui sotto.
Uno è generato senza impostare la proprietà PdfSaveOptions.IsFontSubstitutionCharGranularity e l’altro è generato dopo aver impostato la proprietà PdfSaveOptions.IsFontSubstitutionCharGranularity su true.
Come puoi vedere nel primo Pdf, il font dell’intera frase è cambiato da Times New Roman a Arial Unicode MS a causa del trattino non interrompibile. Mentre nel secondo Pdf, è cambiato solo il font del trattino non interrompibile.
Primo file Pdf |
---|
![]() |
Secondo file Pdf |
---|
![]() |
Codice di Esempio
// 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); |