use of org.docx4j.openpackaging.parts.DrawingML.Drawing in project Aspose.Cells-for-Java by aspose-cells.
the class Xlsx4jAddImage method main.
public static void main(String[] args) throws Exception {
// The path to the documents directory.
String dataDir = Utils.getDataDir(Xlsx4jAddImage.class);
String outputfilepath = dataDir + "AddImage-Xlsx4j.xlsx";
String imagefilePath = dataDir + "greentick.png";
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
WorksheetPart worksheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
// Create Drawing part and add to sheet
Drawing drawingPart = new Drawing();
Relationship drawingRel = worksheet.addTargetPart(drawingPart);
// Add anchor XML to worksheet
org.xlsx4j.sml.CTDrawing drawing = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTDrawing();
worksheet.getJaxbElement().setDrawing(drawing);
drawing.setId(drawingRel.getId());
// Create image part and add to Drawing part
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, drawingPart, FileUtils.readFileToByteArray(new File(imagefilePath)));
String imageRelID = imagePart.getSourceRelationship().getId();
// Create and set drawing part content
// Take your pick ..
// .. build it using code
// drawingPart.setJaxbElement(
// buildDrawingPartContentUsingCode(imageRelID));
// .. or build it from an XML string
drawingPart.setJaxbElement(buildDrawingPartContentFromXmlString(imageRelID));
// Save the xlsx
SaveToZipFile saver = new SaveToZipFile(pkg);
saver.save(outputfilepath);
System.out.println("\n\n done .. " + outputfilepath);
}
Aggregations