1

My Java application generates a document in DOCX format using DocX4J. I need to send it into clipboard to be pasted in Word. I know that Word will consume HTML, but I rather not convert DOCX to HTML (I am not sure if DocX4J supports it and I rather not loose any formatting). What clipboard format (in Java terms DataFlavor) can I use to send DOCX data to clipboard so Word will understand it?

I am doing a similar thing with OpenOffice document and for that I use

DataFlavor odtFlavor = new DataFlavor("application/x-openoffice-embed-source-xml;"+
    "representationclass=java.io.InputStream");

How should I represent the DOCX document itself? In case of OpenOffice ODT I pass the InputStream created from the ODT file.

I believe a similar question has been asked by David Thielen here: What are the clipboard formats for Microsoft Office where you can drop a chart? but there are no answers.

4
  • Can you explain why you use the clipboard?
    – Wivani
    Sep 2, 2011 at 12:46
  • The function I want to implement is copying/pasting or dragging and dropping data from my application into reports that users write. Users write reports in MS Word or OO Writer. I think copy/paste is a very intuitive way of transferring data from one application to another.
    – Alla
    Sep 2, 2011 at 20:52
  • Looks to me like you'd have to use the appropriate mime type. See here for the Word/Office ones.
    – Wivani
    Sep 5, 2011 at 9:29
  • @Wivani, I tried using "application/vnd.openxmlformats-officedocument.wordprocessingml.document;representationclass=java.io.InputStream" - that does not work. Word does not pick it up.
    – Alla
    Sep 15, 2011 at 8:51

1 Answer 1

2

Worst case, docx4j can export to HTML, so you could do that.

Or you could use RTF. docx4j uses FOP to create PDF, so you could use the XSL FO output to create RTF (FOP can do that - your mileage may vary).

Not sure which of these will give you better quality. Possibly the HTML (though what happens to images?).

Or you could make a basic docx to RTF converter.

There may be a way to use the docx format.

If you copy from Word, and look at it in ClipSpy (binary available in the source download from CodeProject), you'll see "Embed Source" is the data as a docx in OLE.

how to reload saved "Embed Source" clipboard data? says you can write your own "Embed Source" by passing Clipboard.SetData a stream object

Seems to depend whether you want to copy/paste or drag/drop though. See further your link What are the clipboard formats for Microsoft Office where you can drop a chart? and http://social.msdn.microsoft.com/Forums/en/worddev/thread/84263fb9-61c2-424a-a294-a12f69fd6b1b

1
  • Hi @plutext. Thanks for your suggestions. Exporting to HTML and using HTML as clipboard format mostly works for me, except that HTML conversion is rather slow (up to two seconds). This makes it unreliable in copy-paste, because user can do Ctrl+C, Alt-Tab, Ctrl+V quite quickly.
    – Alla
    Sep 15, 2011 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.