Retrieve a Print Service with a name containing the specified Printer Name; will return null if not found. - Java 2D Graphics

Java examples for 2D Graphics:Print

Description

Retrieve a Print Service with a name containing the specified Printer Name; will return null if not found.

Demo Code


//package com.java2s;
import java.awt.print.PrinterJob;
import javax.print.PrintService;

public class Main {
    /**//from   w  w w  . j a v a  2s  .c  o m
     * Retrieve a Print Service with a name containing the specified
     * PrinterName; will return null if not found.
     * 
     * @return
     */
    public static PrintService findPrintService(String printerName) {

        printerName = printerName.toLowerCase();

        PrintService service = null;

        // Get array of all print services
        PrintService[] services = PrinterJob.lookupPrintServices();

        // Retrieve a print service from the array
        for (int index = 0; service == null && index < services.length; index++) {

            if (services[index].getName().toLowerCase()
                    .indexOf(printerName) >= 0) {
                service = services[index];
            }
        }
        return service;
    }
}

Related Tutorials