Example usage for javax.print DocPrintJob addPrintJobListener

List of usage examples for javax.print DocPrintJob addPrintJobListener

Introduction

In this page you can find the example usage for javax.print DocPrintJob addPrintJobListener.

Prototype

public void addPrintJobListener(PrintJobListener listener);

Source Link

Document

Registers a listener for event occurring during this print job.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    StreamPrintService service = factories[0].getPrintService(fos);

    DocPrintJob job = service.createPrintJob();
    job.addPrintJobListener(new MyPrintJobListener());
}

From source file:OneFourDialog.java

public static void main(String args[]) throws Exception {
    String filename = args[0];/*  ww  w  .  j a va2s .c o m*/
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService,
            DocFlavor.INPUT_STREAM.GIF, pras);
    if (service != null) {
        DocPrintJob job = service.createPrintJob();
        PrintJobListener listener = new PrintJobAdapter() {
            public void printDataTransferCompleted(PrintJobEvent e) {
                System.exit(0);
            }
        };
        job.addPrintJobListener(listener);
        FileInputStream fis = new FileInputStream(filename);
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis, flavor, das);
        job.print(doc, pras);
        Thread.sleep(10000);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String filename = args[0];// ww  w  .j a  v  a  2  s.  c om
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    DocPrintJob job = printService.createPrintJob();
    PrintJobListener listener = new PrintJobAdapter() {
        public void printDataTransferCompleted(PrintJobEvent e) {
            System.out.println("Good-bye");
            System.exit(0);
        }
    };
    job.addPrintJobListener(listener);
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    FileInputStream fis = new FileInputStream(filename);
    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(fis, flavor, das);
    job.print(doc, pras);
    Thread.sleep(10000);
}

From source file:Print.java

public static void printToService(PrintService service, String filename, PrintRequestAttributeSet attributes)
        throws IOException {
    // Figure out what type of file we're printing
    DocFlavor flavor = getFlavorFromFilename(filename);
    // Open the file
    InputStream in = new FileInputStream(filename);
    // Create a Doc object to print from the file and flavor.
    Doc doc = new SimpleDoc(in, flavor, null);
    // Create a print job from the service
    DocPrintJob job = service.createPrintJob();

    // Monitor the print job with a listener
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printJobCompleted(PrintJobEvent e) {
            System.out.println("Print job complete");
            System.exit(0);/*from   w  w  w.  j  av a 2  s  .co m*/
        }

        public void printDataTransferCompleted(PrintJobEvent e) {
            System.out.println("Document transfered to printer");
        }

        public void printJobRequiresAttention(PrintJobEvent e) {
            System.out.println("Print job requires attention");
            System.out.println("Check printer: out of paper?");
        }

        public void printJobFailed(PrintJobEvent e) {
            System.out.println("Print job failed");
            System.exit(1);
        }
    });

    // Now print the document, catching errors
    try {
        job.print(doc, attributes);
    } catch (PrintException e) {
        System.out.println(e);
        System.exit(1);
    }
}

From source file:Main.java

PrintJobWatcher(DocPrintJob job) {
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printJobCanceled(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;/*  w  w  w  . j a v  a  2  s .  com*/
                PrintJobWatcher.this.notify();
            }
        }

        public void printJobCompleted(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;
                PrintJobWatcher.this.notify();
            }
        }

        public void printJobFailed(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;
                PrintJobWatcher.this.notify();
            }
        }

        public void printJobNoMoreEvents(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;
                PrintJobWatcher.this.notify();
            }
        }

    });
}

From source file:Main.java

PrintJobWatcher(DocPrintJob job) {
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printJobCanceled(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;//from   w  w  w.ja va2  s.  c o m
                PrintJobWatcher.this.notify();
            }
        }

        public void printJobCompleted(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;
                PrintJobWatcher.this.notify();
            }
        }

        public void printJobFailed(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;
                PrintJobWatcher.this.notify();
            }
        }

        public void printJobNoMoreEvents(PrintJobEvent pje) {
            synchronized (PrintJobWatcher.this) {
                done = true;
                PrintJobWatcher.this.notify();
            }
        }
    });
}

From source file:JuliaSet3.java

public void printToService(PrintService service, PrintRequestAttributeSet printAttributes) {
    // Wrap ourselves in the PrintableComponent class defined by JuliaSet2.
    String title = "Julia set for c={" + cx + "," + cy + "}";
    Printable printable = new PrintableComponent(this, title);

    // Now create a Doc that encapsulate the Printable object and its type
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    Doc doc = new SimpleDoc(printable, flavor, null);

    // Java 1.1 uses PrintJob.
    // Java 1.2 uses PrinterJob.
    // Java 1.4 uses DocPrintJob. Create one from the service
    DocPrintJob job = service.createPrintJob();

    // Set up a dialog box to monitor printing status
    final JOptionPane pane = new JOptionPane("Printing...", JOptionPane.PLAIN_MESSAGE);
    JDialog dialog = pane.createDialog(this, "Print Status");
    // This listener object updates the dialog as the status changes
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printJobCompleted(PrintJobEvent e) {
            pane.setMessage("Printing complete.");
        }/*www.  j  a v a  2  s.co  m*/

        public void printDataTransferCompleted(PrintJobEvent e) {
            pane.setMessage("Document transfered to printer.");
        }

        public void printJobRequiresAttention(PrintJobEvent e) {
            pane.setMessage("Check printer: out of paper?");
        }

        public void printJobFailed(PrintJobEvent e) {
            pane.setMessage("Print job failed");
        }
    });

    // Show the dialog, non-modal.
    dialog.setModal(false);
    dialog.show();

    // Now print the Doc to the DocPrintJob
    try {
        job.print(doc, printAttributes);
    } catch (PrintException e) {
        // Display any errors to the dialog box
        pane.setMessage(e.toString());
    }
}

From source file:us.mn.state.health.lims.common.provider.reports.SampleLabelPrintProvider.java

private void runPrintJobs() throws PrintException {
    returnedData += ";;";

    if (masterCount > 0) {
        DocPrintJob masterJob = ps.createPrintJob();

        PrintRequestAttributeSet masterAset = new HashPrintRequestAttributeSet();
        masterAset.add(new Copies(masterCount));

        String masterLabel = "";
        String masterZPL = StringUtil.getMessageForKey("master.label.zpl.prefix"); //"^XA";
        for (Property prop : masterPrintOrder) {
            if (ConfigurationProperties.getInstance().isPropertyValueEqual(prop, "true")) {
                masterLabel += (!GenericValidator.isBlankOrNull(labelData.get(prop.name().toString()))
                        ? labelData.get(prop.name().toString())
                        : "") + "\n";
                if (prop.equals(Property.MASTER_LABELS_ACCESSION)) {
                    masterZPL += StringUtil.getMessageForKey("master.label.zpl.accession.barcode.prefix") //"^FO20,30^BCN,60,N,N,N^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("master.label.zpl.accession.text.prefix") //"^FS^FO30,90^AEN,12,18^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("master.label.zpl.accession.suffix"); //"^FS";
                } else {
                    masterZPL += StringUtil.getMessageForKey("master.label.zpl.other.prefix") //"^FO25,195^AEN,12,18^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("master.label.zpl.other.suffix"); //"^FS";
                }/*from w ww.  j  ava2  s  . c  om*/
            }
        }
        masterZPL += StringUtil.getMessageForKey("master.label.zpl.suffix"); //"^XZ";

        // Printing raw ZPL and returning the ZPL string that can be displayed via JavaScript as a simulated label in the browser
        //         System.out.println("Master Label (" + masterCount + " copies):\n" + masterLabel);
        //         System.out.println("Master Label ZPL (" + masterCount + " copies):\n" + masterZPL + "\n");
        returnedData += masterZPL;

        byte[] byt = masterZPL.getBytes();
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        Doc doc = new SimpleDoc(byt, flavor, null);
        masterJob.addPrintJobListener(new MyPrintJobListener());
        masterJob.print(doc, masterAset);
    }

    returnedData += ";;";

    for (int j = 1; j <= itemCount; j++) {
        DocPrintJob itemJob = ps.createPrintJob();
        PrintRequestAttributeSet itemAset = new HashPrintRequestAttributeSet();
        itemAset.add(new Copies(1));

        String itemLabel = "";
        String itemZPL = StringUtil.getMessageForKey("item.label.zpl.prefix"); //"^XA";
        for (Property prop : itemPrintOrder) {
            if (ConfigurationProperties.getInstance().isPropertyValueEqual(prop, "true")) {
                itemLabel += !GenericValidator.isBlankOrNull(labelData.get(prop.name().toString()))
                        ? labelData.get(prop.name().toString())
                        : "";
                if (prop.equals(Property.SPECIMEN_LABELS_ACCESSION)) {
                    itemLabel += "-" + j;
                    itemZPL += StringUtil.getMessageForKey("item.label.zpl.accession.barcode.prefix") //"^FO20,30^BCN,60,N,N,N^FD"
                            + labelData.get(prop.name().toString()) + "-" + j
                            + StringUtil.getMessageForKey("item.label.zpl.accession.text.prefix") //"^FS^FO30,90^AEN,12,18^FD"
                            + labelData.get(prop.name().toString()) + "-" + j
                            + StringUtil.getMessageForKey("item.label.zpl.accession.suffix"); //"^FS";
                } else {
                    itemZPL += StringUtil.getMessageForKey("item.label.zpl.other.prefix") //"^FO25,195^AEN,12,18^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("item.label.zpl.other.suffix"); //"^FS";
                }
                itemLabel += "\n";
            }
        }
        itemZPL += StringUtil.getMessageForKey("item.label.zpl.suffix"); //"^XZ";

        // Printing raw ZPL and returning a ZPL string that can be displayed via JavaScript as a simulated label in the browser
        //         System.out.println("Item Label:\n" + itemLabel);            
        //         System.out.println("Item Label ZPL:\n" + itemZPL + "\n");
        returnedData += "::" + itemZPL;

        byte[] byt = itemZPL.getBytes();
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        Doc doc = new SimpleDoc(byt, flavor, null);
        itemJob.addPrintJobListener(new MyPrintJobListener());
        itemJob.print(doc, itemAset);
    }

    //String label = SystemConfiguration.getInstance()
    //      .getDefaultSampleLabel(accessionNumber);

    //byte[] byt = label.getBytes();
    //DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    //Doc doc = new SimpleDoc(byt, flavor, null);
    //job.addPrintJobListener(new MyPrintJobListener());
    //job.print(doc, aset);
}

From source file:us.mn.state.health.lims.common.provider.reports.SampleLabelPrintProvider.java

public void printBarcodeLabel(String[] accList) {
    try {//from ww w .j  a va  2  s  .  c  om

        // Support with 3 labels per row for now
        int itemsperrow = 3;

        String numberOfLabelCopiesString = null;
        int numberOfLabelCopies = 1;
        try {
            numberOfLabelCopiesString = SystemConfiguration.getInstance()
                    .getLabelNumberOfCopies("print.label.numberofcopies");
            numberOfLabelCopies = Integer.parseInt(numberOfLabelCopiesString);
        } catch (NumberFormatException nfe) {
            // System.out.println("numberOfLabelCopies not defined as
            // numeric");
        }

        String accessionNumber = "fail";// (String)

        int labelPerPatient = 1;
        try {
            labelPerPatient = masterCount;
        } catch (NumberFormatException nfe) {
            labelPerPatient = 1;
        }

        if (ps == null) {
            // System.out.println("Zebra printer is not found.");
            return;
        }

        ArrayList listAccessNumber = new ArrayList<>();

        for (int i = 0; i < accList.length; i++) {
            listAccessNumber.add(accList[i]);
        }

        ArrayList<String> newListAccessNumber = new ArrayList<String>();
        Iterator iter = listAccessNumber.iterator();
        while (iter.hasNext()) {
            String element = (String) iter.next();
            for (int i = 0; i < labelPerPatient; i++) {
                newListAccessNumber.add(element);
            }
        }
        int sizeListAcc = newListAccessNumber.size();

        if (sizeListAcc % itemsperrow != 0) {
            for (int i = 0; i < (itemsperrow - (sizeListAcc % itemsperrow)); i++) {
                newListAccessNumber.add("");
            }
        }
        String accessionNumber1 = "";
        String accessionNumber2 = "";
        String accessionNumber3 = "";

        for (int i = 0; i < newListAccessNumber.size(); i = i + itemsperrow) {
            accessionNumber1 = newListAccessNumber.get(i).toString();
            accessionNumber2 = newListAccessNumber.get(i + 1).toString();
            accessionNumber3 = newListAccessNumber.get(i + 2).toString();
            DocPrintJob job = ps.createPrintJob();
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            //aset.add(new Copies(numberOfLabelCopies * labelPerPatient));
            aset.add(new Copies(numberOfLabelCopies * 1));
            String label = "";
            if (itemsperrow == 3) {
                label = SystemConfiguration.getInstance().getAllDefaultSampleLabel(accessionNumber1,
                        accessionNumber2, accessionNumber3);
                returnedData += label;
            } else {
                return;
            }
            System.out.println(returnedData);

            byte[] byt = label.getBytes();
            DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
            Doc doc = new SimpleDoc(byt, flavor, null);
            job.addPrintJobListener(new MyPrintJobListener());
            job.print(doc, aset);
            //System.out.println("success...");
        }

    } catch (Exception e) {
        System.out.println("Error in SampleLabelPrintProvider");
        e.printStackTrace();
    } finally {

    }
}