File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Print Word Document Programatically in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Print Word Document Programatically in Java" Watch "Print Word Document Programatically in Java" New topic
Author

Print Word Document Programatically in Java

Rohit Kedia
Ranch Hand

Joined: Apr 24, 2010
Posts: 78
Hi ,

I want to Print Word Document Programatically.

My Word Document will be in Database in a BLOB Column and I want to send that to the Printer how Can I Do that.

The Below program is printing the Word Document silently but it is coming in unformatted way.

Please Help

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2407
Some printer/driver combinations understand PDF files, but I'm not aware of any that know what to do with Word files. Furthermore, it looks like you're treating the contents of the file as text (which it is not) instead of binary (which its is).
Rohit Kedia
Ranch Hand

Joined: Apr 24, 2010
Posts: 78
Not Sure I quite understand what you are saying.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

Word documents have special characters in them that mean special things. In addition to what the text is, you need to know if the text should be bold, centered, italic, what font it is, what size, etc.

That data is written in the .doc file in a certain way. You need to know what those special codes are and how to interpret them. Try opening a .doc file in notepad, and you'll see a lot of gibberish. That's because notepad doesn't know how to read all that special stuff.

If you want to see all that, you'll need to figure out how to interpret the file. There may be 3rd party software packages you can get and use.


Never ascribe to malice that which can be adequately explained by stupidity.
Rohit Kedia
Ranch Hand

Joined: Apr 24, 2010
Posts: 78
Hi ,

Can you suggest anything.

If you want to see all that, you'll need to figure out how to interpret the file. There may be 3rd party software packages you can get and use.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4734
    
    7

Rohit Kedia wrote:My Word Document will be in Database in a BLOB Column and I want to send that to the Printer how Can I Do that.

I doubt that there's a 100% solution, but JODConverter might be a place to start. Apache POI may also be able to do it but, from what I understand, its HWPF (Word) component is quite limited. Otherwise, there's always the OpenOffice SDK, but I suspect it'll involve quite a learning curve. There's also a product called docx4j, but I've never used it and have no idea how good it is.

It's also possible that you'll need a different solution for .doc and .docx files.

Finally, there are commercial programs, including (possibly) Word itself; although MS's own batch-style automators are either (a) abysmal, or (b) cost an arm and a leg.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

If you have MS Word installed, you can do it quite easily:
- store the BLOB in a temporary file (File.createTempFile)
- use java.awt.Desktop and its print(File) method.
- delete the file.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2407
Winston Gutkowski wrote:Apache POI may also be able to do it but, from what I understand, its HWPF (Word) component is quite limited.

POI would help somewhat with extracting text and layout information from a document, but it has no provisions for printing. Either JODConverter or Desktop.print look more promising.
Rohit Kedia
Ranch Hand

Joined: Apr 24, 2010
Posts: 78
POI would help somewhat with extracting text and layout information from a document, but it has no provisions for printing. Either JODConverter or Desktop.print look more promising.


Can you share me some code snippets.

I had an alternate Solution to this Problem but I needed some assistance.

I will fetch all the BLOBS from Database and create a Word Document from it, is that possible ?
Also, I will be having multiple Blobs [different Word Files] and I want to put them Together in One Word Document , is that Possible ?
 
I agree. Here's the link: jrebel
 
subject: Print Word Document Programatically in Java
 
Similar Threads
Invoking printer using java
Print a JtextArea
To print a document
Printing Database Records to an external printer/paper?
What is the problem