使用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();
    }
}
上一篇:c++ 副本构造器


下一篇:spring-junit中使用 log4j或者logback 打印spring日志