• 热门专题

POI用addPicture插入图片到word里面无法显示

作者:冰树之家  发布日期:2011-08-29 18:51:47
Tag标签:addPicture  插入图片  word  
  • 这几天想通过一个现有word文件作为模板,然后根据标签写入对应数据,折腾了好几天了。

    text解决了,表格解决了,结果才发现图片用XWPFDocument压根无法实现addPicture,POI自身有bug,图片是加入到word文件里面了,可是显示不了。。。

    于是乎,又折腾了2天,终于整蛊了怎么让图片在里面显示出来,其他的还在进一步解决中。

    贴个小代码给大家用,凑合着用,还有很多需要修改的:

     

    /** 
     *  
     */  
    package com.report.util;  
      
    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.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;  
      
    /** 
     * @author 郑宗锐 
     *  
     */  
    public class CustomXWPFDocument extends XWPFDocument {  
        public CustomXWPFDocument(InputStream in) throws IOException {  
            super(in);  
        }  
      
        /** 
         *  
         */  
        public CustomXWPFDocument() {  
            super();  
            // TODO Auto-generated constructor stub   
        }  
      
        /** 
         * @param pkg 
         * @throws IOException 
         */  
        public CustomXWPFDocument(OPCPackage pkg) throws IOException {  
            super(pkg);  
            // TODO Auto-generated constructor stub   
        }  
      
        public void createPicture(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("图片" + id);  
            docPr.setDescr("甩葱玩具");  
        }  
    }  
    

     

    调用方法大概是这样的:
     

    OPCPackage pack = POIXMLDocument.openPackage(fileName);  
       CustomXWPFDocument doc = new CustomXWPFDocument(pack);  
       int ind = doc.addPicture(new FileInputStream("d:\\甩葱玩具.jpg"), XWPFDocument.PICTURE_TYPE_JPEG);  
       doc.createPicture(ind, 259, 58);  
    
About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规