tags:

views:

3490

answers:

8

Anyone know if it is possible? And got any sample code for this? Or any other java API that can do this?

A: 

As far as can be gathered from the project website: no.

Confusion
+1  A: 

The Office 2007 format is based on XML and so can probably be written to using XML tools. However there is this library which claims to be able to write DocX format word documents.

The only other alternative is to use a Java-COM Bridge and use COM to manipulate word. This is probably not a good idea though - I would suggest finding a simpler way.

For example, Word can easily read RTF documents and you can generate .rtf documents from within Java. You don't have to use the Microsoft Word format!

Phill Sacre
A: 

POI's HWPF can extract an MS Word document's text and perform simple modifications (basically deleting and inserting text). AFAIK it can't do much more than that. Also keep in mind that HWPF works only with the older MS Word (97) format, not the latest ones.

Rui Vieira
A: 

Not sure if Java out of the box can do it directly. But i've read about a component that can pretty much do anything in terms of automating word document generation without having Word. Aspose Words

Conrad
+1  A: 

As others have said POI isn't going to allow you to do anything really fancy - plus it doesn't support Office 2007+ formats. Treating MS Word as a component that provides this type of functionality via COM is most likely the best approach here (unless you are running on a non-Windows OS or just can't guarantee that Word will be installed on the machine).

If you do go the COM route, I recommend that you look into the JACOB project. You do need to be somewhat familiar with COM (which has a very steep learning curve), but the library works quite well and is easier than trying to do it in native code with a JNI wrapper.

Kevin Day
A: 

If you are using docx, you could try docx4j.

See the AddImage sample

plutext
A: 

JasperReports uses this API alternatively to POI, because it supports images:
JExcelAPI

I didn't try it yet and don't know how good/bad it is.

Stroboskop
A: 

Surely:

Take a look at this: http://code.google.com/p/java2word

Word 2004+ is XML based. The above framework gets the image, convert to Base64 representation and adds it to the XML. When you open your Word Document, there will be your image.

Simple like this:

    IDocument myDoc = new Document2004();   
    myDoc.getBody().addEle("path/myImage.png"));

Java2Word is one API to generate Word Docs using obviously Java code. J2W takes care of all implementation and XML generation behind the scenes.

Leonardo