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 java.io.*;
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) {
   e.printStackTrace();
  }  

  //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)
  is.close();
  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.

12 comments:

bestworldstuff 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...

@Chowdary:

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

Noble Mathew said...

hai
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.

france pope said...

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

the good work.

print services peabody ma

sarath J Nair said...
This comment has been removed by the author.
sarath J Nair said...

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

Muhamamad Waqeel said...

I like your blog content and article
keep blogging :)
http://www.softwares4pk.blogspot.com
http://www.itechsea.blogspot.com

Edwin Chui 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.

Related Posts with Thumbnails