File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes how to attache a picture to a Word Doc File? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to attache a picture to a Word Doc File? " Watch "how to attache a picture to a Word Doc File? " New topic

how to attache a picture to a Word Doc File?

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi guys,

I have a FileWrite that write the uses input into a word document.
I wonder how can I add a picture (logo) to the document?

My first guess was:

Icon logoIcon = new ImageIcon("images/logo.jpg");
JLabel logoLabel = new JLabel(stampIcon);

FileWriter writeDoc = new FileWriter(filename);
PrintWriter fileOutput = new PrintWriter(writeDoc);


but this, of course, doesn't work.

Another guess is:
BufferedImage image = File("images/stamp.jpg"));

but I really don't knwo how to use it?

does anyone have a code sample to show how this should work?

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm not exactly sure what you are asking to do. From the title it looks like you are trying to create a file that can be opened in Microsoft Word. These files typically have a ".doc" extension. Are you trying to insert an image into a ".doc" file? According to the API docs, PrintWriter is used to output text in a formatted way. Images are not considered text; they are stored as binary data. ".doc" files are NOT text, either. So the first step is to find out what the binary format is for a ".doc" file. Unfortunately, I don't know the technical details. In fact, Microsoft is typically tight-mouthed about releasing them. However, you might be able to find this information somewhere on the Web. Perhaps someone over at I/O and Streams will be able to help more.


Java API Documentation
The Java Tutorial
Cay Horstmann
Ranch Hand

Joined: Nov 14, 2004
Posts: 137
There is nothing in the Java API that lets you do this. You would need to know how a word file is formatted (and no, Microsoft won't tell you). Then you would need to write a program that produces those binary codes, both for the text and the image.

Why does it have to be a Word file? Could you produce HTML? A Word user could always import the HTML into Word.



Author of Java 8 for the Really Impatient
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755

HTNL - even better.

Word doc is my final document, but I can compromise on HTML.

infact, I also need to attach it to an email.

so, do you have any code showing how to attach to HTML?

somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
In my project, i'm use Apache POI to create Word Document, it easy but not good.

And other solution you can create html file instead word file because html file is a textfile only.

Java Developer, Thailand
I agree. Here's the link:
subject: how to attache a picture to a Word Doc File?
It's not a secret anymore!