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, durante il salvataggio in PDF o immagine, Aspose.Cells cercherà prima di impostare il carattere predefinito del foglio di lavoro (cioè, Workbook.DefaultStyle.Font). Se il carattere predefinito del foglio di lavoro non riesce ancora a mostrare/rappresentare correttamente il testo, allora Aspose.Cells cercherà di rappresentare con il carattere menzionato nel attributo DefaultFont in PdfSaveOptions/ImageOrPrintOptions.

Per far fronte alle tue aspettative, abbiamo una proprietà booleana chiamata “CheckWorkbookDefaultFont” in PdfSaveOptions/ImageOrPrintOptions. Puoi impostarla su false per disabilitare il tentativo del carattere predefinito del foglio di lavoro o lasciare che l’impostazione DefaultFont in PdfSaveOptions/ImageOrPrintOptions abbia la 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) contiene un testo impostato su “Testo carattere Christmas Time”. Il nome del carattere è “Christmas Time Personal Use” che non è installato sulla macchina. Abbiamo impostato l’attributo DefaultFont di PdfSaveOptions/ImageOrPrintOptions  su “Times New Roman”. Abbiamo anche impostato la proprietà booleana CheckWorkbookDefaultFont su “false” che garantisce che il testo della cella A1 venga rappresentato con il carattere “Times New Roman” e non dovrebbe utilizzare il carattere predefinito del foglio di lavoro (“Calibri” in questo caso). Il codice rappresenta il primo foglio di lavoro nei formati di immagine PNG e TIFF. Infine rappresenta in un formato di file PDF.

Questa è la schermata del file di modello utilizzato nel codice di esempio.

todo:image_alt_text

Questa è l’immagine PNG di output dopo aver impostato la proprietà ImageOrPrintOptions.DefaultFont su “Times New Roman”.

todo:image_alt_text

Vedi l’immagine TIFF di output dopo aver impostato la proprietà ImageOrPrintOptions.DefaultFont su “Times New Roman”.

Vedi il file PDF di output dopo aver impostato la proprietà PdfSaveOptions.DefaultFont su “Times New Roman”.

Codice di Esempio

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Open an Excel file.
Workbook workbook = new 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.
ImageOrPrintOptions imgOpt = new ImageOrPrintOptions();
imgOpt.ImageType = ImageType.Png;
imgOpt.CheckWorkbookDefaultFont = false;
imgOpt.DefaultFont = "Times New Roman";
SheetRender sr = new SheetRender(workbook.Worksheets[0], imgOpt);
sr.ToImage(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.ImageType = ImageType.Tiff;
WorkbookRender wr = new WorkbookRender(workbook, imgOpt);
wr.ToImage(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.
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.DefaultFont = "Times New Roman";
saveOptions.CheckWorkbookDefaultFont = false;
workbook.Save(outputDir + "out1_pdf.pdf", saveOptions);