How to remove blank or empty pages in the output images of the worksheets in Java

public static void main(String[] args) throws Exception {
Workbook book = new Workbook(“/Users/zyx/Downloads/45f96609d3914f8255aef79f21cbd700.xlsx”);
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageType(ImageType.JPEG);

    final WorksheetCollection worksheets = book.getWorksheets();
    for (int i = 0; i < worksheets.getCount(); i++) {
        // Create a SheetRender object for the target sheet
        SheetRender sr = new SheetRender(worksheets.get(i), imgOptions);
        for (int j = 0; j < sr.getPageCount(); j++) {
            // Generate an image for the worksheet
            final String name = "aaa" + "_sheet_" + (i+1) + "_" + (j+1) + ".jpg";
            sr.toImage(j, name);
        }
    }
    book.dispose();
}

https://tezign-assets.oss-cn-beijing.aliyuncs.com/45f96609d3914f8255aef79f21cbd700.xlsx

image.png (424.5 KB)

@zyx,

This is not an issue with the Aspose.Cells API by any means. Please open your template Excel file into MS Excel and click on the sheet named “抖音KOL明细”. Now open “Page Setup” and click on “Print Preview” button to open the sheet into print preview. You will see it has 1821 pages or more. And, you will also notice there are lots of blank pages there (see the attached screenshot below). That is the reason when you will render this sheet to image files, you will get lots of images for blank pages as well. By default, Aspose.Cells will render images for the sheet pages similar to what is shown in print preview of the sheet (even the pages are blank or empty).
sc_shot1.png (31.2 KB)

Let us know if you still have any issue or confusion.

ok, thank you,but I want to know how to judge an empty page so that I can exclude it instead of printing it

@zyx,

Please change the line in your code to following for your needs:

imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;

Alternatively, you may use the following lines:

final WorksheetCollection worksheets = book.getWorksheets();
for (int i = 0; i < worksheets.getCount(); i++) {
worksheets.get(i).getCells().deleteBlankRows();
worksheets.get(i).getCells().deleteBlankColumns();
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(worksheets.get(i), imgOptions);