设置PdfSaveOptions和ImageOrPrintOptions的DefaultFont属性具有优先级

可能的使用场景

在设置 PdfSaveOptionsImageOrPrintOptionsDefaultFont 属性时,您可能希望保存为 PDF 或图像时将该 DefaultFont 设置为工作簿中所有没有安装的字体的文本。

通常,在保存为PDF或图片时,Aspose.Cells for Python via .NET会首先尝试设置工作簿的默认字体(即Workbook.DefaultStyle.Font)。如果工作簿的默认字体仍无法正确显示/渲染文本,则Aspose.Cells for Python via .NET会尝试用PdfSaveOptions/ImageOrPrintOptions中DefaultFont属性指定的字体进行渲染。

为了满足您的需求,我们在PdfSaveOptions/ImageOrPrintOptions中增加了一个布尔属性“check_workbook_default_font”。您可以将其设置为false,以禁用尝试使用工作簿的默认字体,或者让PdfSaveOptions/ImageOrPrintOptions中的default_font设置优先。

设置PdfSaveOptions/ImageOrPrintOptions的DefaultFont属性

以下示例代码打开一个Excel文件。第一个工作表的A1单元格中有文本“Christmas Time Font text”,字体名为“Christmas Time Personal Use”,该字体未安装在机器上。我们将PdfSaveOptions/ImageOrPrintOptionsdefault_font属性设置为“Times New Roman”。同时将布尔属性check_workbook_default_font设置为“false”,确保A1单元格的文本用“Times New Roman”字体渲染,不使用工作簿的默认字体(即“Calibri”)。代码将第一个工作表导出为PNG和TIFF图像格式,最后导出为PDF文件。

这是示例代码中使用的模板文件的屏幕截图。

todo:image_alt_text

ImageOrPrintOptions.default_font属性设置为"Times New Roman"后的输出PNG图像。

todo:image_alt_text

查看在将 ImageOrPrintOptions.default_font 属性设置为 “Times New Roman” 后的输出 TIFF 图像。

查看在将 PdfSaveOptions.default_font 属性设置为 “Times New Roman” 后的输出 PDF 文件。

示例代码

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)