Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to attache a picture to a Word Doc File?

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic