《Java实现打印功能(8页).doc》由会员分享,可在线阅读,更多相关《Java实现打印功能(8页).doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-Java实现打印功能-第 9 页Java实现打印功能用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Graphics2D直接输出直接输出。下面代码实现了简单的打印功能:importjava.awt.BasicStroke;importjava.awt.Color;importjava.awt.Component;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava
2、.awt.Image;importjava.awt.Toolkit;importjava.awt.RenderingHints;importjava.awt.font.FontRenderContext;importjava.awt.font.LineBreakMeasurer;importjava.awt.font.TextAttribute;importjava.awt.font.TextLayout;importjava.awt.geom.Point2D;importjava.awt.image.BufferedImage;importjava.awt.print.Book;import
3、java.awt.print.PageFormat;importjava.awt.print.Paper;importjava.awt.print.Printable;importjava.awt.print.PrinterException;importjava.awt.print.PrinterJob;importjava.text.AttributedString;importjavax.swing.JApplet;publicclassPrintTestimplementsPrintable/*paramGraphic指明打印的图形环境*paramPageFormat指明打印页格式(页
4、面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595842点)*parampageIndex指明页号*/publicintprint(Graphicsgra,PageFormatpf,intpageIndex)throwsPrinterExceptionSystem.out.println(pageIndex=+pageIndex);Componentc=null;/printstringStringstr=中华民族是勤劳、勇敢和富有智慧的伟大民族。;/转换成Graphics2DGraphics2Dg2=(Graphics2D)gra;/设置打印颜色为黑色g2
5、.setColor(Color.black);/打印起点坐标doublex=pf.getImageableX();doubley=pf.getImageableY();switch(pageIndex)case0:/设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)/Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog和DialogInputFontfont=newFont(新宋体,Font.PLAIN,9);g2.setFont(font);/设置字体/BasicStrokebs_3=newBasicStroke(0.5f
6、);floatdash1=2.0f;/设置打印线的属性。/1.线宽2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量g2.setStroke(newBasicStroke(0.5f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,2.0f,dash1,0.0f);/g2.setStroke(bs_3);/设置线宽floatheigth=font.getSize2D();/字体高度System.out.println(x=+x);/-1-用Graphics2D直接输出/首字符的基线(右下部)位于用户空间中的(x,y)位置处/g2.drawLine(
7、10,10,200,300);Imagesrc=Toolkit.getDefaultToolkit().getImage(D:EclipseWorkSpace3.1Kfc-wuxiWebRootimageKFC.jpg);g2.drawImage(src,(int)x,(int)y,c);intimg_Height=src.getHeight(c);intimg_width=src.getWidth(c);/System.out.println(img_Height=+img_Height+img_width=+img_width);g2.drawString(str,(float)x,(fl
8、oat)y+1*heigth+img_Height);g2.drawLine(int)x,(int)(y+1*heigth+img_Height+10),(int)x+200,(int)(y+1*heigth+img_Height+10);g2.drawImage(src,(int)x,(int)(y+1*heigth+img_Height+11),c);returnPAGE_EXISTS;default:returnNO_SUCH_PAGE;publicstaticvoidmain(Stringargs)/通俗理解就是书、文档Bookbook=newBook();/设置成竖打PageForm
9、atpf=newPageFormat();pf.setOrientation(PageFormat.PORTRAIT);/通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。Paperp=newPaper();p.setSize(590,840);/纸张大小p.setImageableArea(10,10,590,840);/A4(595X842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72pf.setPaper(p);/把PageFormat和Printable添加到书中,组成一个页面book.append(newPrintTest(),p
10、f);/获取打印服务对象PrinterJobjob=PrinterJob.getPrinterJob();/设置打印类job.setPageable(book);try/可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印/booleana=job.printDialog();/if(a)/job.print();/catch(PrinterExceptione)e.printStackTrace();这个例子实现了打印字符串,线(包括虚线)和打印图片。而且通过Paper的setImageableArea可以设置打印的区域和边距,让开发者随意的设置打印的位置。下面的打印
11、代码没有设置打印区域,默认为打印纸张的区域和边距,比如我们一般用的A4纸,打印的起点X和Y坐标则是72,72。无区域设置的代码:importjava.awt.BasicStroke;importjava.awt.Color;importjava.awt.Component;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.Toolkit;importjava.awt.RenderingHints;importjava.awt.font
12、.FontRenderContext;importjava.awt.font.LineBreakMeasurer;importjava.awt.font.TextAttribute;importjava.awt.font.TextLayout;importjava.awt.geom.Point2D;importjava.awt.image.BufferedImage;importjava.awt.print.Book;importjava.awt.print.PageFormat;importjava.awt.print.Paper;importjava.awt.print.Printable
13、;importjava.awt.print.PrinterException;importjava.awt.print.PrinterJob;importjava.text.AttributedString;importjavax.swing.JApplet;publicclassPrintTest1implementsPrintable/*paramGraphic指明打印的图形环境*paramPageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595842点)*parampageIndex指明页号*/publicintprin
14、t(Graphicsgra,PageFormatpf,intpageIndex)throwsPrinterExceptionSystem.out.println(pageIndex=+pageIndex);Componentc=null;/printstringStringstr=中华民族是勤劳、勇敢和富有智慧的伟大民族。;/转换成Graphics2DGraphics2Dg2=(Graphics2D)gra;/设置打印颜色为黑色g2.setColor(Color.black);/*Paperpaper=pf.getPaper();/得到页面格式的纸张paper.setSize(500,500)
15、;/纸张大小paper.setImageableArea(0,0,500,500);/设置打印区域的大小System.out.println(paper.getWidth();System.out.println(paper.getHeight();pf.setPaper(paper);/将该纸张作为格式*/打印起点坐标doublex=pf.getImageableX();doubley=pf.getImageableY();switch(pageIndex)case0:/设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)/Java平台所定义的五种字体系列:Serif、S
16、ansSerif、Monospaced、Dialog和DialogInputFontfont=newFont(新宋体,Font.PLAIN,9);g2.setFont(font);/设置字体/BasicStrokebs_3=newBasicStroke(0.5f);floatdash1=4.0f;g2.setStroke(newBasicStroke(0.5f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,4.0f,dash1,0.0f);floatheigth=font.getSize2D();/字体高度System.out.println(x=+x
17、);/使用抗锯齿模式完成文本呈现/*g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);*/-1-用Graphics2D直接输出/首字符的基线(右下部)位于用户空间中的(x,y)位置处/g2.drawLine(10,10,200,10);Imagesrc=Toolkit.getDefaultToolkit().getImage(d:/logo.gif);g2.drawImage(src,(int)x,(int)y,c);intimg_Height=src
18、.getHeight(c);intimg_width=src.getWidth(c);/System.out.println(img_Height=+img_Height+img_width=+img_width);g2.drawString(str,(float)x,(float)y+1*heigth+img_Height);g2.drawLine(int)x,(int)(y+1*heigth+img_Height+10),(int)x+200,(int)(y+1*heigth+img_Height+10);g2.drawImage(src,(int)x,(int)(y+1*heigth+i
19、mg_Height+11),c);/-2-直接构造TextLayout打印/*FontRenderContextfrc=g2.getFontRenderContext();TextLayoutlayout=newTextLayout(str,font,frc);layout.draw(g2,(float)x,(float)y+2*heigth);*/-3-用LineBreakMeasurer进行打印/*AttributedStringtext=newAttributedString(str);text.addAttribute(TextAttribute.FONT,font);LineBrea
20、kMeasurerlineBreaker=newLineBreakMeasurer(text.getIterator(),frc);/每行字符显示长度(点)doublewidth=pf.getImageableWidth();/首字符的基线位于用户空间中的(x,y)位置处Point2D.Doublepen=newPoint2D.Double(100,y+3*heigth);while(layout=lineBreaker.nextLayout(float)width)!=null)layout.draw(g2,(float)x,(float)pen.y);pen.y+=layout.getAs
21、cent();*/returnPAGE_EXISTS;default:returnNO_SUCH_PAGE;publicstaticvoidmain(Stringargs)/获取打印服务对象PrinterJobjob=PrinterJob.getPrinterJob();PageFormatpageFormat=job.defaultPage();/得到默认页格式job.setPrintable(newPrintTest1();/设置打印类try/可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印/booleana=job.printDialog();/if(a)/job.print();/catch(PrinterExceptione)e.printStackTrace();