pdf - Printing in Java to a label printer that is not default printer -
i need print label printer. found code below in net , trying modify needs. works fine when label printer default printer in system, when set other printer default doesn't print properly. in image on link below 1 can see result when default (the 1 on right), first effort when not default (the middle one) , sec effort when not default. need print whether printer default or not, since part of software releasing clients. question is: there specific attributes or commands should sent printer in order print when not default printer? or how can set printer default printer within code, before printing, , restore default printer when done printing? image link (https://lh4.googleusercontent.com/-jba7ba77ik0/urplehzyvie/aaaaaaaaqwg/bo4ee3wepjg/s279-c/21213)
the source code
/* * copyright (c) 2004 david flanagan. rights reserved. * code book java examples in nutshell, 3nd edition. * provided as-is, without warranty either expressed or implied. * may study, use, , modify non-commercial purpose, * including teaching , utilize in open-source projects. * may distribute non-commercially long retain notice. * commercial utilize license, or purchase book, * please visit http://www.davidflanagan.com/javaexamples3. */ //package je3.print; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import javax.print.doc; import javax.print.docflavor; import javax.print.docprintjob; import javax.print.printexception; import javax.print.printservice; import javax.print.printservicelookup; import javax.print.simpledoc; import javax.print.streamprintservice; import javax.print.streamprintservicefactory; import javax.print.attribute.attribute; import javax.print.attribute.hashprintrequestattributeset; import javax.print.attribute.printrequestattributeset; import javax.print.attribute.standard.orientationrequested; import javax.print.event.printjobadapter; import javax.print.event.printjobevent; import org.apache.pdfbox.pdmodel.pddocument; /** * utility programme demonstrates javax.print api , allows list * available printers, query named printer, print text , image files * printer, , print postscript files. */ public class print { public static void main(string[] args) throws ioexception { // these values we'll set command-line arguments boolean query = false; string printername = "brother ql-570 le"; string inputfilename = "c:\\dev\\label.pdf"; printrequestattributeset attributes = new hashprintrequestattributeset(); attributes.add(orientationrequested.landscape); if (query) { // named printer can back upwards // attributes , print status queryprinter(printername, attributes); } else { // print named printer, or default // printer otherwise. print(printername, inputfilename, attributes); } // main() method ends here, there may printing thread // operating in background. programme may not terminate // until printing completes. } // list names of printservices can back upwards attributes public static void queryservices(printrequestattributeset attributes) { // find services can back upwards specified attributes printservice[] services = printservicelookup.lookupprintservices(null, attributes); // loop through available services (int = 0; < services.length; i++) { // print service name system.out.print(services[i].getname()); // query , print document types can print docflavor[] flavors = services[i].getsupporteddocflavors(); (int j = 0; j < flavors.length; j++) { // filter out docflavors have representation class other // java.io.inputstream. string repclass = flavors[j].getrepresentationclassname(); if (!repclass.equals("java.io.inputstream")) continue; system.out.println("\t" + flavors[j].getmimetype()); } } } // list details named printer public static void queryprinter(string printername, printrequestattributeset attributes) { // find named printer printservice service = getnamedprinter(printername, attributes); if (service == null) { system.out.println(printername + ": no such printer capable of " + "handling specified attributes"); return; } // print status , other info printer system.out.println(printername + " status:"); attribute[] attrs = service.getattributes().toarray(); (int = 0; < attrs.length; i++) system.out.println("\t" + attrs[i].getname() + ": " + attrs[i]); } // print contents of named file named printer (or // default printer if printername null) requesting specified // attributes. public static void print(string printername, string filename, printrequestattributeset attributes) throws ioexception { // printer can back upwards attributes printservice service = getnamedprinter(printername, attributes); if (service == null) { system.out.println("can't find printer " + "with specified attributes"); return; } // print file printer. see method definition below printtoservice(service, filename, attributes); // allow user know pick printout system.out.println("printed " + filename + " " + service.getname()); } // print output file instead of printer public static void printtofile(string outputfilename, string outputfiletype, string inputfilename, printrequestattributeset attributes) throws ioexception { // determine whether scheme can print specified type, , // mill object if so. // name of static method way long! streamprintservicefactory[] factories = streamprintservicefactory .lookupstreamprintservicefactories(null, outputfiletype); // error message if can't print specified output type if (factories.length == 0) { system.out.println("unable print files of type: " + outputfiletype); return; } // open output file fileoutputstream out = new fileoutputstream(outputfilename); // printservice object print file streamprintservice service = factories[0].getprintservice(out); // print using method below printtoservice(service, inputfilename, attributes); // , remember close output file out.close(); } // print contents of named file specified printservice, // requesting specified attributes. // shared code used print() , printtofile() above. public static void printtoservice(printservice service, string filename, printrequestattributeset attributes) throws ioexception { // figure out type of file we're printing docflavor flavor = getflavorfromfilename(filename); // open file //inputstream in = new fileinputstream(filename); pddocument document = null; seek { document = pddocument.load(filename); } grab (exception e) { system.out.println("unable open pdf file "); } // create doc object print file , flavor. doc doc = new simpledoc(document, flavor, null); // create print job service docprintjob job = service.createprintjob(); // monitor print job listener job.addprintjoblistener(new printjobadapter() { public void printjobcompleted(printjobevent e) { system.out.println("print job complete"); system.exit(0); } public void printdatatransfercompleted(printjobevent e) { system.out.println("document transfered printer"); } public void printjobrequiresattention(printjobevent e) { system.out.println("print job requires attention"); system.out.println("check printer: out of paper?"); } public void printjobfailed(printjobevent e) { system.out.println("print job failed"); system.exit(1); } }); // print document, catching errors seek { job.print(doc, attributes); } grab (printexception e) { system.out.println(e); system.exit(1); } } // utility method printers can back upwards specified // attributes , homecoming 1 matches specified name. public static printservice getnamedprinter(string name, printrequestattributeset attrs) { printservice[] services = printservicelookup.lookupprintservices(null, attrs); if (services.length > 0) { if (name == null) homecoming services[0]; else { (int = 0; < services.length; i++) { if (services[i].getname().equals(name)) homecoming services[i]; } } } homecoming null; } // utility method homecoming docflavor object matching // extension of filename. public static docflavor getflavorfromfilename(string filename) { string extension = filename.substring(filename.lastindexof('.') + 1); extension = extension.tolowercase(); if (extension.equals("gif")) homecoming docflavor.input_stream.gif; else if (extension.equals("jpeg")) homecoming docflavor.input_stream.jpeg; else if (extension.equals("jpg")) homecoming docflavor.input_stream.jpeg; else if (extension.equals("png")) homecoming docflavor.input_stream.png; else if (extension.equals("ps")) homecoming docflavor.input_stream.postscript; else if (extension.equals("txt")) homecoming docflavor.input_stream.text_plain_host; else if (extension.equals("pdf")) homecoming docflavor.service_formatted.pageable; // fallback: seek determine flavor file content else homecoming docflavor.input_stream.autosense; } }
public static void main(string[] args) { seek { fileinputstream fis = new fileinputstream("c:\\n0018902726.pdf"); docflavor flavor = docflavor.input_stream.autosense; docattributeset das = new hashdocattributeset(); doc pdfdoc = new simpledoc(fis, flavor, null); printservice printservice = printservicelookup.lookupdefaultprintservice(); docprintjob printjob = printservice.createprintjob(); printjob.print(pdfdoc, new hashprintrequestattributeset()); fis.close(); } grab (exception e) { system.out.println("exception : " +e.getmessage()); } }
java pdf printing
No comments:
Post a Comment