Example usage for javax.print PrintServiceLookup lookupPrintServices

List of usage examples for javax.print PrintServiceLookup lookupPrintServices

Introduction

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

Prototype

public static final PrintService[] lookupPrintServices(DocFlavor flavor, AttributeSet attributes) 

Source Link

Document

Locates print services capable of printing the specified DocFlavor .

Usage

From source file:JuliaSet3.java

public void print() {
    // Get a list of all printers that can handle Printable objects.
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);

    // Set some define printing attributes
    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
    printAttributes.add(OrientationRequested.LANDSCAPE); // landscape mode
    printAttributes.add(Chromaticity.MONOCHROME); // print in mono

    // Display a dialog that allows the user to select one of the
    // available printers and to edit the default attributes
    PrintService service = ServiceUI.printDialog(null, 100, 100, services, null, null, printAttributes);

    // If the user canceled, don't do anything
    if (service == null)
        return;//from   w  ww  .j a  v  a  2 s .  c  o m

    // Now call a method defined below to finish the printing
    printToService(service, printAttributes);
}

From source file:org.obiba.onyx.print.PdfPrintingService.java

public void afterPropertiesSet() throws Exception {
    // Try to find a PrintService instance with the specified name if any
    if (printerName != null && printerName.length() > 0) {
        log.info("Looking for a printer named '{}'", printerName);
        // Lookup all services
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        for (PrintService ps : printServices) {
            if (ps.getName().equalsIgnoreCase(printerName)) {
                log.info("Using printer '{}'", ps.getName());
                this.printService = ps;
                break;
            }/*from  w  ww.j a  v a2  s . c o m*/
        }
        if (printService == null) {
            log.warn("Could not find printer with name '{}'. Will try default printer.", printerName);
        }
    }

    // If the printService is null, we weren't configured with a printerName or we couldn't
    // find one with the specified name
    if (printService == null) {
        printService = PrintServiceLookup.lookupDefaultPrintService();
        if (printService != null) {
            log.info("Using default printer '{}'.", printService.getName());
        }
    }

    // If the printService is null, there is no default printer installed.
    if (printService == null) {
        log.warn("No default printer found. Printing will not be available.");
    } else {
        // We have a PrintService instance. Find the first handler that this service accepts.
        for (PdfHandler handler : IMPLEMENTED_FLAVORS) {
            if (printService.isDocFlavorSupported(handler.getImplementedFlavor())) {
                supportedHandler = handler;
                break;
            }
        }

        if (supportedHandler != null) {
            log.info("Printer '{}' supports PDF printing through handler {}. PDF printing will be available.",
                    printService.getName(), supportedHandler.getClass().getSimpleName());
        } else {
            log.warn(
                    "Printer '{}' does not support printing PDF files directly. PDF printing will not be available.",
                    printService.getName());
            printService = null;
        }
    }
}

From source file:com.floreantpos.config.ui.AddPrinterDialog.java

@Override
public void initUI() {
    setLayout(new BorderLayout(5, 5));
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new MigLayout("", "[][grow][]", "[][][][][grow]")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    titlePanel = new TitlePanel();
    titlePanel.setTitle("Printer Type:");//$NON-NLS-1$
    add(titlePanel, BorderLayout.NORTH);

    JLabel lblName = new JLabel("Virtual Printer Name : "); //$NON-NLS-1$
    centerPanel.add(lblName, "cell 0 0,alignx trailing"); //$NON-NLS-1$

    tfName = new FixedLengthTextField(20);

    cbVirtualPrinter = new JComboBox();
    List<VirtualPrinter> virtualPrinters = VirtualPrinterDAO.getInstance().findAll();
    cbVirtualPrinter/*from   w w w  . j a va2s.c  o  m*/
            .setModel(new DefaultComboBoxModel<VirtualPrinter>(virtualPrinters.toArray(new VirtualPrinter[0])));
    //centerPanel.add(cbVirtualPrinter, "cell 1 0,growx"); //$NON-NLS-1$

    centerPanel.add(tfName, "cell 1 0,growx"); //$NON-NLS-1$

    JButton btnNew = new JButton(Messages.getString("AddPrinterDialog.7")); //$NON-NLS-1$
    btnNew.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            doAddNewVirtualPrinter();
        }
    });
    //centerPanel.add(btnNew, "cell 2 0"); //$NON-NLS-1$

    JLabel lblDevice = new JLabel(Messages.getString("AddPrinterDialog.9")); //$NON-NLS-1$
    centerPanel.add(lblDevice, "cell 0 1,alignx trailing"); //$NON-NLS-1$

    cbDevice = new JComboBox();
    List printerServices = new ArrayList<>();
    printerServices.add(DO_NOT_PRINT);
    PrintService[] lookupPrintServices = PrintServiceLookup.lookupPrintServices(null, null);
    //cbDevice.setModel(new DefaultComboBoxModel(PrintServiceLookup.lookupPrintServices(null, null)));
    cbDevice.addItem(null);
    for (int i = 0; i < lookupPrintServices.length; i++) {
        printerServices.add(lookupPrintServices[i]);
    }
    cbDevice.setModel(new ComboBoxModel(printerServices));

    cbDevice.setRenderer(new PrintServiceComboRenderer());
    centerPanel.add(cbDevice, "cell 1 1,growx"); //$NON-NLS-1$

    chckbxDefault = new JCheckBox(Messages.getString("AddPrinterDialog.12")); //$NON-NLS-1$
    centerPanel.add(chckbxDefault, "cell 1 2"); //$NON-NLS-1$

    JSeparator separator = new JSeparator();
    centerPanel.add(separator, "cell 0 3 3 1,growx,gapy 50px"); //$NON-NLS-1$

    add(centerPanel, BorderLayout.CENTER);

    JPanel panel = new JPanel();

    JButton btnOk = new JButton(Messages.getString("AddPrinterDialog.16")); //$NON-NLS-1$
    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            doAddPrinter();
        }
    });
    panel.add(btnOk);

    JButton btnCancel = new JButton(Messages.getString("AddPrinterDialog.17")); //$NON-NLS-1$
    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setCanceled(true);
            dispose();
        }
    });
    panel.add(btnCancel);
    add(panel, BorderLayout.SOUTH); //$NON-NLS-1$
}

From source file:Print.java

public static void queryServices(PrintRequestAttributeSet attributes) {
    // Find all services that can support the specified attributes
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributes);
    // Loop through available services
    for (int i = 0; i < services.length; i++) {
        // Print service name
        System.out.print(services[i].getName());

        // Then query and print the document types it can print
        DocFlavor[] flavors = services[i].getSupportedDocFlavors();
        for (int j = 0; j < flavors.length; j++) {
            // Filter out DocFlavors that have a representation class other
            // than java.io.InputStream.
            String repclass = flavors[j].getRepresentationClassName();
            if (!repclass.equals("java.io.InputStream"))
                continue;
            System.out.println("\t" + flavors[j].getMimeType());
        }/*  w w  w.  j a v  a 2s  .c  om*/
    }
}

From source file:net.sf.jasperreports.engine.print.JRPrinterAWT.java

/**
 *
 *///from   w w w  .jav  a2  s .  c  o  m
public boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);
    if (jasperPrint.getProperty("printService") != null) {
        String printServiceName = jasperPrint.getProperty("printService");
        try {
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            for (PrintService se : services) {
                if (se.getName().contains(printServiceName)) {
                    printJob.setPrintService(se);
                    break;
                }
            }

        } catch (PrinterException ex) {
            Logger.getLogger(JRPrinterAWT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (withPrintDialog) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}

From source file:com.floreantpos.model.PosPrinters.java

public static String getDefaultPrinterName() {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    if (services.length > 0) {
        return services[0].getName();
    }/*from  w ww  .j  av a2 s  . c  om*/
    return null;
}

From source file:com.floreantpos.jasperreport.engine.print.JRPrinterAWT.java

/**
 * Fix for bug ID 6255588 from Sun bug database
 * @param job print job that the fix applies to
 *///from www . j a va 2 s .c  o m
public static void initPrinterJobFields(PrinterJob job) {
    try {
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

        for (int i = 0; i < printServices.length; i++) {
            PrintService service = printServices[i];
            if (service.getName().equals(printerName)) {
                printService = service;
                break;
            }
        }

        job.setPrintService(printService);
    } catch (PrinterException e) {
    }
}

From source file:Print.java

public static PrintService getNamedPrinter(String name, PrintRequestAttributeSet attrs) {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attrs);
    if (services.length > 0) {
        if (name == null)
            return services[0];
        else {//from  www  .  j av a 2  s  .  c  o m
            for (int i = 0; i < services.length; i++) {
                if (services[i].getName().equals(name))
                    return services[i];
            }
        }
    }
    return null;
}

From source file:de.cenote.jasperstarter.App.java

private void listPrinters() {
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    System.out.println("Default printer:");
    System.out.println("-----------------");
    System.out.println((defaultService == null) ? "--- not set ---" : defaultService.getName());
    System.out.println("");
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    System.out.println("Available printers:");
    System.out.println("--------------------");
    for (PrintService service : services) {
        System.out.println(service.getName());
    }/*from   w w w .  jav a  2 s  . c om*/
}

From source file:org.apache.camel.component.printer.PrinterProducer.java

private PrintService assignPrintService() throws PrintException {
    PrintService printService;/*from  w w w.  j a v a2 s . c  o  m*/

    if ((config.getHostname().equalsIgnoreCase("localhost"))
            && (config.getPrintername().equalsIgnoreCase("/default"))) {
        printService = PrintServiceLookup.lookupDefaultPrintService();
    } else {
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        setPrinter("\\\\" + config.getHostname() + "\\" + config.getPrintername());
        int position = findPrinter(services, printer);
        if (position < 0) {
            throw new PrintException("No printer found with name: " + printer
                    + ". Please verify that the host and printer are registered and reachable from this machine.");
        }
        printService = services[position];
    }
    return printService;
}