Render spreadsheets by page breaks

When printing spreadsheets with a lot of data sheets are automatically broken into pages to fit the paper size and keep printed content readable. To preview the page breaks in Excel navigate to View and click Page Break Preview as it shown on the screenshot.

Page break preview in Excel

In case you print the workbook by clicking at File > Print and there would be two pages in the print preview.

Printing spreadsheet in Excel

To perform the same action programmatically with GroupDocs.Viewer set SpreadsheetOptions to SpreadsheetOptions.forRenderingByPageBreaks() and call view(…) method. Let’s take page-breaks.xlsx and render it to PDF by running the following code.

    try (Viewer viewer = new Viewer("page-breaks.xlsx")) {
        PdfViewOptions viewOptions = new PdfViewOptions();
        viewOptions.setSpreadsheetOptions(SpreadsheetOptions.forRenderingByPageBreaks());

        // Enable rendering gird lines and headings to check that proper areas are rendered
        viewOptions.getSpreadsheetOptions().setRenderGridLines(true);
        viewOptions.getSpreadsheetOptions().setRenderHeadings(true);

        viewer.view(viewOptions);
    }

We’ll get one PDF file with two pages that are corresponds to pages highlighted by Excel on the first screenshot.

PDF rendered by page breaks

The same applies when rendering to HTML, JPEG, and PNG formats.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.