Results 1 to 2 of 2
Thread: [Java Printing]
- 10-29-2007, 04:52 PM #1Member
- Join Date
- Oct 2007
- Rep Power
I'm developing a printing functionality using Java Print Service API.
I found the way to send escape codes to the printer, but I don't know how to retrieve a printer feedback or how to ask to the printer if it has paper, and other function issues like lost of communication...
I use the javax.print.event but just the events printDataTransferCompleted and printJobNoMoreEvents works fine. Even when I turn off the printer, disconnect the cable, let it without paper, no other event is fired.
Does anybody know how to get this kind of response from a printer using java?
Thank you very much for any help
- 11-08-2007, 03:28 PM #2Member
- Join Date
- Nov 2007
- Rep Power
Last time I had a similar requirement to be done in a web application.
But I couldn't find on how to detect the connection or I/O problem from the printer. But I think the code was able to detect some of the events that you’ve mentioned.
I used javax.print.DocPrintJob and implement its listener to return a flag whenever the corresponding event occurs upon printing.
There are some methods that you would want to implement. But it may need to check whether one of the methods is supported by the printer.
You may want to use javax.print.attribute.* classes in order to know the properties of your printer.
Here is the chunk of the code:
static class PrintJobWatcher
boolean done = true;
public void printJobCanceled(PrintJobEvent pje)
log.debug("Printing job was cancelled.");
public void printJobFailed(PrintJobEvent pje)
log.error("Printing job failed.");
public void printJobRequiresAttention()
log.error("Rectifiable problem occured (e.g. printer out of paper).");
done = false;
And here the printing watcher is registered:
InputStream in = new FileInputStream( <<File instance>> );
in = new BufferedInputStream(in);
AttributeSet aset = new HashAttributeSet();
aset.add( new PrinterName( <<Printer name>>, null ) );
PrintService printers = PrintServiceLookup.lookupPrintServices( null, aset );
UAssert.check( printers.length > 0, "No printer found." );
//to use the 1st printer
PrintService service = printers;
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(in, flavor, null);
DocPrintJob pj = service.createPrintJob();
PrintJobWatcher prWatcher = new PrintJobWatcher(pj);
PrintRequestAttributeSet prAset = new HashPrintRequestAttributeSet();
- By coldblood22 in forum AWT / SwingReplies: 1Last Post: 04-05-2008, 02:43 PM
- By Java Tip in forum Java TipReplies: 0Last Post: 02-04-2008, 09:36 AM
- By Mahendra in forum Java 2DReplies: 0Last Post: 01-23-2008, 12:45 PM
- By ramachandran in forum Advanced JavaReplies: 1Last Post: 01-05-2008, 10:16 AM
- By ntpl in forum AWT / SwingReplies: 0Last Post: 11-27-2007, 11:20 AM