Impostare la proprietà DefaultFont di PdfSaveOptions e ImageOrPrintOptions per avere la priorità
Possibili Scenari di Utilizzo
Mentre si imposta la proprietà DefaultFont di PdfSaveOptions e ImageOrPrintOptions, potresti aspettarti che il salvataggio in PDF o immagine imposti quel DefaultFont a tutto il testo in un foglio di lavoro che ha un carattere mancante (non installato).
In generale, quando si salva in PDF o immagine, Aspose.Cells per Python via .NET cercherà prima di impostare il font predefinito del Workbook (cioè, Workbook.DefaultStyle.Font). Se il font predefinito del workbook ancora non può mostrare/renderizzare correttamente il testo, Aspose.Cells per Python via .NET tenterà di renderizzare con il font indicato contro l’attributo DefaultFont in PdfSaveOptions/ImageOrPrintOptions.
Per soddisfare le tue aspettative, abbiamo una proprietà booleana chiamata “check_workbook_default_font” in PdfSaveOptions/ImageOrPrintOptions. Puoi impostarla su false per disabilitare il tentativo con il font predefinito del workbook o permettere alla impostazione default_font in PdfSaveOptions/ImageOrPrintOptions di avere priorità.
Impostare la proprietà DefaultFont di PdfSaveOptions/ImageOrPrintOptions
Il seguente esempio di codice apre un file Excel. La cella A1 (nel primo foglio di lavoro) ha un testo impostato su “Christmas Time Font text”. Il nome del font è “Christmas Time Personal Use” che non è installato sulla macchina. Impostiamo l’attributo default_font di PdfSaveOptions/ImageOrPrintOptions su “Times New Roman”. Impostiamo anche la proprietà booleana check_workbook_default_font su “false” per garantire che il testo della cella A1 venga renderizzato con il font “Times New Roman” e non venga usato il font predefinito del workbook (“Calibri” in questo caso). Il codice rende il primo foglio di lavoro in formati immagine PNG e TIFF. Infine, rende in formato PDF.
Questa è la schermata del file di modello utilizzato nel codice di esempio.
Questa è l’immagine PNG di output dopo aver impostato la proprietà ImageOrPrintOptions.default_font su “Times New Roman”.
Vedi l’immagine TIFF di output dopo aver impostato la proprietà ImageOrPrintOptions.default_font su “Times New Roman”.
Vedi il file PDF di output dopo aver impostato la proprietà PdfSaveOptions.default_font su “Times New Roman”.
Codice di Esempio
from aspose.cells import PdfSaveOptions, Workbook | |
from aspose.cells.drawing import ImageType | |
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender, WorkbookRender | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Open an Excel file. | |
workbook = Workbook(sourceDir + "sampleSetDefaultFontPropertyOfPdfSaveOptionsAndImageOrPrintOptions.xlsx") | |
# Rendering to PNG file format while setting the CheckWorkbookDefaultFont attribue to false. | |
# So, "Times New Roman" font would be used for any missing (not installed) font in the workbook. | |
imgOpt = ImageOrPrintOptions() | |
imgOpt.image_type = ImageType.PNG | |
imgOpt.check_workbook_default_font = False | |
imgOpt.default_font = "Times New Roman" | |
sr = SheetRender(workbook.worksheets[0], imgOpt) | |
sr.to_image(0, outputDir + "out1_imagePNG.png") | |
# Rendering to TIFF file format while setting the CheckWorkbookDefaultFont attribue to false. | |
# So, "Times New Roman" font would be used for any missing (not installed) font in the workbook. | |
imgOpt.image_type = ImageType.TIFF | |
wr = WorkbookRender(workbook, imgOpt) | |
wr.to_image(outputDir + "out1_imageTIFF.tiff") | |
# Rendering to PDF file format while setting the CheckWorkbookDefaultFont attribue to false. | |
# So, "Times New Roman" font would be used for any missing (not installed) font in the workbook. | |
saveOptions = PdfSaveOptions() | |
saveOptions.default_font = "Times New Roman" | |
saveOptions.check_workbook_default_font = False | |
workbook.save(outputDir + "out1_pdf.pdf", saveOptions) |