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:Main.java

public static void main(String[] argv) throws Exception {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    for (int i = 0; i < services.length; i++) {
        services[i].addPrintServiceAttributeListener(new MyPrintServiceAttributeListener());
    }/* www . ja  v a 2  s  .  c  o m*/
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    PrintService pss[] = PrintServiceLookup.lookupPrintServices(null, null);
    for (int i = 0; i < pss.length; ++i) {
        System.out.println(pss[i]);
        PrintService ps = pss[i];

        PrintServiceAttributeSet psas = ps.getAttributes();
        Attribute attributes[] = psas.toArray();
        for (int j = 0; j < attributes.length; ++j) {
            Attribute attribute = attributes[j];
            System.out.println("  attribute: " + attribute.getName());
            if (attribute instanceof PrinterName) {
                System.out.println("    printer name: " + ((PrinterName) attribute).getValue());
            }/* ww w .  j av  a 2s .  c om*/
        }
        DocFlavor supportedFlavors[] = ps.getSupportedDocFlavors();
        for (int j = 0; j < supportedFlavors.length; ++j) {
            System.out.println("  flavor: " + supportedFlavors[j]);
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    services = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, null);

    AttributeSet aset = new HashAttributeSet();
    aset.add(new PrinterName("HP LaserJet", null));
    services = PrintServiceLookup.lookupPrintServices(null, aset);

    aset = new HashAttributeSet();
    aset.add(ColorSupported.SUPPORTED);
    services = PrintServiceLookup.lookupPrintServices(null, aset);

}

From source file:Main.java

static public void main(String args[]) throws Exception {
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));

    PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);

    if (pss.length == 0)
        throw new RuntimeException("No printer services available.");

    PrintService ps = pss[0];/*from   w w  w .j ava 2  s. c o  m*/
    System.out.println("Printing to " + ps);

    DocPrintJob job = ps.createPrintJob();

    FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
    Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

    job.print(doc, pras);

    fin.close();
}

From source file:PrintImage.java

static public void main(String args[]) throws Exception {
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));
    PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
    if (pss.length == 0)
        throw new RuntimeException("No printer services available.");
    PrintService ps = pss[0];/*ww  w  .  j  a  va 2 s.  c om*/
    System.out.println("Printing to " + ps);
    DocPrintJob job = ps.createPrintJob();
    FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
    Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
    job.print(doc, pras);
    fin.close();
}

From source file:PrintImage.java

static public void main(String args[]) throws Exception {
    try {//from w w w.j a  va  2  s .  co m
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));

        PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);

        if (pss.length == 0)
            throw new RuntimeException("No printer services available.");

        PrintService ps = pss[0];
        System.out.println("Printing to " + ps);

        DocPrintJob job = ps.createPrintJob();

        FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
        Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

        job.print(doc, pras);

        fin.close();
    } catch (IOException ie) {
        ie.printStackTrace();
    } catch (PrintException pe) {
        pe.printStackTrace();
    }
}

From source file:OneFourDialog.java

public static void main(String args[]) throws Exception {
    String filename = args[0];//  ww w  . j  av  a2s  .co  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:PrintServiceTest.java

public static void main(String[] args) {
    DocFlavor flavor = DocFlavor.URL.GIF;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
    if (args.length == 0) {
        if (services.length == 0)
            System.out.println("No printer for flavor " + flavor);
        else {//from   w ww .j a  v a2s.  c om
            System.out.println("Specify a file of flavor " + flavor
                    + "\nand optionally the number of the desired printer.");
            for (int i = 0; i < services.length; i++)
                System.out.println((i + 1) + ": " + services[i].getName());
        }
        System.exit(0);
    }
    String fileName = args[0];
    int p = 1;
    if (args.length > 1)
        p = Integer.parseInt(args[1]);
    try {
        if (fileName == null)
            return;
        FileInputStream in = new FileInputStream(fileName);
        Doc doc = new SimpleDoc(in, flavor, null);
        DocPrintJob job = services[p - 1].createPrintJob();
        job.print(doc, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (PrintException e) {
        e.printStackTrace();
    }
}

From source file:PrintServiceWebInterface.java

public static void main(String[] args) throws IOException {
    // Get the character encoders and decoders we'll need
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetEncoder encoder = charset.newEncoder();

    // The HTTP headers we send back to the client are fixed
    String headers = "HTTP/1.1 200 OK\r\n" + "Content-type: text/html\r\n" + "Connection: close\r\n" + "\r\n";

    // We'll use two buffers in our response. One holds the fixed
    // headers, and the other holds the variable body of the response.
    ByteBuffer[] buffers = new ByteBuffer[2];
    buffers[0] = encoder.encode(CharBuffer.wrap(headers));
    ByteBuffer body = ByteBuffer.allocateDirect(16 * 1024);
    buffers[1] = body;//from  w  w w .  j ava2s  . c  o  m

    // Find all available PrintService objects to describe
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

    // All of the channels we use in this code will be in non-blocking
    // mode. So we create a Selector object that will block while
    // monitoring all of the channels and will only stop blocking when
    // one or more of the channels is ready for I/O of some sort.
    Selector selector = Selector.open();

    // Create a new ServerSocketChannel, and bind it to port 8000.
    // Note that we have to do this using the underlying ServerSocket.
    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(new java.net.InetSocketAddress(8000));

    // Put the ServerSocketChannel into non-blocking mode
    server.configureBlocking(false);

    // Now register the channel with the Selector. The SelectionKey
    // represents the registration of this channel with this Selector.
    SelectionKey serverkey = server.register(selector, SelectionKey.OP_ACCEPT);

    for (;;) { // The main server loop. The server runs forever.
        // This call blocks until there is activity on one of the
        // registered channels. This is the key method in non-blocking I/O.
        selector.select();

        // Get a java.util.Set containing the SelectionKey objects for
        // all channels that are ready for I/O.
        Set keys = selector.selectedKeys();

        // Use a java.util.Iterator to loop through the selected keys
        for (Iterator i = keys.iterator(); i.hasNext();) {
            // Get the next SelectionKey in the set, and then remove it
            // from the set. It must be removed explicitly, or it will
            // be returned again by the next call to select().
            SelectionKey key = (SelectionKey) i.next();
            i.remove();

            // Check whether this key is the SelectionKey we got when
            // we registered the ServerSocketChannel.
            if (key == serverkey) {
                // Activity on the ServerSocketChannel means a client
                // is trying to connect to the server.
                if (key.isAcceptable()) {
                    // Accept the client connection, and obtain a
                    // SocketChannel to communicate with the client.
                    SocketChannel client = server.accept();

                    // Make sure we actually got a connection
                    if (client == null)
                        continue;

                    // Put the client channel in non-blocking mode.
                    client.configureBlocking(false);

                    // Now register the client channel with the Selector,
                    // specifying that we'd like to know when there is
                    // data ready to read on the channel.
                    SelectionKey clientkey = client.register(selector, SelectionKey.OP_READ);
                }
            } else {
                // If the key we got from the Set of keys is not the
                // ServerSocketChannel key, then it must be a key
                // representing one of the client connections.
                // Get the channel from the key.
                SocketChannel client = (SocketChannel) key.channel();

                // If we got here, it should mean that there is data to
                // be read from the channel, but we double-check here.
                if (!key.isReadable())
                    continue;

                // Now read bytes from the client. We assume that
                // we get all the client's bytes in one read operation
                client.read(body);

                // The data we read should be some kind of HTTP GET
                // request. We don't bother checking it however since
                // there is only one page of data we know how to return.
                body.clear();

                // Build an HTML document as our reponse.
                // The body of the document contains PrintService details
                StringBuffer response = new StringBuffer();
                response.append(
                        "<html><head><title>Printer Status</title></head>" + "<body><h1>Printer Status</h1>");
                for (int s = 0; s < services.length; s++) {
                    PrintService service = services[s];
                    response.append("<h2>").append(service.getName()).append("</h2><table>");
                    Attribute[] attrs = service.getAttributes().toArray();
                    for (int a = 0; a < attrs.length; a++) {
                        Attribute attr = attrs[a];
                        response.append("<tr><td>").append(attr.getName()).append("</td><td>").append(attr)
                                .append("</tr>");
                    }
                    response.append("</table>");
                }
                response.append("</body></html>\r\n");

                // Encode the response into the body ByteBuffer
                encoder.reset();
                encoder.encode(CharBuffer.wrap(response), body, true);
                encoder.flush(body);

                body.flip(); // Prepare the body buffer to be drained
                // While there are bytes left to write
                while (body.hasRemaining()) {
                    // Write both header and body buffers
                    client.write(buffers);
                }
                buffers[0].flip(); // Prepare header buffer for next write
                body.clear(); // Prepare body buffer for next read

                // Once we've sent our response, we have no more interest
                // in the client channel or its SelectionKey
                client.close(); // Close the channel.
                key.cancel(); // Tell Selector to stop monitoring it.
            }
        }
    }
}

From source file:com.akman.enjoyfood.SelectPrinters.java

/**
 * This method get all the printer installed in the system. Add then into
 * JTable.// w ww.  jav a 2 s .c o  m
 */
public void getPrinters() {

    model.setRowCount(0);

    DocFlavor doc_flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();

    PrintService[] service = PrintServiceLookup.lookupPrintServices(doc_flavor, attr_set);
    for (PrintService printService : service) {

        PrintServiceAttributeSet printServiceAttributes = printService.getAttributes();
        PrinterState printerState = (PrinterState) printServiceAttributes.get(PrinterState.class);

        if (printService != null) {
            Object row[] = { new Printer(printService.getName(), true), "Online" };
            model.addRow(row);
        } else {
            Object row[] = { new Printer(printService.getName(), true), "Offline" };
            model.addRow(row);
        }
    }

}