0

I have to print a word document from java. I could just open it and print it. But the following code print it automatically. Is there any way to pop up a print dialogue to choose printer? If the user does not wish to print it, he should be possible to cancel it. Also I need to close the word after printing. Please help me.

public static void main(String args[]) throws IOException {
   Desktop desktop = Desktop.getDesktop();
   File f = new File("C:\\Users\\asa\\Desktop\\resume.doc");
   desktop.open(f);
   Thread.sleep(5000);          
   desktop.print(f);
} 
Sarika.S
  • 7,366
  • 28
  • 76
  • 108
  • 1
    http://stackoverflow.com/questions/7336340/how-to-print-excel-file-and-word-document-in-java – PeterMmm Jul 18 '12 at 10:28
  • 1
    Read this: http://stackoverflow.com/questions/2446721/how-to-get-print-out-of-a-ms-word-file-from-java-application?rq=1 – lbalazscs Jul 18 '12 at 10:29

2 Answers2

3

Have you tried using Desktop#print(File file) method?

ioreskovic
  • 5,310
  • 5
  • 36
  • 67
  • Oh.. That is great!! But one problem is now the opened doc contains nothing. I got printout correctly. – Sarika.S Jul 18 '12 at 10:42
  • You mean the printer prints an empty page? Also, a tip: File f = new File("C:/Users/asa/Desktop/resume.doc"); It will be resolved to the separator used on your system (`\\\` in your case). – ioreskovic Jul 18 '12 at 10:50
  • no. prints correctly. but on desktop, word is open. but.. no content.. I don't know how to say it. :( – Sarika.S Jul 18 '12 at 10:52
  • 1
    Feel free to edit your question and elaborate the problem further :D – ioreskovic Jul 18 '12 at 10:53
  • One problem fixed when I put thread.sleep(). I have edited the question with new requirement. :D Many thanks Lopina.. – Sarika.S Jul 18 '12 at 11:06
1

Try the Java print service API.

Tutorial here: http://docs.oracle.com/javase/tutorial/2d/printing/services.html

Lucas Arrefelt
  • 3,849
  • 4
  • 40
  • 69