简体   繁体   English

如何以编程方式将图像插入Word文档?

[英]How to programatically insert image into Word document?

I am just looking for that. 我只是在寻找那个。 Is it possible to insert image into MS Word document through Java? 是否可以通过Java将图像插入MS Word文档? 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. 将文件放在路径[src文件夹]中的链接java文件中,并记住不要更改包结构。 I have tested it with word 2007, apache poi 3.10 not sure about other versions. 我用word 2007测试了它,apache poi 3.10不确定其他版本。

Not easily, but it's possible. 不容易,但这是可能的。 Try Apache POI . 试试Apache POI

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

More information on this old SO question. 关于这个旧的SO问题的更多信息。

Here an example posted in Java section of Openoffice UNO forum. 是一个在Openoffice UNO论坛的Java部分发布的示例。

Here the Java Sdk. 这里是Java Sdk。

Another option is to look at Apache POI HWPF - Java API to Handle Microsoft Word Files 另一种选择是查看Apache POI HWPF - 用于处理Microsoft Word文件的Java API

http://poi.apache.org/hwpf/index.html 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. HWPF是纯Java的Microsoft Word 97(-2007)文件格式的端口名称。 It does not support the new Word 2007 .docx file format, which is not OLE2 based. 它不支持新的Word 2007 .docx文件格式,它不是基于OLE2的。

This might be a good start: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java?view=log 这可能是一个好的开始: http//svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java?view=log

Docmosis相当直接 - 放置标记图像,书签并告诉docmosis处理替换图像的文档。

If the document doesn't exist and you want to create from scratch, use this: 如果文档不存在而您想从头开始创建,请使用以下命令:

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

else: Apache PIO 否则:Apache PIO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM