printing - How do I specify the printer I want to use in Java? -
currently retrieving default printer installed on machine printing. want able pick printer documents go to. best method of doing ?
code:
printservice[] services = printservicelookup.lookupprintservices(psinformat, null); system.out.println("printer selected " + services[printerinx]); //printservice defaultservice = printservicelookup.lookupdefaultprintservice(); docflavor[] docfalvor = services[printerinx].getsupporteddocflavors(); (int = 0; < docfalvor.length; i++) { system.out.println(docfalvor[i].getmimetype()); }
create printutility
class below, import , seek calling printutility.findprintservice("name_of_my_printer");
if know printer name; if don't know printers have access to, phone call printutility.getprinterservicenamelist();
list
containing viable registered printer names.
alternately see my reply question more details:
class="lang-java prettyprint-override">package com.stackoverflow.print; import java.awt.print.printerjob; import javax.print.printservice; import java.util.list; import java.util.arraylist; public final class printutility { /** * retrieve print service name containing specified printername; homecoming null if not found. * * @return */ public static printservice findprintservice(string printername) { printername = printername.tolowercase(); printservice service = null; // array of print services printservice[] services = printerjob.lookupprintservices(); // retrieve print service array (int index = 0; service == null && index < services.length; index++) { if (services[index].getname().tolowercase().indexof(printername) >= 0) { service = services[index]; } } // homecoming print service homecoming service; } /** * retrieves list of printer service names. * * @return list */ public static list<string> getprinterservicenamelist() { // list of print services printservice[] services = printerjob.lookupprintservices(); list<string> list = new arraylist<string>(); (int = 0; < services.length; i++) { list.add(services[i].getname()); } homecoming list; } /** * utility class; no construction! */ private printutility() {} }
java printing
No comments:
Post a Comment