在将电子表格呈现为图像时设置默认字体

在将电子表格呈现为图像时设置默认字体

以下示例代码创建一个工作簿,在第一个工作表的单元格A4中添加一些文本,并将其字体设置为无效或不存在的字体。然后,它获取工作表的两个图像。第一个图像是通过将ImageOrPrintOptions.DefaultFont属性设置为Courier New获得的,第二个图像是通过将ImageOrPrintOptions.DefaultFont属性设置为Times New Roman获得的。

ImageOrPrintOptions.DefaultFont属性设置为Courier New后的输出图像。

todo:image_alt_text

在将 ImageOrPrintOptions.DefaultFont 属性设置为 Times New Roman 后,这是输出图像。

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Directory path where output HTML files are to be saved
String dataDir = Utils.getSharedDataDir(SetDefaultFontWhileRenderingSpreadsheetToImages.class) + "Conversion/";
//Create workbook object.
Workbook wb = new Workbook();
//Set default font of the workbook to none
Style s = wb.getDefaultStyle();
s.getFont().setName("");
wb.setDefaultStyle(s);
//Access first worksheet.
Worksheet ws = wb.getWorksheets().get(0);
//Access cell A4 and add some text inside it.
Cell cell = ws.getCells().get("A4");
cell.putValue("This text has some unknown or invalid font which does not exist.");
//Set the font of cell A4 which is unknown.
Style st = cell.getStyle();
st.getFont().setName("UnknownNotExist");
st.getFont().setSize(20);
st.setTextWrapped(true);
cell.setStyle(st);
//Set first column width and fourth column height
ws.getCells().setColumnWidth(0, 80);
ws.getCells().setRowHeight(3, 60);
//Create image or print options.
ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.setOnePagePerSheet(true);
opts.setImageFormat(ImageFormat.getPng());
//Render worksheet image with Courier New as default font.
opts.setDefaultFont("Courier New");
SheetRender sr = new SheetRender(ws, opts);
sr.toImage(0, dataDir + "out_courier_new.png");
//Render worksheet image again with Times New Roman as default font.
opts.setDefaultFont("Times New Roman");
sr = new SheetRender(ws, opts);
sr.toImage(0, dataDir + "out_times_new_roman.png");