Java利用poi对word插入文字图片
Java利用poi对word插入文字图片文档末尾rar压缩包内,完整的Java project demo,包含所需的所有jar包,示例文件,使用说明等。三步实现:一、package com.word.poi;
import java.io.IOException;import java.io.InputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;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;
/*********************************************************?类名:CustomXWPFDocument*?功能:XWPFDocument读写word.docx*?作者:qiucx?2017年4月12日*?修改记录:*??????????日期?????????????????修改人?????????????????修改说明********************************************************/public class CustomXWPFDocument extends XWPFDocument {public CustomXWPFDocument(InputStream in) throws IOException {super(in);}
public CustomXWPFDocument() {super();}
public CustomXWPFDocument(OPCPackage pkg) throws IOException {super(pkg);}
/*********************************************************?函数名:?createPicture*?功能?:?在word中标记的位置生成指定路径的图片*?作者?: qiucx?2017年4月12日*?参数表:?@param id*?参数表:?@param width 设置图片宽度*?参数表:?@param height 设置图片高度*?参数表:?@param paragraph?段落参数???*?返回值:?void????*?修改记录:*??????????日期?????????????????修改人?????????????????修改说明********************************************************/public void createPicture(int id, int width, int height,XWPFParagraph paragraph) {final int EMU = 9525;width *= EMU;height *= EMU;System.out.println("getAllPictures---" + getAllPictures().size());String blipId = getAllPictures().get(id).getPackageRelationship().getId();CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();String picXml = ""+ ""+ "