views:

2415

answers:

4

I have a J2EE based web application, in which on clicking a button I need to create a word file from Java. I want to be able to sent the printing command to this file, so that the file is being printed without the user having to open the document and do it manually.

Could anyone please tell me if this is possible and if so how to proceed??

Thanks in advance.

A: 

It is easy to generate the file. Take a look at Apache POI, a Java API To Access Microsoft Format Files. The site is plenty of examples.

About printing it, I don't really know if that's possible. I think you always need to open it in the client side in order to print it.

Guido
+1  A: 

You have to create an ActiveX to load the doc content and fire the print command. There is no other way to open a file in the client computer and fire comands.

EDIT: If you can use HTML, you can just do

<script>
function load() {
window.print();
window.close();
}
</script>

and

<body onLoad="load()" ...>

on a popup window to open the document and print it. And then close the popup.

Leonel Martins
I have actually tried this out but i felt like the html doc window that pops up may be annoying...is there any way to avoid the window from popping up...not sure if my question makes any sense..
ria
its ugly but you can define the size and place (coords of the screen) when you open an popup window. try open it outside the screen (like x = -200 and y = -200, or bigger if the window is bigger. i dont know if you use a small window, the browsers will print the content right.)
Leonel Martins
A: 

As you are specifically mentioning Java as your environment you might want to take OpenOffice or StarOffice into account - they have an API that's a lot easier to talk to from Java than ActiveX or remote-controlling Word.

It's no more heavyweight than Word is. References and docs should be easy to find.

I've worked in a company where we used Remote-controlled-Word a lot and finally switched to StarOffice. This wasn't for printing but for document conversion (e.g. from Word to HTML), but should be sufficiently similar.

Of course I'm talking "old versions of Word" but we usually had the problem of Word locking up with some arbitrary dialog requesting to confirm whatever Word has found - an operation that our server (running in the background with no Desktop contact) obviously was not able to. This went a lot better after switching to StarOffice.

Olaf
A: 

Would it be possible to render the document in HTML instead? If you could do that then you could allow the users to print via the browser fairly simply. You might also have an easier time with PDF's, at the very least it would be more accessible across different platforms.

If you're forced to use MS Word then you're going to be very limited in what you can do. As Leonel mentioned, I think ActiveX is going to be your only choice and even then the document would have to be opened, you just might be able to launch Word from the browser automatically. You might even be able to embed an instance of Word into IE via ActiveX, but I'm not 100% sure about that.

Mike Deck
Thanks for the input.The format of rendering the document doesnt much matter...all thats needed is the document which ever format it is in must be sent to the printer without popping up even if its an HTML or word or pdf. Is this possible with HTML document?
ria