Set DefaultFont property of PdfSaveOptions and ImageOrPrintOptions to have priority

Possible Usage Scenarios

While setting the DefaultFont property of PdfSaveOptions and ImageOrPrintOptions, you might expect that saving to PDF or image would set that DefaultFont to all the text in a workbook that has a missing (not installed) font.

Generally, when saving to PDF or image, Aspose.Cells will first try to set Workbook’s default font (i.e., Workbook.DefaultStyle.Font). If workbook’s default font still cannot show/render text properly, then Aspose.Cells will try to render with font mentioned against DefaultFont attribute in PdfSaveOptions/ImageOrPrintOptions.

To cope with your expectation, we have a Boolean property named “CheckWorkbookDefaultFont” in PdfSaveOptions/ImageOrPrintOptions. You can set it to false to disable trying workbook’s default font or let the DefaultFont setting in PdfSaveOptions/ImageOrPrintOptions to have priority.

Set DefaultFont property of PdfSaveOptions/ImageOrPrintOptions

The following sample code opens an Excel file. The A1 cell (in the first worksheet) has a text set to “Christmas Time Font text”. The font name is “Christmas Time Personal Use” that is not installed on the machine. We set DefaultFont attribute of PdfSaveOptions/ImageOrPrintOptions to “Times New Roman”. We also set CheckWorkbookDefaultFont Boolean property to “false” which ensures that the text of A1 cell is rendered with “Times New Roman” font and should not use the default font of the workbook (“Calibri” in this case). The code renders the first worksheet to PNG and TIFF image formats. It finally renders to a PDF file format. 

This is the screenshot of the template file used in the example code.


This is the output PNG image after setting the ImageOrPrintOptions.DefaultFont property to “Times New Roman”.


See the output TIFF image after setting the ImageOrPrintOptions.DefaultFont property to “Times New Roman”.

See the output PDF file after setting the PdfSaveOptions.DefaultFont property to “Times New Roman”.

Sample Code