Search in sources :

Example 1 with Drawing

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);
}
Also used : CTDrawing(org.docx4j.dml.spreadsheetdrawing.CTDrawing) Drawing(org.docx4j.openpackaging.parts.DrawingML.Drawing) SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) Relationship(org.docx4j.relationships.Relationship) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) File(java.io.File)

Aggregations

File (java.io.File)1 CTDrawing (org.docx4j.dml.spreadsheetdrawing.CTDrawing)1 SaveToZipFile (org.docx4j.openpackaging.io.SaveToZipFile)1 SpreadsheetMLPackage (org.docx4j.openpackaging.packages.SpreadsheetMLPackage)1 Drawing (org.docx4j.openpackaging.parts.DrawingML.Drawing)1 PartName (org.docx4j.openpackaging.parts.PartName)1 WorksheetPart (org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart)1 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)1 Relationship (org.docx4j.relationships.Relationship)1