java打印预览

java实现对表格的打印预览。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.togest.tgyd.mmi.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *表格打印类
 
 */
public class PrintTableUtil implements Printable {

    /**
     * 待打印表格
     */
    private JTable table = null;

    /**
     * 构造器
     * @param table
     */
    public PrintTableUtil(JTable table) {
        this.table = table;
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        Graphics2D g2 = (Graphics2D) graphics;
        g2.setFont(new Font("仿宋体", 0, 13));
        g2.setColor(Color.black);
        // 取得默认字符的高度和长度
        int fontHeight = g2.getFontMetrics().getHeight();
        int fontDesent = g2.getFontMetrics().getDescent();
        // 取得页面的高度和长度
        double pageHeight = pageFormat.getImageableHeight() - fontHeight;
        double pageWidth = pageFormat.getImageableWidth();
        // 取得表格的长度
        double tableWidth = (double) table.getColumnModel().getTotalColumnWidth();
        // 取得表格放大、缩小的百分比
        double scale = 1;
        if (tableWidth >= pageWidth) {
            scale = pageWidth / tableWidth;
        }
        // 取得表格标题的打印长度
        double headerHeightOnPage = table.getTableHeader().getHeight()
                * scale;
        // 取得表格的打印长度
        double tableWidthOnPage = tableWidth * scale;
        // 取得表格一行的高度
        double oneRowHeight = (table.getRowHeight() + table.getRowMargin())
                * scale;
        // 取得一页表格的行数
        int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage) / oneRowHeight);
        // 取得一页表格行数的高度
        double pageHeightForTable = oneRowHeight * numRowsOnAPage;
        // 取得总页数
        int totalNumPages = (int) Math.ceil(((double) table.getRowCount())
                / numRowsOnAPage);
        // 如果当前页大于总页数,不执行打印命令
        if (pageIndex >= totalNumPages) {
            return NO_SUCH_PAGE;
        }
        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        // 在页尾处写上页码
        g2.drawString("第" + (pageIndex + 1) + "页", (int) pageWidth / 2 - 35,
                (int) (pageHeight + fontHeight - fontDesent));
        g2.translate(0f, headerHeightOnPage);
        g2.translate(0f, -pageIndex * pageHeightForTable);
        // 使最后一页也按照全页处理打印
        g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable));
        g2.scale(scale, scale);
        // 打印表格和数据
        table.print(g2);
        g2.scale(1 / scale, 1 / scale);
        g2.translate(0f, pageIndex * pageHeightForTable);
        g2.translate(0f, -headerHeightOnPage);
        g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(headerHeightOnPage));
        g2.scale(scale, scale);
        // 打印表格的标题
        table.getTableHeader().paint(g2);
        return Printable.PAGE_EXISTS;
    }

    public void print() {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        if (job.printDialog()) {
            try {
                job.print();
            } catch (Exception exc) {
                JOptionPane.showMessageDialog(null, "<html>打印" + "<font color=red>有问题</font>,请检查是否安装了打印机驱动或者配置了打印机!");
            }
        }
    }
}
 

java打印预览_第1张图片

你可能感兴趣的:(java)