aspose file tools*
The moose likes Beginning Java and the fly likes Direct Printing a text file by default printer without user interaction. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Direct Printing a text file by default printer without user interaction." Watch "Direct Printing a text file by default printer without user interaction." New topic
Author

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

vishal prasad
Greenhorn

Joined: Aug 29, 2011
Posts: 4
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

Joined: Jul 20, 2011
Posts: 49

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.


Knowledge Reigns Supreme
vishal prasad
Greenhorn

Joined: Aug 29, 2011
Posts: 4
Will you mind sharing it ! or at least the logic
It will be a great help!
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

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

Joined: Oct 27, 2005
Posts: 19556
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

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

Joined: Oct 27, 2005
Posts: 19556
    
  16

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

Joined: Aug 29, 2011
Posts: 4
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

Joined: Jul 20, 2011
Posts: 49

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

Joined: Aug 29, 2011
Posts: 4
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Direct Printing a text file by default printer without user interaction.
 
Similar Threads
Error printing out from a read in file.
Reading one line from a text
Regarding reading a file in a jsp
command line arguments for a file
to read lines inside brackets