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 uno sfondo a colori o uno sfondo grafico. Lo sfondo non è visibile quando il file è aperto ma se il file viene stampato come PDF, lo sfondo è visibile nel PDF generato. Lo sfondo è visibile anche nella finestra di dialogo dell’anteprima di stampa.

Aspose.Cells offre la possibilità di leggere le informazioni di base e aggiungere sfondo nei file ODS.

Leggi le informazioni di base dal file OSD

Aspose.Cells fornisce ilODSPageBackgroundclasse per gestire lo sfondo nei file ODS. L’esempio di codice seguente illustra l’utilizzo diODSPageBackgroundclasse caricando il filefonte ODSfile e leggendo le informazioni di base. Si prega di consultare l’output della console generato dal codice per riferimento.

Codice d’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;
}

Uscita console

Tipo di sfondo: GRAFICO

Posizione dello sfondo: CENTER_CENTER

Aggiungi sfondo colorato al file ODS

Aspose.Cells fornisce ilODSPageBackgroundclasse per gestire lo sfondo nei file ODS. L’esempio di codice seguente illustra l’utilizzo diODSPageBackground.Colorproprietà per aggiungere uno sfondo colorato al file ODS. Si prega di consultare iluscita ODSfile generato dal codice per riferimento.

Codice d’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);

Aggiungi sfondo grafico al file ODS

Aspose.Cells fornisce ilODSPageBackgroundclasse per gestire lo sfondo nei file ODS. L’esempio di codice seguente illustra l’utilizzo diODSPageBackground.GraphicDataproprietà per aggiungere uno sfondo grafico al file ODS. Si prega di consultare iluscita ODSfile generato dal codice per riferimento.

Codice d’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);