Managing Page Breaks with Node.js via C++

Page Breaks

Aspose.Cells provides a Workbook class that represents an Excel file. The Workbook class contains a Workbook.getWorksheets() collection that allows access to each worksheet in the Excel file.

A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods used to manage a worksheet.

To add the page breaks, use the Worksheet class' Worksheet.getHorizontalPageBreaks() and Worksheet.getVerticalPageBreaks() properties.

The Worksheet.getHorizontalPageBreaks() and Worksheet.getVerticalPageBreaks() properties are collections that may contain several page breaks. Each collection contains several methods for managing horizontal and vertical page breaks.

Adding Page Breaks

To add a page break in a worksheet, insert vertical and horizontal page breaks at the specified cell by calling the HorizontalPageBreakCollection.add(number, number, number) and VerticalPageBreakCollection.add(number, number, number) methods. Each add method takes the name of the cell where the break should be added.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Add a page break at cell Y30
workbook.getWorksheets().get(0).getHorizontalPageBreaks().add("Y30");
workbook.getWorksheets().get(0).getVerticalPageBreaks().add("Y30");

// Save the Excel file.
workbook.save(path.join(dataDir, "AddingPageBreaks_out.xls"));

Removing Specific Page Break

To remove a specific page break, call the HorizontalPageBreakCollection.removeAt(number) and VerticalPageBreakCollection.removeAt(number) methods. Each removeAt method takes the index of the page break about to be removed.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "PageBreaks.xls");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook(filePath);

// Removing a specific page break
workbook.getWorksheets().get(0).getHorizontalPageBreaks().removeAt(0);
workbook.getWorksheets().get(0).getVerticalPageBreaks().removeAt(0);

// Save the Excel file.
workbook.save(path.join(dataDir, "RemoveSpecificPageBreak_out.xls"));

Important to know

When you set fitToPages properties (that is PageSetup.getFitToPagesTall() and PageSetup.getFitToPagesWide()) in page setup settings, the page break settings are affected, so, if you print the worksheet, the page break settings are not considered although they are still set.