Modifica il Font su Caratteri Unicode Specifici durante il Salvataggio in PDF con Python.NET

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
Cambio Font Completo Cambio Font Selettivo

Passaggi di Implementazione

Per abilitare la sostituzione del font a livello di carattere:

  1. Crea un oggetto Workbook
  2. Accedi alle celle del foglio di lavoro usando la proprietà Worksheet.cells
  3. Imposta i valori delle celle contenenti caratteri Unicode speciali
  4. Configura PdfSaveOptions con:
    • is_font_substitution_char_granularity = True
  5. 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