I am just looking for that. Is it possible to insert image into MS Word document through Java? please reply....
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 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.

- 170,088
- 45
- 397
- 571

- 21
- 3
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.

- 1
- 1

- 71,966
- 47
- 171
- 241
-
not getting any thingh in Openoffice. please provide the exact source code for the same. – sukhada garud Mar 23 '10 at 06:22
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
Pretty straight forward with Docmosis - place a marker image, book mark it and tell docmosis to process the document replacing the image.

- 6,513
- 2
- 24
- 19
If the document doesn't exist and you want to create from scratch, use this:
http://code.google.com/p/java2word
else: Apache PIO

- 1,001
- 1
- 13
- 15