一个java打印程序,求写一个方法使其可以在web项目中被前台网页的打印按钮调用实现打印功能。 50
java程序如下:publicclassPrintTestimplementsPrintable{publicintprint(Graphicsgra,PageForma...
java程序如下:
public class PrintTest implements Printable{ public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException { System.out.println("pageIndex="+pageIndex); Component c = null; String str = "伟大民族。"; Graphics2D g2 = (Graphics2D) gra; g2.setColor(Color.black); double x = pf.getImageableX(); double y = pf.getImageableY(); switch(pageIndex){ case 0: Font font = new Font("新宋体", Font.PLAIN, 9); g2.setFont(font); float[] dash1 = {2.0f}; g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f)); float heigth = font.getSize2D(); g2.drawImage(src,(int)x,(int)y,c); int img_Height=src.getHeight(c); int img_width=src.getWidth(c); 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+img_Height+11),c); return PAGE_EXISTS; default: return NO_SUCH_PAGE; } }public static void main(String[] args) { Book book = new Book(); PageFormat pf = new PageFormat(); pf.setOrientation(PageFormat.PORTRAIT); Paper p = new Paper(); p.setSize(590,840); p.setImageableArea(10,10, 590,840); pf.setPaper(p); book.append(new PrintTest(), pf); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(book); try { job.print();} catch (PrinterException e) { e.printStackTrace(); } }} 展开
public class PrintTest implements Printable{ public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException { System.out.println("pageIndex="+pageIndex); Component c = null; String str = "伟大民族。"; Graphics2D g2 = (Graphics2D) gra; g2.setColor(Color.black); double x = pf.getImageableX(); double y = pf.getImageableY(); switch(pageIndex){ case 0: Font font = new Font("新宋体", Font.PLAIN, 9); g2.setFont(font); float[] dash1 = {2.0f}; g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f)); float heigth = font.getSize2D(); g2.drawImage(src,(int)x,(int)y,c); int img_Height=src.getHeight(c); int img_width=src.getWidth(c); 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+img_Height+11),c); return PAGE_EXISTS; default: return NO_SUCH_PAGE; } }public static void main(String[] args) { Book book = new Book(); PageFormat pf = new PageFormat(); pf.setOrientation(PageFormat.PORTRAIT); Paper p = new Paper(); p.setSize(590,840); p.setImageableArea(10,10, 590,840); pf.setPaper(p); book.append(new PrintTest(), pf); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(book); try { job.print();} catch (PrinterException e) { e.printStackTrace(); } }} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询