Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Direct Printing a text file by default printer without user interaction.

 
vishal prasad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone!

I want to print a text file directly to the default printer installer/set by the OS.
Actually i made raw printing work using the code below it works fine.

But i have some indian local language in the text file which comes as junk.
So i need to print text file via printer driver without user interaction.
I have also tried java.awt.print.PrinterJob class the code is able to print text file but i get a pint dialog.
what i really need is to print text file via printer driver without user interaction.
Is there any api or workaround to do so!
Any help/ advice is greatly appreciated !
Thank You in anticipation!
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program thats printing without user-dialog based on the classes

java.awt.print.PrinterJob;
javax.print.PrintService;

but i only know it works on Windows-Systems, not exactly sure how platform independent it is.
 
vishal prasad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you mind sharing it ! or at least the logic
It will be a great help!
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont mind. I cant take credit for the code either, i found it and used it with minor modifications.
You might need to figure out what index the printer you want to use has on your system (line 41)
or if theres a way to always get the default-printer.

Here goes:

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Avadis wrote:

And what if you have less than 8 print services?
You're close though - PrintServiceLookup has methods getDefaultPrintService() and lookupDefaultPrintService(). The latter is static so you can easily use it. Just be aware that it may return null.
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The index '7' in there is what i needed to get MY desired printer at MY network.
('0' for me was windows XPS-Printer, '1' a PDF-Printer, and so on).
So you have to replace it with whatever index-number your printer turns out to be.
That comment i made there was misleading, sorry.

Thanks for the tipps about default printer methos Rob!

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't count on a number, even if you want one specific printer. What if you add a printer and it gets inserted before index 7? I'd instead get all the PrintService objects and loop through them and get the one you want based on the name.
 
vishal prasad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code. And others for their valuable advice.
The code works fine but i am still getting the printer dialog box to print.
Basically i want to use silent printing.
I am still searching for an api to print text file silently to printer.
i am going through my options right now. How about converting my text file to pdf and
use jpedal,iText or something similar API to print silently. Let me know what you people think.

Regards
Vishal
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm im running this here on Windows XP and its completely silent, no dialog whatsoever.
I run the program, it terminates, and 2 sec later i hear the printer going off.
 
vishal prasad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry! your code works fine in windows.
Actually my default printer was set as pdf printer
i too got it working in windows. However in LINUX it still
pop a dialog. Now working to findout why so?
will post the solution one i get it for linux
meanwhile if anyone gets any thing on silent printing in java
kindly reply! Dont close the thread yet!
Thanks everyone!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic