驱动打印

java驱动打印代码:

PrintTest.print(printMap, map1,Main.printname);



import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
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 javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;



public class PrintTest  implements Printable{

	@Override
	public int print(Graphics gra, PageFormat pf, int pageIndex)throws PrinterException 
	{
		  Component c = null;
	      //print string
	      String str1 = "打印出的字"; 
	       //转换成Graphics2D
	      Graphics2D g2 = (Graphics2D) gra;
	      //设置打印颜色为黑色
	      g2.setColor(Color.black);

	      //打印起点坐标
	      double x = pf.getImageableX();
	      double y = pf.getImageableY();

	      switch(pageIndex)
	      {
	         case 0:
		           //设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
		           //Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput
		           Font font = new Font("微软雅黑", Font.PLAIN, 10);
		           g2.setFont(font);//设置字体
		           //BasicStroke   bs_3=new   BasicStroke(0.5f);
		           float[]   dash1   =   {2.0f};
		           //设置打印线的属性。
		           //1.线宽 2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量
		           g2.setStroke(new   BasicStroke(0.5f,   BasicStroke.CAP_BUTT,   BasicStroke.JOIN_MITER,   2.0f,   dash1,   0.0f));
		           //g2.setStroke(bs_3);//设置线宽
		           float heigth = font.getSize2D();//字体高度
		           // -1- 用Graphics2D直接输出
		           //首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
		           //g2.drawLine(10,10,200,300);

		           //Image src = Toolkit.getDefaultToolkit().getImage("D:\\EclipseWorkSpace3.1\\Kfc-wuxi\\WebRoot\\image\\KFC.jpg");
		           //g2.drawImage(src,(int)x,(int)y,c);
		           //int img_Height=src.getHeight(c);
		           //int img_width=src.getWidth(c);
		          // int m = 30;
		           //int u = 0;
		           g2.setFont(font);//设置字体
		           g2.drawString(str1, (float)x, (float)y+1*heigth);
		           //g2.drawLine((int)x,(int)(y+1*heigth+img_Height+10),(int)x+10,(int)(y+1*heigth+img_Height+10));
		          // g2.drawImage(src,(int)x,(int)(y+1*heigth+img_Height+11),c);
		         return PAGE_EXISTS;

	         default:
	         return NO_SUCH_PAGE;
	      }
	  		
	  	}
	public static void main(String[] args) 
	{
		print();
	}
	
	public static void print()
	{
//	    通俗理解就是书、文档
			    Book book = new Book();
			    //    设置成竖打
			    PageFormat pf = new PageFormat();
			    pf.setOrientation(PageFormat.PORTRAIT);
			    //    通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
			    Paper p = new Paper();
			    p.setSize(137,840);//纸张大小
			    p.setImageableArea(0,0,137,840);//A4(595 X 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72
			    pf.setPaper(p);
			    //    把 PageFormat 和 Printable 添加到书中,组成一个页面
			    book.append(new PrintTest(), pf);
	
			     //获取打印服务对象
			     PrinterJob job = PrinterJob.getPrinterJob();
			     // 设置打印类
			     job.setPageable(book);
			     HashAttributeSet hs = new HashAttributeSet();
			     //打印机出口名
			     String printerName="58";
			     hs.add(new PrinterName(printerName,null));
			     PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);
			     if(pss.length==0)
			     {
			       System.out.println("无法找到打印机:"+printerName);
			       return ;
			     }
			     try
			     {
			       job.setPrintService(pss[0]);
			       job.print();
			     }
			     catch (PrinterException ex)
			     {
			       System.out.println(ex.getMessage());
			     }
	}
}

你可能感兴趣的:(java 驱动打印)