Example usage for javax.print DocPrintJob print

List of usage examples for javax.print DocPrintJob print

Introduction

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

Prototype

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException;

Source Link

Document

Prints a document with the specified job attributes.

Usage

From source file:OneFourDialog.java

public static void main(String args[]) throws Exception {
    String filename = args[0];/*from ww  w  .j ava2s .  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[] argv) throws Exception {
    try {/*ww  w .  ja v  a2 s.c o  m*/
        OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
        DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
        InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
        StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
                .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

        StreamPrintService service = factories[0].getPrintService(fos);
        final DocPrintJob job = service.createPrintJob();
        Doc doc = new SimpleDoc(is, flavor, null);

        PrintJobWatcher pjDone = new PrintJobWatcher(job);

        if (job instanceof CancelablePrintJob) {
            CancelablePrintJob cancelJob = (CancelablePrintJob) job;
            try {
                cancelJob.cancel();
            } catch (PrintException e) {
            }
        }
        job.print(doc, null);
        pjDone.waitForDone();
        is.close();
    } catch (PrintException e) {
        if (e.getCause() instanceof PrinterAbortException) {
            System.out.println("Print job was cancelled");
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String filename = args[0];/*from   www . j  av  a 2 s .c  o m*/
    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:Main.java

public static void imprimirFactura(String factura) throws PrintException {
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    if (service == null) {
        throw new PrintException("No se encontro impresora conectada");
    }/* w w  w.ja v  a 2s. c  om*/

    //Le decimos el tipo de datos que vamos a enviar a la impresora
    //Tipo: bytes Subtipo: autodetectado
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    DocPrintJob pj = service.createPrintJob();
    byte[] bytes;
    bytes = factura.getBytes();
    Doc doc = new SimpleDoc(bytes, flavor, null);

    pj.print(doc, null);

}

From source file:net.sourceforge.fenixedu.util.report.ReportsUtils.java

private static void print(PrintService printService, byte[] pdf) throws PrintException {
    final DocPrintJob job = printService.createPrintJob();
    final Doc doc = new SimpleDoc(pdf, DocFlavor.BYTE_ARRAY.PDF, null);

    final PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(1));
    aset.add(MediaSizeName.ISO_A4);
    aset.add(Sides.ONE_SIDED);//from   w ww . j a  v  a 2 s  .com

    job.print(doc, aset);
}

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  ww.j av  a 2s.c o 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:org.obiba.onyx.print.PdfPrintingService.java

protected void print(Object source, DocFlavor flavor) throws PrintException {
    log.info("Starting print job");
    DocPrintJob printJob = printService.createPrintJob();
    printJob.print(new SimpleDoc(source, flavor, null), null);
    log.info("Print job finished");
}

From source file:ru.trett.cis.services.PrinterServiceImpl.java

@Override
public String print(String file) throws ApplicationException, FileNotFoundException, PrintException {
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.ISO_A4);
    PrintService printServices = PrintServiceLookup.lookupDefaultPrintService();
    if (printServices == null)
        throw new ApplicationException("Default printer not found");
    LOGGER.info("Default printer is " + printServices.getName());
    DocPrintJob printJob = printServices.createPrintJob();
    FileInputStream fis = new FileInputStream(file);
    Doc doc = new SimpleDoc(fis, flavor, null);
    printJob.print(doc, aset);
    return printServices.getName();
}

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.");
        }/*from w w  w  .  j  a  va2 s .c o 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:org.efaps.tests.Printer.java

/**
 * @param args/*from w  w  w  .  j  a  v  a  2  s . co  m*/
 */
public static void main(final String[] args) {
    final AttributeSet aset = new HashAttributeSet();
    aset.add(new PrinterName("HP-LaserJet-100-colorMFP-M175nw", null));
    final PrintService[] pservices = PrintServiceLookup.lookupPrintServices(null, aset);
    for (final PrintService serv : pservices) {
        System.out.println(serv);
    }

    try {
        final PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        // pras.add(new Copies(2));

        final PrintService pservice = getPrintService4Name("HP-LaserJet-100-colorMFP-M175nw");
        pservice.getSupportedDocFlavors();
        final DocPrintJob job = pservice.createPrintJob();

        final DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        final String filename = "/home/janmoxter/Downloads/Notas_de_Pedido_null.DOCX";
        final FileInputStream fis = new FileInputStream(filename);
        final DocAttributeSet das = new HashDocAttributeSet();
        final Doc doc = new SimpleDoc(fis, flavor, das);
        job.print(doc, pras);
    } catch (final FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final PrintException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}