0

I am try to insert images in an xlsx file using docx4j, but the images are the size of a single cell.

My code so far:

    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, drawing, data.bytes);
    String imageRelID = imagePart.getSourceRelationship().getId();

    Matcher cell = CELL.matcher(c.getR());
    col = 0;
    row = 0;

    addBarcode(drawings, data, imageRelID, col, row);

and the method invoked:

public static void addBarcode(CTDrawing drawing, BarcodeData data, String imageRelID, int col, int row) {
    org.docx4j.dml.ObjectFactory dmlObjectFactory = new org.docx4j.dml.ObjectFactory();
    org.docx4j.dml.spreadsheetdrawing.ObjectFactory dmlspreadsheetdrawingObjectFactory = new org.docx4j.dml.spreadsheetdrawing.ObjectFactory();
    // <twoCellAnchor>
    CTTwoCellAnchor twoCellAnchor = dmlspreadsheetdrawingObjectFactory.createCTTwoCellAnchor();
    twoCellAnchor.setEditAs(org.docx4j.dml.spreadsheetdrawing.STEditAs.ONE_CELL);
    drawing.getEGAnchor().add(twoCellAnchor);
    // <twoCellAnchor/from>
    CTMarker from = dmlspreadsheetdrawingObjectFactory.createCTMarker();
    twoCellAnchor.setFrom(from);
    from.setCol(col);
    from.setColOff(0);
    from.setRow(row);
    from.setRowOff(0);
    // <twoCellAnchor/to>
    CTMarker to = dmlspreadsheetdrawingObjectFactory.createCTMarker();
    twoCellAnchor.setTo(to);
    to.setCol(col);
    to.setColOff(data.width * 9525);
    to.setRow(row);
    to.setRowOff(data.height * 9525);
    // <twoCellAnchor/pic>
    CTPicture pic = dmlspreadsheetdrawingObjectFactory.createCTPicture();
    twoCellAnchor.setPic(pic);
    pic.setMacro(null);
    // <twoCellAnchor/clientData>
    CTAnchorClientData clientData = dmlspreadsheetdrawingObjectFactory.createCTAnchorClientData();
    twoCellAnchor.setClientData(clientData);
    // <twoCellAnchor/pic/nvPicPr>
    CTPictureNonVisual nvPicPr = dmlspreadsheetdrawingObjectFactory.createCTPictureNonVisual();
    pic.setNvPicPr(nvPicPr);
    // <twoCellAnchor/pic/nvPicPr/cNvPr>
    CTNonVisualDrawingProps cNvPr = dmlObjectFactory.createCTNonVisualDrawingProps();
    nvPicPr.setCNvPr(cNvPr);
    cNvPr.setDescr(null);
    cNvPr.setName("Barcode");
    cNvPr.setId(1);
    // <twoCellAnchor/pic/nvPicPr/cNvPicPr>
    CTNonVisualPictureProperties cNvPicPr = dmlObjectFactory.createCTNonVisualPictureProperties();
    nvPicPr.setCNvPicPr(cNvPicPr);
    // <twoCellAnchor/pic/nvPicPr/cNvPr/picLocks>
    CTPictureLocking picLocks = dmlObjectFactory.createCTPictureLocking();
    cNvPicPr.setPicLocks(picLocks);
    picLocks.setNoChangeAspect(true);
    // <twoCellAnchor/pic/blipFill>
    CTBlipFillProperties blipFill = dmlObjectFactory.createCTBlipFillProperties();
    pic.setBlipFill(blipFill);
    // <twoCellAnchor/pic/blipFill/blip>
    CTBlip blip = dmlObjectFactory.createCTBlip();
    blipFill.setBlip(blip);
    blip.setLink(null);
    blip.setEmbed(imageRelID);
    blip.setCstate(null); // org.docx4j.dml.STBlipCompression.NONE);
    // <twoCellAnchor/pic/blipFill/blip/extLst>
    CTOfficeArtExtensionList extLst = dmlObjectFactory.createCTOfficeArtExtensionList();
    blip.setExtLst(extLst);
    // <twoCellAnchor/pic/blipFill/blip/extLst/ext>
    // CTOfficeArtExtension ext =
    // dmlObjectFactory.createCTOfficeArtExtension();
    // ext.setUri("{28A0092B-C50C-407E-A947-70E740481C1C}");
    // extLst.getExt().add(ext);
    // <twoCellAnchor/pic/blipFill/stretch>
    CTStretchInfoProperties stretch = dmlObjectFactory.createCTStretchInfoProperties();
    blipFill.setStretch(stretch);
    // <twoCellAnchor/pic/blipFill/stretch/fillRect>
    CTRelativeRect fillRect = dmlObjectFactory.createCTRelativeRect();
    stretch.setFillRect(fillRect);
    fillRect.setR(null);
    fillRect.setT(null);
    fillRect.setL(null);
    fillRect.setB(null);
    // <twoCellAnchor/pic/spPr>
    CTShapeProperties spPr = dmlObjectFactory.createCTShapeProperties();
    pic.setSpPr(spPr);
    // <twoCellAnchor/pic/spPr/xfrm>
    CTTransform2D xfrm = dmlObjectFactory.createCTTransform2D();
    spPr.setXfrm(xfrm);
    xfrm.setRot(null); // Rotation 0
    // <twoCellAnchor/pic/spPr/xfrm/off>
    CTPoint2D off = dmlObjectFactory.createCTPoint2D();
    xfrm.setOff(off);
    off.setY(0);
    off.setX(0);
    // <twoCellAnchor/pic/spPr/xfrm/ext>
    CTPositiveSize2D ext = dmlObjectFactory.createCTPositiveSize2D();
    xfrm.setExt(ext);
    ext.setCx(data.width * 9525);
    ext.setCy(data.height * 9525);
    // <twoCellAnchor/pic/spPr/prstGeom>
    CTPresetGeometry2D prstGeom = dmlObjectFactory.createCTPresetGeometry2D();
    spPr.setPrstGeom(prstGeom);
    prstGeom.setPrst(org.docx4j.dml.STShapeType.RECT);
    // <twoCellAnchor/pic/spPr/prstGeom/avLst>
    CTGeomGuideList avLst = dmlObjectFactory.createCTGeomGuideList();
    prstGeom.setAvLst(avLst);
}
Ben
  • 7,548
  • 31
  • 45
  • I guess you've used the docx4j webapp to generate that code from a sample xlsx, but you'll need to examine the xlsx xml to understand how the size of the image is determined. If you can't work that out, post a new question asking that. That's a generic openxml, xlsx question, not a docx4j question. Once you understand how it is done, then you can modify this question to address any bit you are having trouble expressing in docx4j. – JasonPlutext Jun 24 '14 at 11:32
  • i am using imports: import org.docx4j.dml.*; import org.docx4j.openpackaging.io.SaveToZipFile; import org.docx4j.openpackaging.packages.SpreadsheetMLPackage; import org.docx4j.openpackaging.parts.*; import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; import org.docx4j.openpackaging.parts.relationships.RelationshipsPart.AddPartBehaviour; import org.docx4j.relationships.Relationship; import org.xlsx4j.sml.*; ------------- and used docx4j and not openxml – user3759059 Jun 27 '14 at 10:29

0 Answers0