Java調用印表機列印指定路徑圖片

來源:互聯網
上載者:User

標籤:ring   fileinput   oid   apache   java   close   draw   ons   apr   

依賴 javax.print

package com.xgt.util;import org.apache.commons.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.FileInputStream;import java.io.IOException;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.attribute.DocAttributeSet;import javax.print.attribute.HashDocAttributeSet;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;import javax.print.attribute.standard.Copies;import javax.print.attribute.standard.MediaPrintableArea;import javax.print.attribute.standard.OrientationRequested;import javax.print.attribute.standard.PrintQuality;public class PrintUtil {    private static final Logger logger = LoggerFactory.getLogger(PrintUtil.class);    public static void drawImage(String fileName, int count) {        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 = new HashPrintRequestAttributeSet();            pras.add(OrientationRequested.PORTRAIT);            pras.add(new Copies(count));            pras.add(PrintQuality.HIGH);            DocAttributeSet das = new HashDocAttributeSet();            // 設定列印紙張的大小(以毫米為單位)            das.add(new MediaPrintableArea(0, 0, 210, 296, MediaPrintableArea.MM));            fin = new FileInputStream(fileName);            Doc doc = new SimpleDoc(fin, dof, das);            DocPrintJob job = ps.createPrintJob();            job.print(doc, pras);            fin.close();            logger.info("列印成功!檔案:"+fileName+"數量為:"+count);        } catch (IOException ie) {            ie.printStackTrace();        } catch (PrintException pe) {            pe.printStackTrace();        }finally {            IOUtils.closeQuietly(fin);        }    }    public static void main(String[] args) {        new PrintUtil().drawImage("D:\\QRCodePng\\合成圖.png", 1);    }}

 

Java調用印表機列印指定路徑圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.