How to programmatically insert an image into a Word document?

I'm just looking for this. Is it possible to insert an image into an MS Word document through Java? answer ....

+3
source share
7 answers
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 java file link in the [src] folder and do not forget to change the package structure. I tested it with the word 2007, apache poi 3.10 is not sure about other versions.

+2
source

Not easy, but possible. Try Apache POI .

+1
source

Openoffice UNO Aspose.word JAVA.

SO.

, Java Openoffice UNO.

Java Sdk.

+1

Apache POI HWPF - API Java Microsoft Word

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

-:

HWPF - Microsoft Word 97 (-2007) Java. .docx Word 2007. OLE2.

: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java?view=log

0

Pretty straightforward with Docmosis - place the marker image, mark it and tell the document to process the document, replacing the image.

0
source

If the document does not exist and you want to create from scratch, use this:

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

else: Apache PIO

0
source

Source: https://habr.com/ru/post/1737640/


All Articles