Package org.docx4j.openpackaging.parts.DrawingML

Examples of org.docx4j.openpackaging.parts.DrawingML.Drawing


   
    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);
View Full Code Here


        }
    }

    private void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, BinaryPartAbstractImage xlsxImage, Image image, CTTwoCellAnchor anchor) throws InvalidFormatException {
        PartName drawingPartName = new PartName(worksheetPart.getPartName().getName().replace("worksheets/sheet", "drawings/drawing"));
        Drawing drawing = (Drawing) pkg.getParts().get(drawingPartName);
        java.util.List<Object> objects = drawing.getJaxbElement().getEGAnchor();
        String rid = "rId" + (objects.size() + 1);

        CTPicture pic = new CTPicture();
        CTPictureNonVisual nvPicPr = new CTPictureNonVisual();
        CTNonVisualDrawingProps nvpr = new CTNonVisualDrawingProps();
        nvpr.setId(objects.size() + 2);
        String name = xlsxImage.getPartName().getName();
        name = name.substring(name.lastIndexOf("/") + 1);
        nvpr.setName(name);
        nvpr.setDescr(name);
        nvPicPr.setCNvPr(nvpr);
        CTPictureLocking ctPictureLocking = new CTPictureLocking();
        ctPictureLocking.setNoChangeAspect(true);
        CTNonVisualPictureProperties nvpp = new CTNonVisualPictureProperties();
        nvpp.setPicLocks(ctPictureLocking);
        nvPicPr.setCNvPicPr(nvpp);
        pic.setNvPicPr(nvPicPr);
        CTBlipFillProperties blipProps = new CTBlipFillProperties();
        CTStretchInfoProperties props = new CTStretchInfoProperties();
        CTRelativeRect rect = new CTRelativeRect();
        props.setFillRect(rect);
        blipProps.setStretch(props);
        CTBlip blip = new CTBlip();
        blip.setEmbed(rid);
        blip.setCstate(STBlipCompression.PRINT);
        blipProps.setBlip(blip);
        pic.setBlipFill(blipProps);
        CTShapeProperties sppr = new CTShapeProperties();
        ImageSize imageSize = new ImageSize(image.width, image.height, 96);//todo this doesn't work unfortunately
        imageSize.calcSizeFromPixels();
        CTPoint2D off = new CTPoint2D();
        off.setX(0);
        off.setY(0);
        CTPositiveSize2D ext = new CTPositiveSize2D();
        ext.setCx(imageSize.getWidthMpt());
        ext.setCy(imageSize.getHeightMpt());
        CTTransform2D xfrm = new CTTransform2D();
        xfrm.setOff(off);
        xfrm.setExt(ext);
        sppr.setXfrm(xfrm);
        CTPresetGeometry2D prstGeom = new CTPresetGeometry2D();
        prstGeom.setPrst(STShapeType.RECT);
        prstGeom.setAvLst(new CTGeomGuideList());
        sppr.setPrstGeom(prstGeom);
        pic.setSpPr(sppr);
        anchor.setPic(pic);
        CTAnchorClientData data = new CTAnchorClientData();
        anchor.setClientData(data);

        drawing.getJaxbElement().getEGAnchor().add(anchor);

        Relationship rel = new Relationship();
        rel.setId(rid);
        rel.setType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
        rel.setTarget("../media/" + name);

        drawing.getRelationshipsPart().addRelationship(rel);
        RelationshipsPart relPart = drawing.getRelationshipsPart();
        pkg.getParts().remove(relPart.getPartName());
        pkg.getParts().put(relPart);
        pkg.getParts().remove(drawing.getPartName());
        pkg.getParts().put(drawing);
    }
View Full Code Here

            if (part instanceof JaxbXmlPart) {
                Object o = ((JaxbXmlPart) part).getJaxbElement();

                if (o instanceof CTChartSpace) {
                    Drawing drawing = (Drawing) parent;
                    CTDrawing ctDrawing = drawing.getJaxbElement();
                    Object anchorObj = ctDrawing.getEGAnchor().get(chartNum++);

                    Range range = null;
                    CTTwoCellAnchor ctTwoCellAnchor = null;
                    if (anchorObj instanceof CTTwoCellAnchor) {
View Full Code Here

   
    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);
View Full Code Here

TOP

Related Classes of org.docx4j.openpackaging.parts.DrawingML.Drawing

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.