Travailler avec l arrière plan dans les fichiers ODS
Arrière-plan dans les fichiers ODS
Un arrière-plan peut être ajouté aux feuilles dans les fichiers ODS. L’arrière-plan peut être soit un arrière-plan de couleur, soit un arrière-plan graphique. L’arrière-plan n’est pas visible lorsque le fichier est ouvert mais s’il est imprimé en PDF, l’arrière-plan est visible dans le PDF généré. L’arrière-plan est également visible dans la boîte de dialogue d’aperçu avant impression.
Aspose.Cells offre la possibilité de lire les informations d’arrière-plan et d’ajouter un arrière-plan dans les fichiers ODS.
Lire les informations d’arrière-plan à partir du fichier OSD
Aspose.Cells fournit la classe ODSPageBackground pour gérer l’arrière-plan dans les fichiers ODS. L’exemple de code suivant démontre l’utilisation de la classe ODSPageBackground en chargeant le fichier ODS source (GraphicBackground.ods) et en lisant les informations d’arrière-plan. Veuillez consulter la sortie de la console générée par le code pour référence.
Code d’exemple
// 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; | |
} |
Sortie console
Background Type: GRAPHIC
Backgorund Position: CENTER_CENTER
Ajouter un arrière-plan coloré au fichier ODS
Aspose.Cells fournit la classe ODSPageBackground pour gérer l’arrière-plan dans les fichiers ODS. L’exemple de code suivant démontre l’utilisation de la propriété ODSPageBackground.Color pour ajouter un arrière-plan de couleur au fichier ODS. Veuillez consulter le fichier ODS de sortie (ColoredBackground.ods) généré par le code pour référence.
Code d’exemple
// 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); |
Ajouter un arrière-plan graphique au fichier ODS
Aspose.Cells fournit la classe ODSPageBackground pour gérer l’arrière-plan dans les fichiers ODS. L’exemple de code suivant démontre l’utilisation de la propriété ODSPageBackground.GraphicData pour ajouter un arrière-plan graphique au fichier ODS. Veuillez consulter le fichier ODS de sortie (GraphicBackground.ods) généré par le code pour référence.
Code d’exemple
// 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); |