Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PrintText {
- public static void main(String[] args) throws PrintException, IOException {
- String defaultPrinter =
- PrintServiceLookup.lookupDefaultPrintService().getName();
- System.out.println("Default printer: " + defaultPrinter);
- PrintService service = PrintServiceLookup.lookupDefaultPrintService();
- // prints the famous hello world! plus a form feed
- InputStream is = new ByteArrayInputStream("hello world!f".getBytes("UTF8"));
- PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
- pras.add(new Copies(1));
- DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
- Doc doc = new SimpleDoc(is, flavor, null);
- DocPrintJob job = service.createPrintJob();
- PrintJobWatcher pjw = new PrintJobWatcher(job);
- job.print(doc, pras);
- pjw.waitForDone();
- is.close();
- }
- }
- 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) {
- }
- }
- }
- Default printer: EPSON TM-T81 Receipt
- Printing done ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement