使用PrinterJob进行分页打印

简介: 使用PrinterJob进行分页打印

 https://blog.csdn.net/quantum7/article/details/109055137


 这一篇博文是使用的PrintJob。实际上还可以使用PrinterJob。具体代码是:

package taishan;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;
public final class PrinterFrame extends TFrame implements Printable
{
    private final static AffineTransform CLOCKWISE_TX     = AffineTransform.getRotateInstance( Math.PI / 2);
    private final static String FONT_NAME_SONGTI = "宋体";
    private final static int FONT_SIZE  = 30;
    private final static String TAISHAN = "泰山Office";
    private final static int   COUNTER_X = 3;
    private final static int   COUNTER_Y = 1;
    private final static int   START_X   = 100;
    private final static int   START_Y   = 100;
    private final static int   STEP      = 50;
    public PrinterFrame()
    {
        this.getContentPane().setBackground(Color.WHITE);
        Font font = new Font(FONT_NAME_SONGTI, 0, FONT_SIZE);
        font = font.deriveFont(CLOCKWISE_TX);
        this.setFont(font);
    }
    private void drawTest(Graphics g)
    {
        int startX = START_X + COUNTER_X * STEP;
        int startY = START_Y + COUNTER_Y * STEP;;
        SimpleDateFormat dft = new SimpleDateFormat("HH:mm:ss");
        String text = dft.format(new Date());
        g.drawLine(startX, startY, startX+100, startY);
        g.drawString(text, startX, startY);
        startX += STEP;
        startY += STEP;
        g.drawLine(startX, startY, startX,     startY+200);
        g.drawString(TAISHAN, startX, startY);
    }
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        drawTest(g);
    }
    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
    {
        System.out.println(" print "+pageIndex);
        if (pageIndex > 0)
        {
            return Printable.NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D)graphics;
        drawTest(g2d);
        g2d.setPaint(Color.BLACK);
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        Font font = new Font("宋体", Font.BOLD, 12);
        g2d.setFont(font);
        g2d.drawString("泰山信息科技", 100, 100);
        font = new Font("黑体", Font.ITALIC, 18);
        g2d.setFont(font);
        g2d.drawString("泰山信息科技", 100, 400);
        return Printable.PAGE_EXISTS;
    }
    private static void startPrint()
    {
        HashAttributeSet hs = new HashAttributeSet();
        String printName = PrintServiceLookup.lookupDefaultPrintService().getName();
        hs.add(new PrinterName(printName, null));
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, hs);
        if (printServices.length == 0)
        {
            return;
        }
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        Book book = new Book();
        book.append(new PrinterFrame(), printerJob.defaultPage());
        printerJob.setPageable(book);
        try
        {
            printerJob.setPrintService(printServices[0]);
            printerJob.print();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        PrintFrame frame = new PrintFrame();
        frame.setSize(1000, 600);
        frame.setVisible(true);
        startPrint();
    }
}
目录
相关文章
|
7月前
|
前端开发
bladex中自定义分页的写法~
bladex中自定义分页的写法~
|
7月前
|
前端开发
bladex自定义分页
bladex自定义分页
|
SQL Oracle 关系型数据库
什么是分页?如何使用分页?(一)
什么是分页?如何使用分页?
189 0
|
SQL 存储 关系型数据库
什么是分页?如何使用分页?(二)
什么是分页?如何使用分页?
82 0
|
数据库
pagehelper分页查询明明下一页没有数据了却还是返回了数据
pagehelper分页查询明明下一页没有数据了却还是返回了数据
515 0
|
JavaScript 前端开发
html打印表格每页都有的表头和打印分页
本文转载:http://www.cnblogs.com/RitchieChen/archive/2008/07/30/1256829.html  在做项目的时候碰到的。用户要求,页面呈现太长时,打印的时候,要求,每页上都要有表头。
1860 0
|
前端开发
分页重复问题思考
目前项目中存在一个问题,列表会出现数据重复! 原因很容易想到,由于排序原因新添加数据会排在顶部。 勤劳的我们又要开始摸头了 :-)
254 0
使用PrinterJob进行分页打印
使用PrinterJob进行分页打印
209 0
sqlsever2008 简单分页查询例子
工资从高到低排序 输出工资是第4到6行数据 select top 3 * from emp where EMPNO not in(select top 3 EMPNO from emp order by sal des...
1157 0