java批量打印

下面是实现java批量打印功能的具体工具类,其中的Info类和UserClass为具体的快递面单中要打印的信息,以参数的方式传递过来,循环调用java PrinterJob服务进行打印;这里的快递尺寸已经调整好,可以使用,如需添加只要根据实际的面单进行调整,也可以专门写一个接口类的形式来创建快递面单的尺寸,本人没有尝试;如遇问题欢迎留言,共同探讨!

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import com.myprint.model.Info;
import com.myprint.model.UserClass;

 

public class PrintUtil   implements Printable{
 /**
  * 纸张尺寸
  */
 private double width;
 private double height;
 
 /**
  * 打印起始坐标
  */
 private double startX;
 private double startY;
 
 //打印页数
 private int pages;
 
 //打印数据坐标
 private Map pcoord;
 
 //发件方信息  
 private SenderInfo info;
 
 //打印数据源
 private List sourcelist;
 //private int PAGES = 0;
 
 //private PrintPaper printpaper;
 
 private UserObject obj;
 
 public PrintUtil(){}
 
 public PrintUtil(int expkind, UserObject obj, List sourcelist)
 {

  this.pcoord = this.pcoord == null ? new HashMap() : this.pcoord;
  this.pcoord.clear();
  this.info = info;
  this.sourcelist = sourcelist;
  
  switch(expkind)
  {
   case 0: //EMS
       this.width = 652;
       this.height = 360;
       this.startX = 30;
       this.startY = 80;
       this.pages = this.sourcelist == null ? 0 : this.sourcelist.size();
    
    
          pcoord.put("nameX", 30F);
          pcoord.put("nameY", 80F + 30F);
         
          pcoord.put("numX", 30F + 90F);
          pcoord.put("numY", 80F + 30F);
         
          pcoord.put("cmpX", 30F);
          pcoord.put("cmpY", 80F + 60F);
         
          pcoord.put("addrX", 30F);
          pcoord.put("addrY", 80F + 90F);
         
          pcoord.put("RnameX", 30F + 290F);
          pcoord.put("RnameY", 80F + 30F);
         
          pcoord.put("RnumX", 30F + 390);
          pcoord.put("RnumY", 80F + 30);
         
          pcoord.put("RcmpyX", 30F + 280);
          pcoord.put("RcmpyY", 80F + 55);
         
          pcoord.put("RaddrX", 30F + 280);
          pcoord.put("RaddrY", 80F + 90); 
        break;
   case 1://顺风快递
       this.width = 632;
       this.height = 412;
       this.startX = 40;
       this.startY = 30;
       this.pages = this.sourcelist == null ? 0 : this.sourcelist.size();
      
          pcoord.put("nameX", 285F);
          pcoord.put("nameY", 126F);
         
          pcoord.put("numX", 200F);
          pcoord.put("numY", 185F);
         
          pcoord.put("cmpX", 120F);
          pcoord.put("cmpY", 126F);
         
          pcoord.put("addrX", 120F);
          pcoord.put("addrY", 145F);
         
          pcoord.put("RnameX", 275F);
          pcoord.put("RnameY", 240F);
         
          pcoord.put("RnumX", 200F);
          pcoord.put("RnumY", 300F);
         
          pcoord.put("RcmpyX", 130F);
          pcoord.put("RcmpyY", 240F);
         
          pcoord.put("RaddrX", 130F);
          pcoord.put("RaddrY", 270F);  
    
    break;
   case 2://申通快递
       this.width = 652;
       this.height = 360;
       this.startX = 30;
       this.startY = 80;
       this.pages = this.sourcelist == null ? 0 : this.sourcelist.size();
          pcoord.put("nameX", 42F);
          pcoord.put("nameY", 100F);
         
          pcoord.put("numX", 62F);
          pcoord.put("numY", 200F);
         
          pcoord.put("cmpX", 42F);
          pcoord.put("cmpY", 126F);
         
          pcoord.put("addrX", 42F);
          pcoord.put("addrY", 155F);
         
          pcoord.put("RnameX", 320F);
          pcoord.put("RnameY", 100F);
         
          pcoord.put("RnumX", 330F);
          pcoord.put("RnumY", 200F);
         
          pcoord.put("RcmpyX", 310F);
          pcoord.put("RcmpyY", 126F);
         
          pcoord.put("RaddrX", 310F);
          pcoord.put("RaddrY", 155F);  
    
    break;
   default:
    break;
    
  }
 }
   /** 
   * @param Graphic指明打印的图形环境
   * @param PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×842点)
   * @param pageIndex指明页号
   **/
   public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {    
      //转换成Graphics2D
      Graphics2D g2 = (Graphics2D) gra;
      //设置打印颜色为黑色
      g2.setColor(Color.black);

      switch(pageIndex){
         case 0:
           Font font = new Font("新宋体", Font.PLAIN, 13);
           g2.setFont(font);//设置字体
           //BasicStroke   bs_3=new   BasicStroke(0.5f);  
           float[]   dash1   =   {2.0f}; 
           g2.setStroke(new   BasicStroke(0.5f,   BasicStroke.CAP_BUTT,   BasicStroke.JOIN_MITER,   2.0f,   dash1,   0.0f));                                           
           g2.drawString(info.getSname() == null ? "" : info.getSname(), pcoord.get("nameX"), pcoord.get("SnameY"));
           g2.drawString(info.getSnum() == null ? "" : info.getSnum(), pcoord.get("numX"), pcoord.get("numY")); 
           g2.drawString(info.getScmp() == null ? "" : info.getScmp(), pcoord.get("cmpX"), pcoord.get("cmpY"));
           g2.drawString(info.getSaddr() == null ? "" : info.getSaddr(), pcoord.get("addrX"), pcoord.get("addrY"));
        
           g2.drawString(obj.getRname() == null ? "" : obj.getRname(), pcoord.get("rnameX"), pcoord.get("rnameY"));           
           g2.drawString(obj.getRnum() == null ? "" : obj.getRnum(), pcoord.get("rnumX"), pcoord.get("rnumY"));
           g2.drawString(obj.getRcmpy() == null ? "" : obj.getRcmpy(), pcoord.get("rcmpyX"), pcoord.get("rcmpyY"));
           g2.drawString(obj.getRaddr() == null ? "" : obj.getRaddr(), pcoord.get("raddrX"), pcoord.get("raddrY"));
          
         return PAGE_EXISTS;
         default:
         return NO_SUCH_PAGE;
      }
     
   }
   // 打印内容到指定位置
   public void printContent()
   {
       if (sourcelist != null && sourcelist.size() > 0) // 当打印内容不为空时
       {
           //PAGES = printpaper.getSourcelist().size(); // 获取打印总页数
           // 书、文档
           Book book = new Book();
           // 设置成竖打
           PageFormat pf = new PageFormat();
           pf.setOrientation(PageFormat.PORTRAIT);
           // 通过Paper设置页面的空白边距和可打印区域。

           Paper p = new Paper();
           p.setSize(this.width,this.height);//纸张大小
           p.setImageableArea(this.startX,this.startY, this.width,this.height);//设置打印区域

           pf.setPaper(p);
           // 把 PageFormat 和 Printable 添加到书中,组成一个页面
           book.append(this, pf);
          

            //获取打印服务对象
            PrinterJob job = PrinterJob.getPrinterJob();     
            // 设置打印类
            job.setPageable(book);
            try {
              //直接打印
             for(UserObject obj: sourcelist)
             {
              this.obj= obj;
              job.print();
             }
               
                //}
            } catch (PrinterException e) {
                e.printStackTrace();
            }
       }
       else
       {
           // 如果打印内容为空时,提示用户打印将取消
           JOptionPane.showConfirmDialog(null,
                   "对不起, 打印内容为空, 打印取消!",
                   "提示", JOptionPane.DEFAULT_OPTION,
                   JOptionPane.WARNING_MESSAGE);
       }
   }
}

你可能感兴趣的:(java技术)