Working with Background in ODS Files with Node.js via C++
Background in ODS Files
Background can be added to sheets in ODS files. The background can either be a colored background or graphic background. The background is not visible when the file is open but if the file is printed as PDF, the background is visible in the generated PDF. The background is also visible in the print preview dialogue.
Aspose.Cells for Node.js via C++ provides the ability to read the background information and add the background in ODS files.
Read Background Information from ODS file
Aspose.Cells for Node.js via C++ provides the OdsPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of OdsPageBackground class by loading the source ODS file and reading the background information. Please see the Console Output generated by the code for reference.
Sample Code
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// Source and output directories
const sourceDir = path.join(__dirname, "data");
const outputDir = path.join(__dirname, "output");
// Load source Excel file
const filePath = path.join(sourceDir, "GraphicBackground.ods");
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
const background = worksheet.getPageSetup().getODSPageBackground();
console.log("Background Type: " + background.getType().toString());
console.log("Background Position: " + background.getGraphicPositionType().toString());
// Save background image
const imagePath = outputDir + "background.jpg";
fs.writeFileSync(imagePath, Buffer.from(background.getGraphicData()));
Console Output
Background Type: Graphic
Background Position: CenterCenter
Add Colored Background to ODS file
Aspose.Cells for Node.js via C++ provides the OdsPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of OdsPageBackground.getColor() property to add a color background to the ODS file. Please see the output ODS file generated by the code for reference.
Sample Code
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Output directory
const outputDir = path.join(__dirname, "output");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
worksheet.getCells().get(0, 0).putValue(1);
worksheet.getCells().get(1, 0).putValue(2);
worksheet.getCells().get(2, 0).putValue(3);
worksheet.getCells().get(3, 0).putValue(4);
worksheet.getCells().get(4, 0).putValue(5);
worksheet.getCells().get(5, 0).putValue(6);
worksheet.getCells().get(0, 1).putValue(7);
worksheet.getCells().get(1, 1).putValue(8);
worksheet.getCells().get(2, 1).putValue(9);
worksheet.getCells().get(3, 1).putValue(10);
worksheet.getCells().get(4, 1).putValue(11);
worksheet.getCells().get(5, 1).putValue(12);
const background = worksheet.getPageSetup().getODSPageBackground();
background.setColor(AsposeCells.Color.Azure);
background.setType(AsposeCells.OdsPageBackgroundType.Color);
workbook.save(outputDir + "ColoredBackground.ods", AsposeCells.SaveFormat.Ods);
Add Graphic Background to ODS file
Aspose.Cells for Node.js via C++ provides the OdsPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of OdsPageBackground.getGraphicData() property to add graphic background to the ODS file. Please see the output ODS file generated by the code for reference.
Sample Code
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// Source directory
const sourceDir = path.join(__dirname, "data");
// Output directory
const outputDir = path.join(__dirname, "output");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
worksheet.getCells().get(0, 0).putValue(1);
worksheet.getCells().get(1, 0).putValue(2);
worksheet.getCells().get(2, 0).putValue(3);
worksheet.getCells().get(3, 0).putValue(4);
worksheet.getCells().get(4, 0).putValue(5);
worksheet.getCells().get(5, 0).putValue(6);
worksheet.getCells().get(0, 1).putValue(7);
worksheet.getCells().get(1, 1).putValue(8);
worksheet.getCells().get(2, 1).putValue(9);
worksheet.getCells().get(3, 1).putValue(10);
worksheet.getCells().get(4, 1).putValue(11);
worksheet.getCells().get(5, 1).putValue(12);
const background = worksheet.getPageSetup().getODSPageBackground();
background.setType(AsposeCells.OdsPageBackgroundType.Graphic);
background.setGraphicData(fs.readFileSync(path.join(sourceDir, "background.jpg")));
background.setGraphicType(AsposeCells.OdsPageBackgroundGraphicType.Area);
workbook.save(outputDir + "GraphicBackground.ods", AsposeCells.SaveFormat.Ods);