Print a file and check status using Java - Full Java Code
Print a file and check status using Java - Full Java Code
This article is part of a tutorial.
Tutorial Index page - Java Printing
/** * $Id: PrintAndwait.java 102 2010-03-14 00:15:27Z oneyour $ * * This is an accompanying program for the article * <a href="http://www.1your.com/drupal/fullcodeprintandcheckstatus " title="http://www.1your.com/drupal/fullcodeprintandcheckstatus ">http://www.1your.com/drupal/fullcodeprintandcheckstatus </a> * * Copyright (c) 2009 - 2010 <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a>. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a> nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; 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 String printJobStatus = null; /** * */ public PrintAndwait() { } /** * Request and read input from the command line (System.in) * * @param inputMessage * The message to be displayed as a request to the user * * @return * The input from the command line * * @throws IOException * Any problems while reading the input */ public static String readCommandLineInput(String inputMessage) { System.out.println(inputMessage); Scanner scanner = new Scanner(System.in); String inputLine = scanner.nextLine(); return inputLine; } public static void main(String[] args) { // Request and read user input String sInstruction = "Enter path of the File to print: \n"; String filePath = readCommandLineInput(sInstruction); //Create a new File instance by converting the given pathname string into an abstract pathname. File oFile = new File(filePath); // Check if the file exists boolean bFileExists = oFile.exists(); if(!bFileExists) { // Inform the user that the file does not exist System.out.println("File does not exist \n"); } else { PrintAndwait printAndWait = new PrintAndwait(); String status = printAndWait.printAndWait(filePath); System.out.println(status); } } public String printAndWait(String filePath) { // Input the file FileInputStream textStream = null; try { textStream = new FileInputStream(filePath); } 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."; } }
Feedback or Questions?
We welcome feedback and questions and will try our best to attend to it as quickly as possible!
Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.
