this first time working on apache poi , question going ask has been asked on site no clear answer given them have no choice take help.
i trying write java program takes images 1 folder , insert image word document. using apache poi program. here posting code.
import java.io.*; import java.util.*; import org.apache.poi.util.ioutils; import org.apache.poi.xwpf.usermodel.*; public class imagesdoc { public static void main(string[] args) throws ioexception { xwpfdocument docx = new xwpfdocument(); xwpfparagraph par = docx.createparagraph(); xwpfrun run = par.createrun(); run.settext("hello, world. first java generated docx-file. have fun."); run.setfontsize(13); inputstream pic = new fileinputstream("c:\\users\\amitabh\\pictures\\pics\\pool.jpg"); byte [] picbytes = ioutils.tobytearray(pic); docx.addpicture(picbytes, document.picture_type_jpeg); fileoutputstream out = new fileoutputstream("c:\\users\\amitabh\\pictures\\pics\\simple1.docx"); docx.write(out); out.close(); pic.close(); } } i able create word document file , able insert text docx.addpicture(picbytes, document.picture_type_jpeg); line giving me error as"add cast docx". have added possible jars program. error have searched on net , found many people having similar problem. "addpicture" xwpfdocument reference not working. please me resolve problem.
first, point out example provided apache poi - link, i.e. correct way
doc.createparagraph().createrun().addpicture(new fileinputstream(imgfile), format, imgfile, units.toemu(200), units.toemu(200)); however, there still existing bug renders .docx file unreadable after executing above statement. might resolved soon, in case above-mentioned statement work. meantime, there work-around.
first, generate docx file without pictures. add class customxwpfdocument package.
import org.apache.poi.xwpf.usermodel.xwpfdocument; import org.apache.xmlbeans.xmlexception; import org.apache.xmlbeans.xmltoken; import org.openxmlformats.schemas.drawingml.x2006.main.ctnonvisualdrawingprops; import org.openxmlformats.schemas.drawingml.x2006.main.ctpositivesize2d; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingdrawing.ctinline; import java.io.ioexception; import java.io.inputstream; public class customxwpfdocument extends xwpfdocument { public customxwpfdocument(inputstream in) throws ioexception { super(in); } public void createpicture(string blipid,int id, int width, int height) { final int emu = 9525; width *= emu; height *= emu; //string blipid = getallpictures().get(id).getpackagerelationship().getid(); ctinline inline = createparagraph().createrun().getctr().addnewdrawing().addnewinline(); string picxml = "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" + " <a:graphicdata uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + " <pic:nvpicpr>" + " <pic:cnvpr id=\"" + id + "\" name=\"generated\"/>" + " <pic:cnvpicpr/>" + " </pic:nvpicpr>" + " <pic:blipfill>" + " <a:blip r:embed=\"" + blipid + "\" xmlns:r=\"http://schemas.openxmlformats.org/officedocument/2006/relationships\"/>" + " <a:stretch>" + " <a:fillrect/>" + " </a:stretch>" + " </pic:blipfill>" + " <pic:sppr>" + " <a:xfrm>" + " <a:off x=\"0\" y=\"0\"/>" + " <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" + " </a:xfrm>" + " <a:prstgeom prst=\"rect\">" + " <a:avlst/>" + " </a:prstgeom>" + " </pic:sppr>" + " </pic:pic>" + " </a:graphicdata>" + "</a:graphic>"; //ctgraphicalobjectdata graphicdata = inline.addnewgraphic().addnewgraphicdata(); xmltoken xmltoken = null; try { xmltoken = xmltoken.factory.parse(picxml); } catch(xmlexception xe) { xe.printstacktrace(); } inline.set(xmltoken); //graphicdata.set(xmltoken); inline.setdistt(0); inline.setdistb(0); inline.setdistl(0); inline.setdistr(0); ctpositivesize2d extent = inline.addnewextent(); extent.setcx(width); extent.setcy(height); ctnonvisualdrawingprops docpr = inline.addnewdocpr(); docpr.setid(id); docpr.setname("picture " + id); docpr.setdescr("generated"); } } then, create updated document adding pictures :-
customxwpfdocument document = new customxwpfdocument(new fileinputstream(new file("c:\\users\\avarice\\desktop\\doc1.docx"))); fileoutputstream fos = new fileoutputstream(new file("c:\\users\\avarice\\desktop\\doc2.docx")); string id = document.addpicturedata(new fileinputstream(new file("c:\\users\\avarice\\desktop\\thumbnail.jpg")), document.picture_type_jpeg); document.createpicture(id,document.getnextpicnamenumber(document.picture_type_jpeg), 64, 64); document.write(fos); fos.flush(); fos.close(); you should have following jars in build path:-
poi-ooxml-schemas
Comments
Post a Comment