Java列印

weixin_34088583發表於2016-10-25

Java列印

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class Print implements Printable {
    int m_wPage;
    int m_hPage;
    int m_orientation;
    Printable m_target;
    int maxNumPage = 1;
    String title = "資料表格列印";
    Font titleFont = new Font("黑體", Font.BOLD, 14);
    boolean hasTail = true;
    int tailAlign = 0;
    int headAlign = 0;
    int topSpace = 0;
    int leftSpace = 0;
    int yStart = 0;
    int yEnd = 0;
    int xStart = topSpace;
    int xEnd = 0;
    int x = 0, y = 0;
    String strTemp = "列印內容";
    public void doPrint() {
        try {
            m_orientation = PageFormat.PORTRAIT; // 豎向列印
            m_orientation = PageFormat.LANDSCAPE; // 橫向列印
            // 設定列印物件,預設紙張
            PrinterJob prnJob = PrinterJob.getPrinterJob();
            
            PageFormat pageFormat = prnJob.defaultPage();
            pageFormat.setOrientation(m_orientation);
            m_wPage = (int) (pageFormat.getWidth()); // 豎向595
            m_hPage = (int) (pageFormat.getHeight()); // 豎向841
            // 將待列印的窗體根據預設紙張設定傳入列印物件
            prnJob.setPrintable(this, pageFormat);
            if (!prnJob.printDialog())
                return;
            prnJob.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
            System.err.println("列印錯誤:" + ex.toString());
        }
    }
    /**
     * 初始化列印引數
     */
    public void initPrintParameter() {}
    /**
     * 構造列印內容,以送印表機列印
     */
    public int print(Graphics pg, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if(pageIndex>0){ // 保證只列印一頁;
            return NO_SUCH_PAGE;
        }
        // 初始化列印引數
        initPrintParameter();
        // 將畫布設定為頁面大小
        pg.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
        int wPage = 0;
        int hPage = 0;
        // 根據印表機頁面設定調整畫布大小
        if (pageFormat.getOrientation() == pageFormat.PORTRAIT) {
            wPage = (int) pageFormat.getImageableWidth(); // 豎向451
            hPage = (int) pageFormat.getImageableHeight(); // 豎向697
        } else {
            wPage = (int) pageFormat.getImageableWidth();
            //wPage += wPage / 2; // 為什麼加增加一半?
            hPage = (int) pageFormat.getImageableHeight();
            pg.setClip(0, 0, wPage, hPage);
        }
        wPage = wPage - 2 * leftSpace;
        hPage = hPage - 2 * topSpace;
        xStart = leftSpace;
        xEnd = wPage - 2;
        // 為畫布設定顏色和字型
        int y = topSpace;
        pg.setFont(titleFont);
        pg.setColor(Color.black);
        // 畫標題,並使其居中
        Font fn = pg.getFont();
        FontMetrics fm = pg.getFontMetrics();
        y += fm.getAscent();
        alignText(title, pg, y, xStart, xEnd, headAlign);
        y += 30;
        x = leftSpace + 2;
        Font headerFont = new Font("宋體", Font.BOLD, 14);
        pg.setFont(headerFont);
        fm = pg.getFontMetrics();
        int h = fm.getAscent();
        yStart = y - 1;
        y += h;
        pg.setFont(headerFont);
        fm = pg.getFontMetrics();
        int header = y;
        h = fm.getHeight();
        // 計算行高,每頁行數,總行數和指定頁碼的起始行、結束行
        int rowH = Math.max(h, 10);
        int tailH = rowH + 30;
        int rowPerPage = 0;
        int leftPix = 0;
        if (hasTail) {
            rowPerPage = (hPage - header - tailH) / rowH;
            leftPix = (hPage - header - tailH) % rowH;
            yEnd = hPage - leftPix - tailH + 2;
        } else {
            rowPerPage = (hPage - header) / rowH;
            leftPix = (hPage - header) % rowH;
            yEnd = hPage - leftPix + 2;
        }
        pg.drawString(strTemp, x, y);
        // 畫表格邊框
        pg.drawLine(xStart, yStart, xStart, yEnd);
        pg.drawLine(xStart, yStart, xEnd, yStart);
        pg.drawLine(xEnd, yStart, xEnd, yEnd);
        pg.drawLine(xStart, yEnd, xEnd, yEnd);
        // 列印頁碼
        if (hasTail) {
            int pageNumber = pageIndex + 1;
            String s = "第" + pageNumber + "頁";
            alignText(s, pg, yEnd + 30, xStart, xEnd, tailAlign);
        }
        System.gc();
        return PAGE_EXISTS;
    }
    /**
     * 文字排列,座標在y處,顯示範圍(start-end) 0表示居中顯示,1表示左對齊,2表示右對齊
     */
    private void alignText(String s, Graphics pg, int y, int start, int end, int mode) {
        Font fn = pg.getFont();
        FontMetrics fm = pg.getFontMetrics();
        int wString = fm.stringWidth(s);
        int x = start;
        switch (mode) {
        case 0:
            if ((end - start - wString) > 0)
                x = start + (end - start - wString) / 2;
            break;
        case 1:
            break;
        case 2:
            if ((end - start - wString) > 0)
                x = start + (end - start - wString);
            break;
        }
        pg.drawString(s, x, y);
    }
    public static void main(String[] args) {
        Print p = new Print();
        p.doPrint();
    }
}

 

相關文章