如何通过docx4j将图像插入xlsx

时间:2014-06-20 06:40:11

标签: java excel docx4j

我尝试使用docx4j在xlsx文件中插入图像,但图像大小为单个单元格。

到目前为止我的代码:

    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);

并调用了该方法:

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);
}

0 个答案:

没有答案