Lavorare con lo sfondo nei file ODS
Sfondo nei file ODS
Lo sfondo può essere aggiunto ai fogli nei file ODS. Lo sfondo può essere un colore di sfondo o uno sfondo grafico. Lo sfondo non è visibile quando il file è aperto, ma se il file è stampato come PDF, lo sfondo è visibile nel PDF generato. Lo sfondo è visibile anche nella finestra di anteprima di stampa.
Aspose.Cells fornisce la possibilità di leggere le informazioni di sfondo e aggiungere uno sfondo nei file ODS.
Leggere le informazioni di sfondo dal file OSD
Aspose.Cells fornisce la classe ODSPageBackground per gestire lo sfondo nei file ODS. Il seguente esempio di codice dimostra l’uso della classe ODSPageBackground caricando il file ODS di origine (GraphicBackground.ods) e leggendo le informazioni di sfondo. Si prega di consultare l’output della console generato dal codice per riferimento.
Codice di Esempio
// 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; | |
} |
Output della console
Background Type: GRAPHIC
Backgorund Position: CENTER_CENTER
Aggiungere uno sfondo colorato al file ODS
Aspose.Cells fornisce la classe ODSPageBackground per gestire lo sfondo nei file ODS. Il seguente esempio di codice dimostra l’uso della proprietà ODSPageBackground.Color per aggiungere uno sfondo colorato al file ODS. Si prega di consultare il file ODS di output (ColoredBackground.ods) generato dal codice per riferimento.
Codice di Esempio
// 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); |
Aggiungere uno sfondo grafico al file ODS
Aspose.Cells fornisce la classe ODSPageBackground per gestire lo sfondo nei file ODS. Il seguente esempio di codice dimostra l’uso della proprietà ODSPageBackground.GraphicData per aggiungere uno sfondo grafico al file ODS. Si prega di consultare il file ODS di output (GraphicBackground.ods) generato dal codice per riferimento.
Codice di Esempio
// 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); |