• 图片打印


    package cn.com;
    
    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.google.common.base.Strings;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    
    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;
    import javax.print.attribute.standard.Sides;
    
    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(Sides.ONE_SIDED);
                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:/UploadFiles/photo/201902001/立项资料/材料B/1571639594294.jpg", 1);
        }
    
    }
    
  • 相关阅读:
    Delphi操作ACCESS技巧集
    Delphi ADOQuery
    Delphi 与SQL编程
    delphi Sqlite
    Delphi 2010下使用sqlitesimpledelphi连接SQLite数据库及中文乱码问题的解决
    Java并发指南13:Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析
    Java并发指南12:深度解读 java 线程池设计思想及源码实现
    Java并发指南11:解读 Java 阻塞队列 BlockingQueue
    Java并发指南10:Java 读写锁 ReentrantReadWriteLock 源码分析
    Java并发指南9:AQS共享模式与并发工具类的实现
  • 原文地址:https://www.cnblogs.com/sunBinary/p/11725298.html
Copyright © 2020-2023  润新知