GroupDocs.Viewer uses page breaks to split a worksheet into separate pages. Microsoft Excel adds automatic page breaks based on paper size and page settings, but you can also insert manual horizontal and vertical page breaks. Switch to Page Break Preview in Microsoft Excel to see where page breaks appear on a worksheet.
In the image below, the vertical page break is inserted after the column E, and the horizontal page break is located under the row 30.
The following example converts a worksheet to PDF and uses the page breaks to split this worksheet into pages:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("products.xlsx")){// Convert the spreadsheet to PDF.varviewOptions=newPdfViewOptions("output.pdf");// Split using page breaks.viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForRenderingByPageBreaks();viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("products.xlsx")' Convert the spreadsheet to PDF.
DimviewOptions=NewPdfViewOptions("output.pdf")' Split using page breaks.
viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForRenderingByPageBreaks()viewer.View(viewOptions)EndUsingEndSubEndModule
The image below illustrates the result.
Split a worksheet into pages by rows
The following code snippet splits a worksheet into two pages and renders them to PDF:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("two-pages.xlsx")){// Specify number of rows for every page.introwsPerPage=15;// Convert the spreadsheet to PDF.varviewOptions=newPdfViewOptions("output.pdf");// Split by number of rows.viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForSplitSheetIntoPages(rowsPerPage);viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("two-pages.xlsx")' Specify number of rows for every page.
DimrowsPerPageAsInteger=15' Convert the spreadsheet to PDF.
DimviewOptions=NewPdfViewOptions("output.pdf")' Split by number of rows.
viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForSplitSheetIntoPages(rowsPerPage)viewer.View(viewOptions)EndUsingEndSubEndModule
The image below demonstrates the output PDF file.
Split a worksheet into pages by rows and columns
The following code snippet splits a worksheet into four pages and renders them to PDF:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("four-pages.xlsx")){// Specify number of rows and columns for every page.introwsPerPage=15;intcolumnsPerPage=7;// Convert the spreadsheet to PDF.varviewOptions=newPdfViewOptions("output.pdf");// Split by number of rows and columns.viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForSplitSheetIntoPages(rowsPerPage,columnsPerPage);viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("four-pages.xlsx")' Specify number of rows and columns for every page.
DimrowsPerPageAsInteger=15DimcolumnsPerPageAsInteger=7' Convert the spreadsheet to PDF.
DimviewOptions=NewPdfViewOptions("output.pdf")' Split by number of rows and columns.
viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForSplitSheetIntoPages(rowsPerPage,columnsPerPage)viewer.View(viewOptions)EndUsingEndSubEndModule
The image below demonstrates the output PDF file.
Render a print area
In Microsoft Excel, you can designate one or more cell ranges in a worksheet as the only region to print (a print area). A worksheet can contain multiple print areas. Each print area prints on its own page.
GroupDocs.Viewer also supports this option. Call the SpreadsheetOptions.ForRenderingPrintArea static method and assign the returned SpreadsheetOptions instance to the ViewOptions.SpreadsheetOptions property to display only the worksheet’s print area in the output HTML, PDF, or image file.
The following example renders the print area displayed in the image above to PDF:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to PDF.varviewOptions=newPdfViewOptions("output.pdf");// Render the print area only.viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForRenderingPrintArea();viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("invoice.xlsx")' Convert the spreadsheet to PDF.
DimviewOptions=NewPdfViewOptions("output.pdf")' Render the print area only.
viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForRenderingPrintArea()viewer.View(viewOptions)EndUsingEndSubEndModule
The following example converts each worksheet to one page in the PDF file:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("Products.xlsx")){// Convert the spreadsheet to PDF.varviewOptions=newPdfViewOptions("output.pdf");// Render each worksheet to one page.viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForOnePagePerSheet();viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("Products.xlsx")' Convert the spreadsheet to PDF.
DimviewOptions=NewPdfViewOptions("output.pdf")' Render each worksheet to one page.
viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForOnePagePerSheet()viewer.View(viewOptions)EndUsingEndSubEndModule
The image below illustrates the result. The output PDF file contains one page that displays all worksheet data.
Render worksheet by page breaks and print area
GroupDocs.Viewer uses page breaks to split a worksheet into separate pages. Microsoft Excel adds automatic page breaks based on paper size and page settings, but you can also insert manual horizontal and vertical page breaks. Switch to Page Break Preview in Microsoft Excel to see where page breaks appear on a worksheet.
Also, you can designate one or more cell ranges in a worksheet as the only region to print (a print area). A worksheet can contain multiple print areas. Each print area prints on its own page.
When printing, Microsoft Excel splits a worksheet into pages using both page breaks and print areas. In the following image the red line shows the print area, and the blue line shows page breaks:
The following example renders the Microsoft Excel spreadsheet using page breaks and print areas displayed in the image above to PDF:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...//render spreadsheet to PDFusing(varviewer=newViewer("products.xlsx")){varviewOptions=newPdfViewOptions("output.pdf");viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForRenderingPrintAreaAndPageBreaks();viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())'render spreadsheet to PDF
Usingviewer=NewViewer("products.xlsx")DimviewOptions=NewPdfViewOptions("output.pdf")viewOptions.SpreadsheetOptions=SpreadsheetOptions.ForRenderingPrintAreaAndPageBreaks()viewer.View(viewOptions)EndUsingEndSubEndModule
The image below illustrates the result:
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.