Working with Background in ODS Files
Background in ODS Files
Background can be added to sheets in ODS files. The background can either be a color 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 provides the ability to read the background information and add background in ODS files.
Read Background Information from OSD file
Aspose.Cells 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public static void main(String[] args) throws Exception { | |
// The path to the source directory. | |
String sourceDir = Utils.Get_SourceDirectory(); | |
// The path to the output directory. | |
String outDir = Utils.Get_OutputDirectory(); | |
//Load source Excel file | |
Workbook workbook = new Workbook(sourceDir + "GraphicBackground.ods"); | |
//Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
OdsPageBackground background = worksheet.getPageSetup().getODSPageBackground(); | |
System.out.println("Background Type: " + getTypeValue(background.getType())); | |
System.out.println("Backgorund Position: " + getPositionValue(background.getGraphicPositionType())); | |
//Save background image | |
ByteArrayInputStream stream = new ByteArrayInputStream(background.getGraphicData()); | |
BufferedImage image = ImageIO.read(stream); | |
ImageIO.write(image, "png", new File(outDir + "background.png")); | |
System.out.println("ReadODSBackground executed successfully."); | |
} | |
public static String getTypeValue(int type) { | |
String value = ""; | |
if(type == OdsPageBackgroundType.COLOR) { | |
value = "COLOR"; | |
} else if(type == OdsPageBackgroundType.GRAPHIC) { | |
value = "GRAPHIC"; | |
} else if(type == OdsPageBackgroundType.NONE) { | |
value = "NONE"; | |
} | |
return value; | |
} | |
public static String getPositionValue(int position) { | |
String value = ""; | |
if(position == OdsPageBackgroundGraphicPositionType.BOTTOM_CENTER) { | |
value = "BOTTOM_CENTER"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.BOTTOM_LEFT) { | |
value = "BOTTOM_LEFT"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.BOTTOM_RIGHT) { | |
value = "BOTTOM_RIGHT"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.CENTER_CENTER) { | |
value = "CENTER_CENTER"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.CENTER_LEFT) { | |
value = "CENTER_LEFT"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.CENTER_RIGHT) { | |
value = "CENTER_RIGHT"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.TOP_CENTER) { | |
value = "TOP_CENTER"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.TOP_LEFT) { | |
value = "TOP_LEFT"; | |
} else if(position == OdsPageBackgroundGraphicPositionType.TOP_RIGHT) { | |
value = "TOP_RIGHT"; | |
} | |
return value; | |
} |
Console Output
Background Type: GRAPHIC
Backgorund Position: CENTER_CENTER
Add Colored Background to ODS file
Aspose.Cells provides the ODSPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of ODSPageBackground.Color property to add a color background to the ODS file. Please see the output ODS file generated by the code for reference.
Sample Code
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the output directory. | |
String outDir = Utils.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
//Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
worksheet.getCells().get(0, 0).setValue(1); | |
worksheet.getCells().get(1, 0).setValue(2); | |
worksheet.getCells().get(2, 0).setValue(3); | |
worksheet.getCells().get(3, 0).setValue(4); | |
worksheet.getCells().get(4, 0).setValue(5); | |
worksheet.getCells().get(5, 0).setValue(6); | |
worksheet.getCells().get(0, 1).setValue(7); | |
worksheet.getCells().get(1, 1).setValue(8); | |
worksheet.getCells().get(2, 1).setValue(9); | |
worksheet.getCells().get(3, 1).setValue(10); | |
worksheet.getCells().get(4, 1).setValue(11); | |
worksheet.getCells().get(5, 1).setValue(12); | |
OdsPageBackground background = worksheet.getPageSetup().getODSPageBackground(); | |
background.setColor(Color.getAzure()); | |
background.setType(OdsPageBackgroundType.COLOR); | |
workbook.save(outDir + "ColoredBackground.ods", SaveFormat.ODS); |
Add Graphic Background to ODS file
Aspose.Cells provides the ODSPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of ODSPageBackground.GraphicData property to add graphic background to the ODS file. Please see the output ODS file generated by the code for reference.
Sample Code
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the source directory. | |
String sourceDir = Utils.Get_SourceDirectory(); | |
// The path to the output directory. | |
String outDir = Utils.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
//Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
worksheet.getCells().get(0, 0).setValue(1); | |
worksheet.getCells().get(1, 0).setValue(2); | |
worksheet.getCells().get(2, 0).setValue(3); | |
worksheet.getCells().get(3, 0).setValue(4); | |
worksheet.getCells().get(4, 0).setValue(5); | |
worksheet.getCells().get(5, 0).setValue(6); | |
worksheet.getCells().get(0, 1).setValue(7); | |
worksheet.getCells().get(1, 1).setValue(8); | |
worksheet.getCells().get(2, 1).setValue(9); | |
worksheet.getCells().get(3, 1).setValue(10); | |
worksheet.getCells().get(4, 1).setValue(11); | |
worksheet.getCells().get(5, 1).setValue(12); | |
OdsPageBackground background = worksheet.getPageSetup().getODSPageBackground(); | |
BufferedImage image = ImageIO.read(new File(sourceDir + "background.png")); | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
ImageIO.write(image, "png", bos ); | |
byte [] imageData = bos.toByteArray(); | |
background.setType(OdsPageBackgroundType.GRAPHIC); | |
background.setGraphicData(imageData); | |
background.setGraphicType(OdsPageBackgroundGraphicType.AREA); | |
workbook.save(outDir + "GraphicBackground.ods", SaveFormat.ODS); |