码迷,mamicode.com
首页 > 编程语言 > 详细

java打印

时间:2014-12-15 15:20:44      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   使用   

用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Graphics2D直接输出直接输出。
下面代码实现了简单的打印功能:
bubuko.com,布布扣import java.awt.BasicStroke;
bubuko.com,布布扣import java.awt.Color;
bubuko.com,布布扣import java.awt.Component;
bubuko.com,布布扣import java.awt.Font;
bubuko.com,布布扣import java.awt.Graphics;
bubuko.com,布布扣import java.awt.Graphics2D;
bubuko.com,布布扣import java.awt.Image;
bubuko.com,布布扣
bubuko.com,布布扣import java.awt.Toolkit;
bubuko.com,布布扣import java.awt.RenderingHints;
bubuko.com,布布扣import java.awt.font.FontRenderContext;
bubuko.com,布布扣import java.awt.font.LineBreakMeasurer;
bubuko.com,布布扣import java.awt.font.TextAttribute;
bubuko.com,布布扣import java.awt.font.TextLayout;
bubuko.com,布布扣import java.awt.geom.Point2D;
bubuko.com,布布扣import java.awt.image.BufferedImage;
bubuko.com,布布扣import java.awt.print.Book;
bubuko.com,布布扣import java.awt.print.PageFormat;
bubuko.com,布布扣import java.awt.print.Paper;
bubuko.com,布布扣import java.awt.print.Printable;
bubuko.com,布布扣import java.awt.print.PrinterException;
bubuko.com,布布扣import java.awt.print.PrinterJob;
bubuko.com,布布扣import java.text.AttributedString;
bubuko.com,布布扣
bubuko.com,布布扣import javax.swing.JApplet;
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣public class PrintTest   implements Printable{
bubuko.com,布布扣   /**
bubuko.com,布布扣   * @param Graphic指明打印的图形环境
bubuko.com,布布扣   * @param PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×842点)
bubuko.com,布布扣   * @param pageIndex指明页号
bubuko.com,布布扣   **/
bubuko.com,布布扣   public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
bubuko.com,布布扣       System.out.println("pageIndex="+pageIndex);
bubuko.com,布布扣       Component c = null;
bubuko.com,布布扣      //print string
bubuko.com,布布扣      String str = "中华民族是勤劳、勇敢和富有智慧的伟大民族。";
bubuko.com,布布扣      //转换成Graphics2D
bubuko.com,布布扣      Graphics2D g2 = (Graphics2D) gra;
bubuko.com,布布扣      //设置打印颜色为黑色
bubuko.com,布布扣      g2.setColor(Color.black);
bubuko.com,布布扣
bubuko.com,布布扣      //打印起点坐标
bubuko.com,布布扣      double x = pf.getImageableX();
bubuko.com,布布扣      double y = pf.getImageableY();
bubuko.com,布布扣       
bubuko.com,布布扣      switch(pageIndex){
bubuko.com,布布扣         case 0:
bubuko.com,布布扣           //设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
bubuko.com,布布扣           //Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput
bubuko.com,布布扣           Font font = new Font("新宋体", Font.PLAIN, 9);
bubuko.com,布布扣           g2.setFont(font);//设置字体
bubuko.com,布布扣           //BasicStroke   bs_3=new   BasicStroke(0.5f);   
bubuko.com,布布扣           float[]   dash1   =   {2.0f}; 
bubuko.com,布布扣           //设置打印线的属性。
bubuko.com,布布扣           //1.线宽 2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量
bubuko.com,布布扣           g2.setStroke(new   BasicStroke(0.5f,   BasicStroke.CAP_BUTT,   BasicStroke.JOIN_MITER,   2.0f,   dash1,   0.0f));  
bubuko.com,布布扣           //g2.setStroke(bs_3);//设置线宽
bubuko.com,布布扣           float heigth = font.getSize2D();//字体高度
bubuko.com,布布扣           System.out.println("x="+x);
bubuko.com,布布扣           // -1- 用Graphics2D直接输出
bubuko.com,布布扣           //首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
bubuko.com,布布扣           //g2.drawLine(10,10,200,300); 
bubuko.com,布布扣           
bubuko.com,布布扣           Image src = Toolkit.getDefaultToolkit().getImage("D:\\EclipseWorkSpace3.1\\Kfc-wuxi\\WebRoot\\image\\KFC.jpg");
bubuko.com,布布扣           g2.drawImage(src,(int)x,(int)y,c);
bubuko.com,布布扣           int img_Height=src.getHeight(c);
bubuko.com,布布扣           int img_width=src.getWidth(c);
bubuko.com,布布扣           //System.out.println("img_Height="+img_Height+"img_width="+img_width) ;
bubuko.com,布布扣           
bubuko.com,布布扣           g2.drawString(str, (float)x, (float)y+1*heigth+img_Height);
bubuko.com,布布扣           g2.drawLine((int)x,(int)(y+1*heigth+img_Height+10),(int)x+200,(int)(y+1*heigth+img_Height+10));
bubuko.com,布布扣           
bubuko.com,布布扣           g2.drawImage(src,(int)x,(int)(y+1*heigth+img_Height+11),c);
bubuko.com,布布扣           
bubuko.com,布布扣         return PAGE_EXISTS;
bubuko.com,布布扣         default:
bubuko.com,布布扣         return NO_SUCH_PAGE;
bubuko.com,布布扣      }
bubuko.com,布布扣      
bubuko.com,布布扣   }
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣public static void main(String[] args) {
bubuko.com,布布扣    
bubuko.com,布布扣    //    通俗理解就是书、文档
bubuko.com,布布扣    Book book = new Book();
bubuko.com,布布扣    //    设置成竖打
bubuko.com,布布扣    PageFormat pf = new PageFormat();
bubuko.com,布布扣    pf.setOrientation(PageFormat.PORTRAIT);
bubuko.com,布布扣    //    通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
bubuko.com,布布扣    Paper p = new Paper();
bubuko.com,布布扣    p.setSize(590,840);//纸张大小 
bubuko.com,布布扣    p.setImageableArea(10,10, 590,840);//A4(595 X 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72
bubuko.com,布布扣    pf.setPaper(p);
bubuko.com,布布扣    //    把 PageFormat 和 Printable 添加到书中,组成一个页面
bubuko.com,布布扣    book.append(new PrintTest(), pf);
bubuko.com,布布扣
bubuko.com,布布扣     //获取打印服务对象
bubuko.com,布布扣     PrinterJob job = PrinterJob.getPrinterJob();      
bubuko.com,布布扣     // 设置打印类
bubuko.com,布布扣     job.setPageable(book);
bubuko.com,布布扣     
bubuko.com,布布扣     try {
bubuko.com,布布扣         //可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
bubuko.com,布布扣         //boolean a=job.printDialog();
bubuko.com,布布扣         //if(a)
bubuko.com,布布扣         //{        
bubuko.com,布布扣         job.print();
bubuko.com,布布扣         //}
bubuko.com,布布扣     } catch (PrinterException e) {
bubuko.com,布布扣         e.printStackTrace();
bubuko.com,布布扣     }
bubuko.com,布布扣   }
bubuko.com,布布扣}

这个例子实现了打印字符串,线(包括虚线)和打印图片。而且通过Paper的setImageableArea可以设置打印的区域和边距,让开发者随意的设置打印的位置。
下面的打印代码没有设置打印区域,默认为打印纸张的区域和边距,比如我们一般用的A4纸,打印的起点X和Y坐标则是72,72。
无区域设置的代码:
bubuko.com,布布扣import java.awt.BasicStroke;
bubuko.com,布布扣import java.awt.Color;
bubuko.com,布布扣import java.awt.Component;
bubuko.com,布布扣import java.awt.Font;
bubuko.com,布布扣import java.awt.Graphics;
bubuko.com,布布扣import java.awt.Graphics2D;
bubuko.com,布布扣import java.awt.Image;
bubuko.com,布布扣
bubuko.com,布布扣import java.awt.Toolkit;
bubuko.com,布布扣import java.awt.RenderingHints;
bubuko.com,布布扣import java.awt.font.FontRenderContext;
bubuko.com,布布扣import java.awt.font.LineBreakMeasurer;
bubuko.com,布布扣import java.awt.font.TextAttribute;
bubuko.com,布布扣import java.awt.font.TextLayout;
bubuko.com,布布扣import java.awt.geom.Point2D;
bubuko.com,布布扣import java.awt.image.BufferedImage;
bubuko.com,布布扣import java.awt.print.Book;
bubuko.com,布布扣import java.awt.print.PageFormat;
bubuko.com,布布扣import java.awt.print.Paper;
bubuko.com,布布扣import java.awt.print.Printable;
bubuko.com,布布扣import java.awt.print.PrinterException;
bubuko.com,布布扣import java.awt.print.PrinterJob;
bubuko.com,布布扣import java.text.AttributedString;
bubuko.com,布布扣
bubuko.com,布布扣import javax.swing.JApplet;
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣public class PrintTest1   implements Printable{
bubuko.com,布布扣   /**
bubuko.com,布布扣   * @param Graphic指明打印的图形环境
bubuko.com,布布扣   * @param PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×842点)
bubuko.com,布布扣   * @param pageIndex指明页号
bubuko.com,布布扣   **/
bubuko.com,布布扣   public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
bubuko.com,布布扣       System.out.println("pageIndex="+pageIndex);
bubuko.com,布布扣       Component c = null;
bubuko.com,布布扣      //print string
bubuko.com,布布扣      String str = "中华民族是勤劳、勇敢和富有智慧的伟大民族。";
bubuko.com,布布扣      //转换成Graphics2D
bubuko.com,布布扣      Graphics2D g2 = (Graphics2D) gra;
bubuko.com,布布扣      //设置打印颜色为黑色
bubuko.com,布布扣      g2.setColor(Color.black);
bubuko.com,布布扣      
bubuko.com,布布扣      /*Paper paper = pf.getPaper();//得到页面格式的纸张 
bubuko.com,布布扣      paper.setSize(500,500);//纸张大小 
bubuko.com,布布扣      paper.setImageableArea(0,0,500,500); //设置打印区域的大小 
bubuko.com,布布扣      System.out.println(paper.getWidth());
bubuko.com,布布扣      System.out.println(paper.getHeight()); 
bubuko.com,布布扣      pf.setPaper(paper);//将该纸张作为格式 */
bubuko.com,布布扣
bubuko.com,布布扣      //打印起点坐标
bubuko.com,布布扣      double x = pf.getImageableX();
bubuko.com,布布扣      double y = pf.getImageableY();
bubuko.com,布布扣       
bubuko.com,布布扣      switch(pageIndex){
bubuko.com,布布扣         case 0:
bubuko.com,布布扣           //设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
bubuko.com,布布扣           //Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput
bubuko.com,布布扣           Font font = new Font("新宋体", Font.PLAIN, 9);
bubuko.com,布布扣           g2.setFont(font);//设置字体
bubuko.com,布布扣           //BasicStroke   bs_3=new   BasicStroke(0.5f);  
bubuko.com,布布扣           float[]   dash1   =   {4.0f}; 
bubuko.com,布布扣           g2.setStroke(new   BasicStroke(0.5f,   BasicStroke.CAP_BUTT,   BasicStroke.JOIN_MITER,   4.0f,   dash1,   0.0f));
bubuko.com,布布扣
bubuko.com,布布扣           float heigth = font.getSize2D();//字体高度
bubuko.com,布布扣           System.out.println("x="+x);
bubuko.com,布布扣           //使用抗锯齿模式完成文本呈现
bubuko.com,布布扣           /*g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
bubuko.com,布布扣          RenderingHints.VALUE_TEXT_ANTIALIAS_ON);*/
bubuko.com,布布扣           // -1- 用Graphics2D直接输出
bubuko.com,布布扣           //首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
bubuko.com,布布扣           
bubuko.com,布布扣           //g2.drawLine(10,10,200,10);  
bubuko.com,布布扣           
bubuko.com,布布扣           Image src = Toolkit.getDefaultToolkit().getImage("d://logo.gif");
bubuko.com,布布扣           g2.drawImage(src,(int)x,(int)y,c);
bubuko.com,布布扣           int img_Height=src.getHeight(c);
bubuko.com,布布扣           int img_width=src.getWidth(c);
bubuko.com,布布扣           //System.out.println("img_Height="+img_Height+"img_width="+img_width) ;
bubuko.com,布布扣           
bubuko.com,布布扣           g2.drawString(str, (float)x, (float)y+1*heigth+img_Height);
bubuko.com,布布扣           g2.drawLine((int)x,(int)(y+1*heigth+img_Height+10),(int)x+200,(int)(y+1*heigth+img_Height+10));
bubuko.com,布布扣           
bubuko.com,布布扣           g2.drawImage(src,(int)x,(int)(y+1*heigth+img_Height+11),c);
bubuko.com,布布扣           
bubuko.com,布布扣    
bubuko.com,布布扣           // -2- 直接构造TextLayout打印
bubuko.com,布布扣           /*FontRenderContext frc = g2.getFontRenderContext();
bubuko.com,布布扣           TextLayout layout = new TextLayout(str, font, frc);
bubuko.com,布布扣           layout.draw(g2, (float)x, (float)y+2*heigth);*/
bubuko.com,布布扣           // -3- 用LineBreakMeasurer进行打印
bubuko.com,布布扣           /*AttributedString text = new AttributedString(str);
bubuko.com,布布扣           text.addAttribute(TextAttribute.FONT, font);
bubuko.com,布布扣           LineBreakMeasurer lineBreaker = new LineBreakMeasurer(text.getIterator(), frc);
bubuko.com,布布扣           //每行字符显示长度(点)
bubuko.com,布布扣           double width = pf.getImageableWidth();
bubuko.com,布布扣           //首字符的基线位于用户空间中的 (x, y) 位置处
bubuko.com,布布扣           Point2D.Double pen = new Point2D.Double (100, y+3*heigth);
bubuko.com,布布扣           while ( (layout = lineBreaker.nextLayout( (float) width)) != null){
bubuko.com,布布扣              layout.draw(g2, (float)x, (float) pen.y);
bubuko.com,布布扣              pen.y += layout.getAscent();
bubuko.com,布布扣           }*/
bubuko.com,布布扣         return PAGE_EXISTS;
bubuko.com,布布扣         default:
bubuko.com,布布扣         return NO_SUCH_PAGE;
bubuko.com,布布扣      }
bubuko.com,布布扣      
bubuko.com,布布扣   }
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣public static void main(String[] args) {
bubuko.com,布布扣    
bubuko.com,布布扣     //获取打印服务对象
bubuko.com,布布扣     PrinterJob job = PrinterJob.getPrinterJob();     
bubuko.com,布布扣     PageFormat pageFormat = job.defaultPage();//得到默认页格式  
bubuko.com,布布扣     job.setPrintable(new PrintTest1());//设置打印类
bubuko.com,布布扣     
bubuko.com,布布扣     try {
bubuko.com,布布扣         //可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
bubuko.com,布布扣         //boolean a=job.printDialog();
bubuko.com,布布扣         //if(a)
bubuko.com,布布扣         //{
bubuko.com,布布扣         
bubuko.com,布布扣         job.print();
bubuko.com,布布扣         //}
bubuko.com,布布扣     } catch (PrinterException e) {
bubuko.com,布布扣         e.printStackTrace();
bubuko.com,布布扣     }
bubuko.com,布布扣   }
bubuko.com,布布扣}
posted on 2007-01-31 15:03 kelly 阅读(43343) 评论(13)  编辑  收藏 所属分类: java

bubuko.com,布布扣


FeedBack:
 
# re: 用java实现打印功能 2010-06-28 14:25 java打印
你好,请教个问题,你试过这段程序吗?
这样可以打印吗?你用的说明系统?
// 通俗理解就是书、文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(590,840);//纸张大小 
p.setImageableArea(10,10, 590,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);

try {
//可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
//boolean a=job.printDialog();
//if(a)
//{ 
job.print();
//}
} catch (PrinterException e) {
e.printStackTrace();
}

java打印

标签:des   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/lixue7/p/4164734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!