Modifica il Font su Caratteri Unicode Specifici durante il Salvataggio in PDF con Python.NET
Alcuni caratteri Unicode non sono visualizzabili con i font specificati dall’utente. Uno di questi caratteri Unicode è Sintagma Non-Lineare (U+2011) con numero Unicode 8209. Questo carattere non può essere visualizzato con Times New Roman ma può essere mostrato con font come Arial Unicode MS.
Quando tali caratteri appaiono in testi formattati con un font specifico (ad esempio, Times New Roman), Aspose.Cells cambia automaticamente il font dell’intera parola/frase a uno compatibile (ad esempio, Arial Unicode MS). Per gli utenti che vogliono cambiare solo il font del carattere non visualizzabile, offriamo un controllo granulare tramite la proprietà PdfSaveOptions.is_font_substitution_char_granularity.
Esempio di Confronto
Gli screenshot di seguito mostrano output con impostazioni differenti. Il primo PDF mostra la sostituzione del font completo del testo, mentre il secondo PDF modifica solo il font del carattere specifico.
Sostituzione Completa del Testo | Sostituzione a Livello di Carattere |
---|---|
![]() |
![]() |
Passaggi di Implementazione
Per abilitare la sostituzione del font a livello di carattere:
- Crea un oggetto Workbook
- Accedi alle celle del foglio di lavoro usando la proprietà Worksheet.cells
- Imposta i valori delle celle contenenti caratteri Unicode speciali
- Configura PdfSaveOptions con:
is_font_substitution_char_granularity = True
- Salva il workbook in formato PDF
import os
from aspose.cells import Workbook, PdfSaveOptions
# For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# Create workbook object
workbook = Workbook()
# Access the first worksheet
worksheet = workbook.worksheets[0]
# Access cells
cell1 = worksheet.cells.get("A1")
cell2 = worksheet.cells.get("B1")
# Set the styles of both cells to Times New Roman
style = cell1.get_style()
style.font.name = "Times New Roman"
cell1.set_style(style)
cell2.set_style(style)
# Put the values inside the cell
cell1.put_value("Hello without Non-Breaking Hyphen")
cell2.put_value("Hello" + chr(8209) + " with Non-Breaking Hyphen")
# Autofit the columns
worksheet.auto_fit_columns()
# Save to Pdf without setting PdfSaveOptions.is_font_substitution_char_granularity
workbook.save(os.path.join(data_dir, "SampleOutput_out.pdf"))
# Save to Pdf after setting PdfSaveOptions.is_font_substitution_char_granularity to true
opts = PdfSaveOptions()
opts.is_font_substitution_char_granularity = True
workbook.save(os.path.join(data_dir, "SampleOutput2_out.pdf"), opts)
Configurazione Chiave
Utilizza questi componenti API essenziali:
- Classe PdfSaveOptions per impostazioni di rendering PDF
- Proprietà is_font_substitution_char_granularity per sostituzione del font a livello di carattere
- Metodo Workbook.save per generazione output
is_font_substitution_char_granularity
) invece del PascalCase utilizzato in .NET.