Setting Margins with Node.js via C++
Setting Margins
Aspose.Cells provides a class, Workbook, that represents an Excel file. The Workbook class contains the 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 the Worksheet.getPageSetup() property used to set the page setup options for a worksheet. The Worksheet.getPageSetup() attribute is an object of the Worksheet.getPageSetup() class that enables developers to set different page layout options for a printed worksheet. The Worksheet.getPageSetup() class provides various properties and methods used to set page setup options.
Page Margins
Set page margins (left, right, top, bottom) using Worksheet.getPageSetup() class members. A few of the methods are listed below which are used to specify page margins:
- PageSetup.getLeftMargin()
- PageSetup.getRightMargin()
- PageSetup.getTopMargin()
- PageSetup.getBottomMargin()
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create a workbook object
const workbook = new AsposeCells.Workbook();
// Get the worksheets in the workbook
const worksheets = workbook.getWorksheets();
// Get the first (default) worksheet
const worksheet = worksheets.get(0);
// Get the pagesetup object
const pageSetup = worksheet.getPageSetup();
// Set bottom, left, right and top page margins
pageSetup.setBottomMargin(2);
pageSetup.setLeftMargin(1);
pageSetup.setRightMargin(1);
pageSetup.setTopMargin(3);
// Save the Workbook.
workbook.save(path.join(dataDir, "SetMargins_out.xls"));
Center on Page
It is possible to center something on a page horizontally and vertically. For this, there are some useful members of the Worksheet.getPageSetup() class, PageSetup.getCenterHorizontally() and PageSetup.getCenterVertically().
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create a workbook object
const workbook = new AsposeCells.Workbook();
// Get the worksheets in the workbook
const worksheets = workbook.getWorksheets();
// Get the first (default) worksheet
const worksheet = worksheets.get(0);
// Get the pagesetup object
const pageSetup = worksheet.getPageSetup();
// Specify Center on page Horizontally and Vertically
pageSetup.setCenterHorizontally(true);
pageSetup.setCenterVertically(true);
// Save the Workbook.
workbook.save(path.join(dataDir, "CenterOnPage_out.xls"));
Header and Footer Margins
Set header and footer margins with the Worksheet.getPageSetup() class members such as PageSetup.getHeaderMargin() and PageSetup.getFooterMargin().
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create a workbook object
const workbook = new AsposeCells.Workbook();
// Get the worksheets in the workbook
const worksheets = workbook.getWorksheets();
// Get the first (default) worksheet
const worksheet = worksheets.get(0);
// Get the pagesetup object
const pageSetup = worksheet.getPageSetup();
// Specify Header / Footer margins
pageSetup.setHeaderMargin(2);
pageSetup.setFooterMargin(2);
// Save the Workbook.
workbook.save(path.join(dataDir, "CenterOnPage_out.xls"));