开发者

How to programatically insert image into Word document?

I am just looking for that. Is it possible to insert image into MS Word开发者_如何学Go document through Java? please reply....


import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class test {
    public static void main(String[] args) throws Exception {
        XWPFDocument doc = new XWPFDocument();
        XWPFParagraph p = doc.createParagraph();
        XWPFRun xwpfRun = p.createRun();
        String[] IMageargs={
                "c:/1.jpg","c:/2.jpg","c:/3.jpg","c:/4.jpg"
        };
        for (String imgFile : IMageargs) {
            int format=XWPFDocument.PICTURE_TYPE_JPEG;
            xwpfRun.setText(imgFile);
            xwpfRun.addBreak();
            xwpfRun.addPicture (new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
            //xwpfRun.addBreak(BreakType.PAGE);
        }
        FileOutputStream out = new FileOutputStream("C:\\test.docx");
        doc.write(out);
        out.close();
    }
}

Put the file in the link java file in the path[src folder] and remember not to change the package structure. I have tested it with word 2007, apache poi 3.10 not sure about other versions.


Not easily, but it's possible. Try Apache POI.


Have a look to Openoffice UNO or Aspose.word for JAVA.

More information on this old SO question.

Here an example posted in Java section of Openoffice UNO forum.

Here the Java Sdk.


Check out Aspose Words for Java.

  • http://www.aspose.com/categories/java-components/aspose.words-for-java/default.aspx


Pretty straight forward with Docmosis - place a marker image, book mark it and tell docmosis to process the document replacing the image.


If the document doesn't exist and you want to create from scratch, use this:

http://code.google.com/p/java2word

else: Apache PIO


Another option is to look at Apache POI HWPF - Java API to Handle Microsoft Word Files

http://poi.apache.org/hwpf/index.html

From the website:

HWPF is the name of our port of the Microsoft Word 97(-2007) file format to pure Java. It does not support the new Word 2007 .docx file format, which is not OLE2 based.

This might be a good start: https://github.com/apache/poi/blob/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/TestHWPFPictures.java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜