Share this page 

Print a text file using the javax.print APITag(s): IO


This example will print a given text file using the javax.print API.
  • With cheap personal printer (at least with mine!), you cannot select many options... more than one copy and page orientation (portrait or landscape) won't work.
  • You need to set the "flavor" as AUTOSENSE so the content is sent as "OCTET-STREAM" even if it's possible, according to the Javadoc, to set the "flavor" as UTF8 or US_ASCII. I believe this is a limitation of the Windows platform implementation.
  • You need to send a FORMFEED between each print job to eject the last page.
  • A special class, PrintJobWatcher, is used to wait for the completion of a print job.
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.File;
    import java.io.FileInputStream;
    
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
    
    import javax.print.event.PrintJobAdapter;
    import javax.print.event.PrintJobEvent;
    
    public class PrintTextFile {
    
      public static void main(String[] args) throws PrintException, IOException {
        String defaultPrinter =
          PrintServiceLookup.lookupDefaultPrintService().getName();
        System.out.println("Default printer: " + defaultPrinter);
    
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    
        FileInputStream in = new FileInputStream(new File("c:/temp/foo.txt"));
    
        PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));
    
    
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc doc = new SimpleDoc(in, flavor, null);
    
        DocPrintJob job = service.createPrintJob();
        PrintJobWatcher pjw = new PrintJobWatcher(job);
        job.print(doc, pras);
        pjw.waitForDone();
        in.close();
    
        // send FF to eject the page
        InputStream ff = new ByteArrayInputStream("\f".getBytes());
        Doc docff = new SimpleDoc(ff, flavor, null);
        DocPrintJob jobff = service.createPrintJob();
        pjw = new PrintJobWatcher(jobff);
        jobff.print(docff, null);
        pjw.waitForDone();
      }
    }
    
    class PrintJobWatcher {
      boolean done = false;
    
      PrintJobWatcher(DocPrintJob job) {
        job.addPrintJobListener(new PrintJobAdapter() {
          public void printJobCanceled(PrintJobEvent pje) {
            allDone();
          }
          public void printJobCompleted(PrintJobEvent pje) {
            allDone();
          }
          public void printJobFailed(PrintJobEvent pje) {
            allDone();
          }
          public void printJobNoMoreEvents(PrintJobEvent pje) {
            allDone();
          }
          void allDone() {
            synchronized (PrintJobWatcher.this) {
              done = true;
              System.out.println("Printing done ...");
              PrintJobWatcher.this.notify();
            }
          }
        });
      }
      public synchronized void waitForDone() {
        try {
          while (!done) {
            wait();
          }
        } catch (InterruptedException e) {
        }
      }
    }
    
    To print a String, see this HowTo