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

可能的使用场景

在设置DefaultFont属性时,您可能期望将其设置为工作簿中具有缺失(未安装)字体的所有文本的PdfSaveOptionsImageOrPrintOptions

通常,在保存为PDF或图像时,Aspose.Cells首先会尝试设置工作簿的默认字体(即Workbook.DefaultStyle.Font)。如果工作簿的默认字体仍然无法正确显示/呈现文本,那么Aspose.Cells将尝试使用PdfSaveOptions/ImageOrPrintOptionsDefaultFont属性所提到的字体进行呈现。

为了符合您的期望,我们在PdfSaveOptions/ImageOrPrintOptions中有一个名为"CheckWorkbookDefaultFont“的布尔属性。您可以将其设置为false以禁用尝试工作簿的默认字体,或者让PdfSaveOptions/ImageOrPrintOptions中的DefaultFont设置具有优先级。

设置PdfSaveOptions/ImageOrPrintOptions的DefaultFont属性

下面的示例代码打开一个Excel文件。第一个工作表中的A1单元格的文本设置为"Christmas Time Font text”。字体名称为"Christmas Time Personal Use",在该计算机上未安装。我们将PdfSaveOptions/ImageOrPrintOptionsDefaultFont属性设置为"Times New Roman"。我们还将CheckWorkbookDefaultFont布尔属性设置为"false",以确保A1单元格的文本使用"Times New Roman"字体呈现,而不使用工作簿的默认字体(在此情况下为"Calibri")。 该代码将第一个工作表呈现为PNG和TIFF图像格式。最后将其呈现为PDF文件格式。

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

todo:image_alt_text

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

todo:image_alt_text

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

PdfSaveOptions.DefaultFont属性设置为"Times New Roman"后的输出PDF文件。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String srcDir = Utils.Get_SourceDirectory();
String outDir = Utils.Get_OutputDirectory();
// Open an Excel file.
Workbook workbook = new Workbook(
srcDir + "sampleSetDefaultFontPropertyOfPdfSaveOptionsAndImageOrPrintOptions.xlsx");
// Rendering to PNG file format while setting the
// CheckWorkbookDefaultFont attribute to false.
// So, "Times New Roman" font would be used for any missing (not
// installed) font in the workbook.
ImageOrPrintOptions imgOpt = new ImageOrPrintOptions();
imgOpt.setImageType(ImageType.PNG);
imgOpt.setCheckWorkbookDefaultFont(false);
imgOpt.setDefaultFont("Times New Roman");
SheetRender sr = new SheetRender(workbook.getWorksheets().get(0), imgOpt);
sr.toImage(0, outDir + "outputSetDefaultFontProperty_ImagePNG.png");
// Rendering to TIFF file format while setting the
// CheckWorkbookDefaultFont attribute to false.
// So, "Times New Roman" font would be used for any missing (not
// installed) font in the workbook.
imgOpt.setImageType(ImageType.TIFF);
WorkbookRender wr = new WorkbookRender(workbook, imgOpt);
wr.toImage(outDir + "outputSetDefaultFontProperty_ImageTIFF.tiff");
// Rendering to PDF file format while setting the
// CheckWorkbookDefaultFont attribute to false.
// So, "Times New Roman" font would be used for any missing (not
// installed) font in the workbook.
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setDefaultFont("Times New Roman");
saveOptions.setCheckWorkbookDefaultFont(false);
workbook.save(outDir + "outputSetDefaultFontProperty_PDF.pdf", saveOptions);