wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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);

fileOutput.print(logoLabel);

but this, of course, doesn't work.


Another guess is:
BufferedImage image = ImageIO.read(new 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?

Thanks
Ppr
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.

Layne


Java API Documentation
The Java Tutorial
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 114
    
  10
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.

Cheers,

Cay


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

Joined: Sep 10, 2004
Posts: 755
Cay,

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?

thanks
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.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to attache a picture to a Word Doc File?
 
Similar Threads
Converting a Word doc into a Gui.
Saving files in .txt format using JFileChooser.
first JavaRanch question
Testing File to see if it Exists, and writing meta data
PrintWriter vs FileWriter