java - How to insert images to xlsx by docx4j -
i seek insert images in xlsx file using docx4j, images size of single cell.
my code 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 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 = dmlspreadsheetdrawingobjectfactory.createctmarker(); twocellanchor.setfrom(from); from.setcol(col); from.setcoloff(0); from.setrow(row); from.setrowoff(0); // <twocellanchor/to> ctmarker = 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); } java excel docx4j
No comments:
Post a Comment