Introduction
According to definition, a Page Break is the place in the text where one page ends and the next one begins. Microsoft Excel allows to add page breaks at any selected cell of the worksheet. The location of the cell where the page break is added, page is ended and all rest of the data after the page break is printed on the next page while printing. In simple words, page breaks divide your worksheet into multiple pages according to your specifications. You can also add page breaks to your worksheets at runtime using Aspose.Cells. Aspose.Cells allows developers to add two kinds of page breaks:
- Horizontal Page Break
- Vertical Page Break
In our rest of the discussion, we will describe that how can you add horizontal or vertical page breaks into your worksheets using Aspose.Cells.
Aspose.Cells & Page Breaks
Aspose.Cells provides a class, Workbook that represents an Excel file. Workbook class contains a Worksheets collection that allows to access each worksheet in the Excel file.
A worksheet is represented by the Worksheet class. Worksheet class provides a wide range of properties and methods to manage a worksheet. But, to add the page breaks, developers may use HPageBreaks and VPageBreaks properties of the Worksheet class.
HPageBreaks and VPageBreaks properties are infact collections that may contain several page breaks. Each collection (HPageBreaks and VPageBreaks) contains several methods to manage horizontal and vertical page breaks. The use of these methods is discussed in below topics.
Adding Page Breaks
To add a page break in a worksheet, please insert vertical and horizontal page breaks at the specified cell by calling the Add methods of HPageBreaks and VPageBreaks collections. Each Add method will take the cell name where the page break is to add.
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Add a page break at cell Y30
workbook.Worksheets[0].HPageBreaks.Add("Y30");
workbook.Worksheets[0].VPageBreaks.Add("Y30");
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Adding a page break at cell Y30
workbook.Worksheets(0).HPageBreaks.Add("Y30")
workbook.Worksheets(0).VPageBreaks.Add("Y30")
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Add a page break at cell Y30
Worksheets worksheets = excel.getWorksheets();
Worksheet worksheet = worksheets.getSheet(0);
HPageBreaks hPageBreaks= worksheet.getHPageBreaks();
hPageBreaks.add("Y30");
VPageBreaks vPageBreaks= worksheet.getVPageBreaks();
vPageBreaks.add("Y30");
Note: In Page Break Preview or Print Preview modes, you can see how these page breaks work.
Clearing All Page Breaks
To clear all page breaks in a worksheet, please call Clear methods of HPageBreaks and VPageBreaks collections.
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Clearing all page breaks
workbook.Worksheets[0].HPageBreaks.Clear();
workbook.Worksheets[0].VPageBreaks.Clear();
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Clearing all page breaks
workbook.Worksheets(0).HPageBreaks.Clear()
workbook.Worksheets(0).VPageBreaks.Clear()
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
workbook.getWorksheets().getSheet(0).getHPageBreaks().clear();
workbook.getWorksheets().getSheet(0).getVPageBreaks().clear();
Removing Specific Page Break
To remove a specific page break in the worksheet, please call RemoveAt methods of HPageBreaks and VPageBreaks collections. Each RemoveAt method will take the index of the page break to be removed.
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Removing a specific page break
workbook.Worksheets[0].HPageBreaks.RemoveAt(0);
workbook.Worksheets[0].VPageBreaks.RemoveAt(0);
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Removing a specific page break
workbook.Worksheets(0).HPageBreaks.RemoveAt(0)
workbook.Worksheets(0).VPageBreaks.RemoveAt(0)
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Removing a specific page break
Worksheets worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.getSheet(0);
HPageBreaks hPageBreaks= worksheet.getHPageBreaks();
hPageBreaks.delete(0);
VPageBreaks vPageBreaks= worksheet.getVPageBreaks();
vPageBreaks.delete(0);
Important to know
When you set FitToPages properties (that is FitToPagesTall & FitToPagesWide) in Page Setup Settings, Page Break Settings will not be affected.