Monday, October 18, 2010

How to Print from Java (JPS) - javax.print Package

javax.print package is available in JDKs 1.4 and above. Formerly Java AWT printing API was used to perform basic printing and now it is enhanced with advanced options like printer service discovery, in javax.print API.

I am posting a simple example showing how to perform a basic print using javax.print API.

import javax.print.*;

public class PrintTest {
 public static void main(String[] args) throws IOException {
  //we are going to print "printtest.txt" file which is inside working directory
  File file = new File("printtest.txt");
  InputStream is = new BufferedInputStream(new FileInputStream(file));
  //Discover the default print service. If you call PrintServiceLookup.lookupPrintServices
  //then it will return an array of print services available and you can choose a
  //printer from them
  PrintService service = PrintServiceLookup.lookupDefaultPrintService();
  //Doc flavor specifies the output format of the file (Mime type + Char encoding)
  //You can retrieve doc flavors supported by the printer, like this
  //DocFlavor [] supportedFlavors = service.getSupportedDocFlavors();
  DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;  
  // Create the print job
  DocPrintJob job = service.createPrintJob();
  //Create the Doc. You can pass set of attributes(type of PrintRequestAttributeSet) as the 
  //3rd parameter specifying the page setup, orientation, no. of copies, etc instead of null. 
  Doc doc = new SimpleDoc(is, flavor, null);

  //Order to print, (can pass attributes instead of null)
  try {
   job.print(doc, null);
  } catch (PrintException e) {

  //DocPrintJob.print() is not guaranteed to be synchronous. So it's better to wait on completion
  //of the print job before closing the stream. (See the link below)
  System.out.println("Printing done....");


This example shows how to create a Print job watcher. You can create an object from PrintJobWatcher in your PrintTest class and call waitForDone() before closing the stream.

You can find documentation on javax.print API here.

The DocFlavor I have used in this example (INPUT_STREAM.TEXT_PLAIN_US_ASCII) wont work on Windows. The PrintService doesn't discover this flavor as a supported one for some reason. Yes, this Java Print Service facility is still buggy (in JDK 1.6). It is weird that we can't print ASCII formatted files while we can print any JPEG or PNG.  I used INPUT_STREAM.AUTOSENSE flavor which uses mime type "application/octet-stream", to print on Windows. Then the printer will print just the octets coming from the stream.However it works fine.


Anonymous said...

Thanks for the info i searched this a lot

RB Chowdary said...

so, we can't implement this in windows..........right....?

ravindra said...

Hi Kalani
This is Ravi, is it possible to same code in Applet, I tried this in Eclipse, In Eclipse when I run it as a Applet it's executing. But when i executing in Browser it's not coming....!

Can you please help me out from this..............!

Thank You..

kalani Ruwanpathirana said...


The doc flavor TEXT_PLAIN_US_ASCII won't work on Windows but you can use other flavors on Windows.

@ravindra: I am not sure why it doesn't run on browser if it runs in Eclipse. Are applets turned on?

Ouail said...

I realy can't thanks you about this , you are realy amazing , thanks a lot

Unknown said...

can use it in a roll paper printer with out wasting extra paper.
I am developing an application to print bill. Everything works fine but the problem is after printing what we need, printer advances. I use java.awt.print.

Unknown said...

Nice blog, thanks for sharing the information. I will come to look for update. Keep up

the good work.

print services peabody ma

DS&A said...
This comment has been removed by the author.
DS&A said...

Please tell me how to get the default printer of client system

Unknown said...

I've implemented this solution but I've got an error printing the character Ñ in my reports. ¿Is there a way I can print this kind of characters?

Nassau-Suffolk Blueprinting Company said...

We offer blueprinting services with highest quality results. As a services provider we take pride in the work we deliver and develop relationships with our clients.We use wholesale paperto provide quality prints and ensure that the print products go beyond the expectations.

Bala said...


I am getting print document as a blank page , Can you please help me, I am using the javad 1.8.0_191, printer : Microsoft Print to PDF, I am searching a lot to fix the issue. There is no errors in the console.

technologyforall said...

Thank you for sharing this post, I really enjoyed reading every single word.

Data Science can be interpreted as an advanced application of Computer Science which has been specially designed to deal with the data analytics applications. By making use of advanced tools and algorithms, Data Science has the power to mine & extract valuable insights which are encrypted inside the data. Thereby, uncovering the hidden patterns & information from the data has become a lot easier. This Data Science Course Training In Hyderabad will help the students gain real-world hands-on insights for handling the real-time industry challenges in this Data Science domain.

For More info Please Visit Our Site or else feel free to Call/WhatsApp us on +91-9951666670 or mail at

Data Science Course in Hyderabad

Ramesh Sampangi said...

Become a data science expert by joining AI Patasala’s Data Science Course in Hyderabad program, where you can learn data science concepts with practical knowledge.
Data Science Training in Hyderabad
Data Science Course in Hyderabad

Related Posts with Thumbnails