get printer service - Java 2D Graphics

Java examples for 2D Graphics:Print

Description

get printer service

Demo Code

/*//w ww .  java2 s  .c  o m
 * File:  PrintUtilities2.java
 *
 * Copyright (C) 2007, Andrew Moe
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Primary   Andrew Moe <moea@uwstout.edu>
 * Contact:  Student Developer, University of Wisconsin-Stout
 *           
 * Contact : Dennis Mikkelson <mikkelsond@uwstout.edu>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the National Science Foundation under grant
 * number DMR-0218882, and by the Intense Pulsed Neutron Source Division
 * of Argonne National Laboratory, Argonne, IL 60439-4845, USA.
 *
 * For further information, see <http://www.pns.anl.gov/ISAW/>
 *
 * Modified:
 * $Log$
 * Revision 1.9  2007/09/09 16:12:48  rmikk
 * Added code to handle the case where no printer can be found. It first tries to
 *    find a printer with a null attribute set.
 *
 * Revision 1.8  2007/08/23 21:06:31  dennis
 * Removed unused imports.
 *
 * Revision 1.7  2007/08/22 15:19:08  rmikk
 * Created a public static get_print_service that included the attribute set
 * The low-level print code has a scale factor of 1
 * The JWindow's do NOT need to be visible.  Removed that code
 * Broke up the printing code and put into several Runnables
 *
 * Revision 1.6  2007/08/16 22:13:11  rmikk
 * Added a few more InvokeLater's so there is time for the Window to draw
 *
 * Revision 1.5  2007/08/12 21:56:46  amoe
 * -Fixed comment typos.
 * -Added gov.anl.ipns.Util.Sys.FinishWindowListener to JWindow when the
 *  component container is being initiated.
 *
 * Revision 1.4  2007/08/02 20:59:46  oakgrovej
 * reintroduced the squish/stretch of the component to fit on the page.
 * It will keep the aspect ratio.
 *
 * Revision 1.3  2007/08/02 15:19:32  oakgrovej
 * Removed the stretching/shrinking of the component to be printed.
 *
 * Revision 1.2  2007/07/26 21:14:22  amoe
 * -In init_component_container(), the container size is set to the same size
 *  as the Component to be printed, not the default.
 *
 * -PrinterPage.print(..) no longer sets the component container to visible.
 *  This in now done in silent_print() and dialog_print().  Also here, the
 *  container is now moved off-screen since it is not necessary to see.  This
 *  is a temporary fix, since making the container not visible will cause it
 *  to make the output component to not show up when printed.
 *
 * Revision 1.1  2007/07/25 22:05:23  amoe
 * Initial commit.
 *
 */
//package com.java2s;

import javax.print.DocFlavor;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import javax.print.attribute.HashPrintRequestAttributeSet;

public class Main {
    public static PrintService get_print_service(String printerName,
            HashPrintRequestAttributeSet aset) {

        PrintService[] pservices = PrintServiceLookup.lookupPrintServices(
                DocFlavor.SERVICE_FORMATTED.PRINTABLE, aset);

        if (pservices == null)
            return null;

        for (int i = 0; i < pservices.length; i++) {

            if (pservices[i] != null
                    && pservices[i].toString() != null
                    && pservices[i].getName().trim()
                            .equals(printerName.trim()))
                return pservices[i];
        }

        return null;
    }
}

Related Tutorials