Full Code Listing - Print a file and check status

A simple article that provides a working code that will print a text file to the default printer and wait till it get a status back from the Printing System.

This example requires a text file named "fileToPrint.txt" present in the classpath which is the file this program will print.

/**
 * PrintAndwait.java
 * Copyright 1your.com
 */

package com.oneyour.print;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

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.PrintJobEvent;
import javax.print.event.PrintJobListener;

/**
 *
 */

public class PrintAndwait implements PrintJobListener
{
    private final static String fileToPrint = "fileToPrint.txt";
    private String printJobStatus = null;
   
    /**
     *
     */

    public PrintAndwait()
    {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args)
    {
        PrintAndwait printAndWait = new PrintAndwait();
        String status = printAndWait.printAndWait();
        System.out.println(status);
    }
   
    public String printAndWait()
    {
        // Input the file
        FileInputStream textStream = null;
        try
        {
            textStream = new FileInputStream(fileToPrint);
        }
        catch (FileNotFoundException exception)
        {
            exception.printStackTrace();
            return ("Could not find the file -" + exception.getMessage());
        }
       
        // Set the document type
        DocFlavor myFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
       
        // Create a Doc
        Doc myDoc = new SimpleDoc(textStream, myFormat, null);
       
        // Build a set of attributes
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(new Copies(1));
       
        // discover the printers that can print the format according to the
        // instructions in the attribute set
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
       
        // Create a print job from one of the print services
        if (service != null)
        {
            DocPrintJob job = service.createPrintJob();
            job.addPrintJobListener(this);
            try
            {
                job.print(myDoc, aset);
               
                // Wait till we know the status of our print job
                while(printJobStatus == null)
                {
                    // Wait for 1 second before checking to see if the job is complete.
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException ignore)
                    {
                        ignore.printStackTrace();
                    }
                }
               
            }
            catch (PrintException exception)
            {
                exception.printStackTrace();
                return ("Problems printing the file. -" + exception.getMessage());
            }
        }
       
        return printJobStatus;
    }
   
    @Override
    public void printJobCompleted(PrintJobEvent pje)
    {
        printJobStatus = "Printing successful";
    }
   
    public void printJobNoMoreEvents(PrintJobEvent pje)  
    {
        printJobStatus = "Printing may be successful but no errors reported.";
    }

    @Override
    public void printDataTransferCompleted(PrintJobEvent pje)
    {
    }

    @Override
    public void printJobCanceled(PrintJobEvent pje)
    {
        printJobStatus = "Print job cancelled.";
    }

    @Override
    public void printJobFailed(PrintJobEvent pje)
    {
        printJobStatus = "Print job failed.";
    }

    @Override
    public void printJobRequiresAttention(PrintJobEvent pje)
    {
        printJobStatus = "Print job requires attention.";
    }
 
}

 

 

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Full Code Listing - Print a file and check status | Our website now yours! - Currenlty Java focussed.

Full Code Listing - Print a file and check status

A simple article that provides a working code that will print a text file to the default printer and wait till it get a status back from the Printing System.

This example requires a text file named "fileToPrint.txt" present in the classpath which is the file this program will print.

/**
 * PrintAndwait.java
 * Copyright 1your.com
 */

package com.oneyour.print;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

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.PrintJobEvent;
import javax.print.event.PrintJobListener;

/**
 *
 */

public class PrintAndwait implements PrintJobListener
{
    private final static String fileToPrint = "fileToPrint.txt";
    private String printJobStatus = null;
   
    /**
     *
     */

    public PrintAndwait()
    {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args)
    {
        PrintAndwait printAndWait = new PrintAndwait();
        String status = printAndWait.printAndWait();
        System.out.println(status);
    }
   
    public String printAndWait()
    {
        // Input the file
        FileInputStream textStream = null;
        try
        {
            textStream = new FileInputStream(fileToPrint);
        }
        catch (FileNotFoundException exception)
        {
            exception.printStackTrace();
            return ("Could not find the file -" + exception.getMessage());
        }
       
        // Set the document type
        DocFlavor myFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
       
        // Create a Doc
        Doc myDoc = new SimpleDoc(textStream, myFormat, null);
       
        // Build a set of attributes
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(new Copies(1));
       
        // discover the printers that can print the format according to the
        // instructions in the attribute set
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
       
        // Create a print job from one of the print services
        if (service != null)
        {
            DocPrintJob job = service.createPrintJob();
            job.addPrintJobListener(this);
            try
            {
                job.print(myDoc, aset);
               
                // Wait till we know the status of our print job
                while(printJobStatus == null)
                {
                    // Wait for 1 second before checking to see if the job is complete.
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException ignore)
                    {
                        ignore.printStackTrace();
                    }
                }
               
            }
            catch (PrintException exception)
            {
                exception.printStackTrace();
                return ("Problems printing the file. -" + exception.getMessage());
            }
        }
       
        return printJobStatus;
    }
   
    @Override
    public void printJobCompleted(PrintJobEvent pje)
    {
        printJobStatus = "Printing successful";
    }
   
    public void printJobNoMoreEvents(PrintJobEvent pje)  
    {
        printJobStatus = "Printing may be successful but no errors reported.";
    }

    @Override
    public void printDataTransferCompleted(PrintJobEvent pje)
    {
    }

    @Override
    public void printJobCanceled(PrintJobEvent pje)
    {
        printJobStatus = "Print job cancelled.";
    }

    @Override
    public void printJobFailed(PrintJobEvent pje)
    {
        printJobStatus = "Print job failed.";
    }

    @Override
    public void printJobRequiresAttention(PrintJobEvent pje)
    {
        printJobStatus = "Print job requires attention.";
    }
 
}

 

 

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options