首页

基于java.awt.print.*定义PrinterUtil打印工具类实现对图形图像的打印代码示例

标签:PrinterUtil,打印工具类,java打印机,图像图像打印,打印服务机     发布时间:2018-10-30   

一、前言

通过java.awt.print.*定义PrinterUtil打印机工具类,实现对图形图像输出打印,详情代码示例。

基于java.awt.print.*定义PrinterUtil打印工具类实现对图形图像的打印代码示例

二、代码示例

package test;@b@@b@import java.awt.Graphics;@b@import java.awt.Image;@b@import java.awt.print.Book;@b@import java.awt.print.PageFormat;@b@import java.awt.print.Paper;@b@import java.awt.print.Printable;@b@import java.awt.print.PrinterException;@b@import java.awt.print.PrinterJob;@b@import java.io.File;@b@import java.io.IOException;@b@import javax.imageio.ImageIO;@b@@b@public class PrinterUtil {@b@	@b@	protected PageFormat pageFormat;@b@	@b@	protected int width = 730, height = 850;@b@	@b@	public PrinterUtil(){@b@		initFormat();@b@	}@b@	@b@	public PrinterUtil(int width, int height){@b@		this.width = width;@b@		this.height = height;@b@		initFormat();@b@	}@b@	@b@	private void initFormat(){@b@		pageFormat = new PageFormat(){@b@			{@b@				setPaper(new Paper(){@b@					{@b@						setSize(width, height);@b@						setImageableArea(0, 0, width, height);@b@					}@b@				});@b@			}@b@		};@b@	}@b@	@b@	/**@b@	 * 打印图片 @b@	 * @param imageFile 图片@b@	 * @throws IOException@b@	 */@b@	public void printImage(File imageFile) throws IOException{@b@		Image image = ImageIO.read(imageFile);@b@		printImage(image);@b@	}@b@	@b@	private void printImage(final Image image){@b@		Printable printable = new Printable() {@b@			@b@			@Override@b@			public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)@b@					throws PrinterException {@b@				int x = 0;@b@				int y = 0;@b@				graphics.drawImage(image, x, y, null);@b@				return Printable.PAGE_EXISTS;@b@			}@b@		};@b@		println(getBook(printable));@b@	}@b@	@b@	public void printGraphics(final BaseTemplate template){@b@		@b@		Printable printable = new Printable() {@b@			@b@			@Override@b@			public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)@b@					throws PrinterException {@b@				@b@				template.paintGraphics(graphics);@b@				return Printable.PAGE_EXISTS;@b@			}@b@		};@b@		println(getBook(printable));@b@	}@b@	@b@	/**@b@	 * 打印 graphics@b@	 * @param g@b@	 */@b@	public void printGraphics(final Graphics g){@b@		@b@		Printable printable = new Printable() {@b@			@b@			@Override@b@			public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)@b@					throws PrinterException {@b@				graphics = g;@b@				return Printable.PAGE_EXISTS;@b@			}@b@		};@b@		println(getBook(printable));@b@	}@b@	@b@	private Book getBook(Printable printable){@b@		Book book = new Book();@b@		book.append(printable, pageFormat);@b@		return book;@b@	}@b@	@b@	private void println(Book book){@b@		PrinterJob printer = PrinterJob.getPrinterJob();@b@		printer.setPageable(book);@b@		@b@		boolean doPrint = printer.printDialog();@b@		if(doPrint){@b@			try {@b@				printer.print();@b@			} catch (Exception e) {@b@				e.printStackTrace();@b@			}@b@		}@b@	}@b@	@b@	public  static  void  main(String[] args) throws Exception{@b@		@b@		PrinterUtil util=new PrinterUtil();@b@		@b@		util.printImage(new File("C:/temp/txmn1027_0.jpg"));@b@	}@b@	@b@	@b@}
import java.awt.Graphics;@b@import javax.swing.JFrame;@b@@b@public abstract class BaseTemplate extends JFrame{@b@@b@	private static final long serialVersionUID = 1L;@b@@b@	public abstract void paintGraphics(Graphics g);@b@}
  • ◆ 相关内容