java – 在word文档中插入图片

前端之家收集整理的这篇文章主要介绍了java – 在word文档中插入图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我第一次在Apache POI上工作,而我将要问的问题已经在这个网站上被问到,但是没有给出明确的答案,所以我别无选择,只能用你的帮助.

我正在尝试编写一个java程序,它从一个文件夹中获取图像,并将该图像插入到word文档中.我正在使用Apache POI进行此程序.在这里我发贴我的代码

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. This is my 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();
    }
}

我可以创建word文档文件,我也可以插入文本docx.addPicture(picbytes,Document.PICTURE_TYPE_JPEG);线路给我的错误是“添加到docx”.我已经添加了这个程序的所有可能的罐子.对于这个错误,我搜索了整个网络,发现很多人都有类似的问题. XWPFDocument引用的“addPicture”不起作用.请帮我解决这个问题.

解决方法

首先,我想指出apache poi- Link提供的例子,即正确的做法
doc.createParagraph().createRun().addPicture(new FileInputStream(imgFile),format,imgFile,Units.toEMU(200),Units.toEMU(200));

但是,在执行上述语句后,仍然存在一个现有的错误,导致.docx文件不可读.这可能会很快解决,在这种情况下,上述声明将会进行.与此同时,还有一个解决方案.

首先,生成没有任何图片的docx文件.然后将此类CustomXWPFDocument添加到您的包中.

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

然后,通过添加您的图片创建更新的文档:

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

您还应该在构建路径中具有以下jar:

POI-OOXML-模式

xmlbeans

dom4j

猜你在找的Java相关文章