Java calls the printer to print the specified path picture

Source: Internet
Author: User

Dependent Javax.print

 PackageCom.xgt.util;Importorg.apache.commons.io.IOUtils;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJavax.print.Doc;ImportJavax.print.DocFlavor;ImportJavax.print.DocPrintJob;Importjavax.print.PrintException;ImportJavax.print.PrintService;ImportJavax.print.PrintServiceLookup;ImportJavax.print.SimpleDoc;ImportJavax.print.attribute.DocAttributeSet;ImportJavax.print.attribute.HashDocAttributeSet;ImportJavax.print.attribute.HashPrintRequestAttributeSet;ImportJavax.print.attribute.PrintRequestAttributeSet;Importjavax.print.attribute.standard.Copies;ImportJavax.print.attribute.standard.MediaPrintableArea;Importjavax.print.attribute.standard.OrientationRequested;Importjavax.print.attribute.standard.PrintQuality; Public classPrintutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (printutil.class);  Public Static voidDrawImage (String FileName,intcount) {FileInputStream fin=NULL; Try{docflavor DOF=NULL; if(Filename.endswith (". gif") ) {DOF=Docflavor.input_stream.            GIF; } Else if(Filename.endswith (". jpg") ) {DOF=Docflavor.input_stream.            JPEG; } Else if(Filename.endswith (". png") ) {DOF=Docflavor.input_stream.            PNG; } printservice PS=Printservicelookup.lookupdefaultprintservice (); Printrequestattributeset Pras=NewHashprintrequestattributeset ();            Pras.add (orientationrequested.portrait); Pras.add (NewCopies (count));            Pras.add (Printquality.high); Docattributeset das=NewHashdocattributeset (); //set the size of the printed paper (in millimeters)Das.add (NewMediaprintablearea (0, 0, 210, 296, mediaprintablearea.mm)); Fin=NewFileInputStream (fileName); Doc Doc=NewSimpledoc (Fin, DOF, das); Docprintjob Job=Ps.createprintjob ();            Job.print (Doc, Pras);            Fin.close (); Logger.info ("Print successfully!" File: "+filename+" number is: "+count); } Catch(IOException IE) {ie.printstacktrace (); } Catch(printexception pe) {pe.printstacktrace (); }finally{ioutils.closequietly (FIN); }    }     Public Static voidMain (string[] args) {NewPrintutil (). DrawImage ("d:\\qrcodepng\\ synthesis map. png", 1); }}

Java calls the printer to print the specified path picture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.