在渲染到图像之前从数据中删除空白

Contents
[ ]

图纸渲染类能够将工作表转换为具有任何指定属性的图像文件,例如图像格式、分页表等。支持多种图像格式,包括 BMP、GIF、JPG、TIFF 和 EMF。

当您使用纸张到图像功能时,默认情况下,输出图像周围有白色/空白区域,即边框。你可以删除它。将源工作表的顶部、左侧、底部和右侧页面设置页边距设置为 0,并指定图像或打印选项相应的属性。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(RemoveWhitespaceAroundData.class) + "TechnicalArticles/";
// Instantiate a workbook
// Open the template file
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Specify your print area if you want
// sheet.PageSetup.PrintArea = "A1:H8";
// To remove the white border around the image.
sheet.getPageSetup().setLeftMargin(0);
sheet.getPageSetup().setRightMargin(0);
sheet.getPageSetup().setTopMargin(0);
sheet.getPageSetup().setBottomMargin(0);
// Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageType(ImageType.EMF);
// Set only one page would be rendered for the image
imgOptions.setOnePagePerSheet(true);
imgOptions.setPrintingPage(PrintingPageType.IGNORE_BLANK);
// Create the SheetRender object based on the sheet with its
// ImageOrPrintOptions attributes
SheetRender render = new SheetRender(sheet, imgOptions);
// Convert the image
render.toImage(0, dataDir + "RWhitespaceAroundData_out.emf");