itext生成pdf(附帶頁眉,頁腳,頁碼)

package cn.picclife.mwx.salesupport.marketactivity.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFUtil {

    //頁碼事件
    private static class PageXofYTest extends PdfPageEventHelper{
        public PdfTemplate total;

        public BaseFont bfChinese;

        /**
         * 重寫PdfPageEventHelper中的onOpenDocument方法
         */
        @Override
        public void onOpenDocument(PdfWriter writer, Document document) {
            // 獲得文檔的內容併爲該內容新建一個模板
            total = writer.getDirectContent().createTemplate(500, 500);
            try {

                String prefixFont = "";
                String os = System.getProperties().getProperty("os.name");
                if(os.startsWith("win") || os.startsWith("Win")){
                    prefixFont = "C:\\Windows\\Fonts" + File.separator;
                }else {
                    prefixFont = "/usr/share/fonts/chinese" + File.separator;
                }

                // 設置字體對象爲Windows系統默認的字體
                bfChinese = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            } catch (Exception e) {
                throw new ExceptionConverter(e);
            }
        }

        /**
         * 重寫PdfPageEventHelper中的onEndPage方法
         */
        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            // 新建得到用戶頁面文本和圖片內容位置的對象
            PdfContentByte pdfContentByte = writer.getDirectContent();
            // 保存圖形狀態
            pdfContentByte.saveState();
            String text = writer.getPageNumber() + "/";
            // 獲取點字符串的寬度
            float textSize = bfChinese.getWidthPoint(text, 9);
            pdfContentByte.beginText();
            // 設置隨後的文本內容寫做的字體和字號
            pdfContentByte.setFontAndSize(bfChinese, 9);

            // 定位'X/'
            float x = (document.right() + document.left()) / 2;
            float y = 56f;
            pdfContentByte.setTextMatrix(x, y);
            pdfContentByte.showText(text);
            pdfContentByte.endText();

            // 將模板加入到內容(content)中- // 定位'Y'
            pdfContentByte.addTemplate(total, x + textSize, y);

            pdfContentByte.restoreState();
        }

        /**
         * 重寫PdfPageEventHelper中的onCloseDocument方法
         */
        @Override
        public void onCloseDocument(PdfWriter writer, Document document) {
            total.beginText();
            try {
                String prefixFont = "";
                String os = System.getProperties().getProperty("os.name");
                if(os.startsWith("win") || os.startsWith("Win")){
                    prefixFont = "C:\\Windows\\Fonts" + File.separator;
                }else {
                    prefixFont = "/usr/share/fonts/chinese" + File.separator;
                }

                bfChinese = BaseFont.createFont(prefixFont + "simsun.ttc,0",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                total.setFontAndSize(bfChinese, 9);
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            total.setTextMatrix(0, 0);
            // 設置總頁數的值到模板上,並應用到每一個界面
            total.showText(String.valueOf(writer.getPageNumber() - 1));
            total.endText();
        }
    }

    //頁眉事件
    private static class Header extends PdfPageEventHelper {
        public static PdfPTable header;

        public Header(PdfPTable header) {
            Header.header = header;
        }

        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            //把頁眉表格定位
            header.writeSelectedRows(0, -1, 36, 806, writer.getDirectContent());
        }

        /**
         * 設置頁眉
         * @param writer
         * @param req
         * @throws MalformedURLException
         * @throws IOException
         * @throws DocumentException
         */
        public void setTableHeader(PdfWriter writer) throws MalformedURLException, IOException, DocumentException {
            String imageAddress = "E://TESTPDF/";
            PdfPTable table = new PdfPTable(1);
            table.setTotalWidth(555);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(0);
            Image image01;
            image01 = Image.getInstance(imageAddress + "testhead.png"); //圖片本身傳
            //image01.scaleAbsolute(355f, 10f);
            image01.setWidthPercentage(80);
            cell.setPaddingLeft(30f);
            cell.setPaddingTop(-20f);
            cell.addElement(image01);
            table.addCell(cell);
            Header event = new Header(table);
            writer.setPageEvent(event);
        }   
    }

    //頁腳事件
    private static class Footer extends PdfPageEventHelper {
        public static PdfPTable footer;

        @SuppressWarnings("static-access")
        public Footer(PdfPTable footer) {
            this.footer = footer;
        }

        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            //把頁腳表格定位
            footer.writeSelectedRows(0, -1, 38, 50, writer.getDirectContent());
        }

        /**
         * 頁腳是圖片
         * @param writer
         * @throws MalformedURLException
         * @throws IOException
         * @throws DocumentException
         */
        public void setTableFooter(PdfWriter writer) throws MalformedURLException, IOException, DocumentException {
            String imageAddress = "E://TESTPDF/";
            PdfPTable table = new PdfPTable(1);
            table.setTotalWidth(523);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(1);
            Image image01;
            image01 = Image.getInstance(imageAddress + "testfooter.png"); //圖片本身傳
            image01.scaleAbsoluteWidth(523);
            image01.scaleAbsoluteHeight(30f);
            image01.setWidthPercentage(100);
            cell.addElement(image01);
            table.addCell(cell);
            Footer event = new Footer(table);
            writer.setPageEvent(event);
        }

        /**
         * 頁腳是文字
         * @param writer
         * @param songti09
         */
        public void setTableFooter(PdfWriter writer, Font songti09) {
            PdfPTable table = new PdfPTable(1);
            table.setTotalWidth(520f);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(1);
            String string = "地址:  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX         網址:  www.xxxxxxx.com       諮詢熱線:  400x-xxx-xxx";
            Paragraph p = new Paragraph(string, songti09);
            cell.setPaddingLeft(10f);
            cell.setPaddingTop(-2f);
            cell.addElement(p);
            table.addCell(cell);
            Footer event = new Footer(table);
            writer.setPageEvent(event);
        }
    }

    public static void main(String[] args) throws Exception {
        Document document = new Document(PageSize.A4, 48, 48, 60, 65);
        // add index page.
        String path = "test.pdf";
        String dir = "E://TEST";
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }
        path = dir + File.separator + path;
        FileOutputStream os = new FileOutputStream(path);
        PdfWriter writer = PdfWriter.getInstance(document, os);

        // 設置頁面佈局
        writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
        // 爲這篇文檔設置頁面事件(X/Y)
        writer.setPageEvent(new PageXofYTest());

        String prefixFont = "";
        String oss = System.getProperties().getProperty("os.name");
        if(oss.startsWith("win") || oss.startsWith("Win")){
            prefixFont = "C:\\Windows\\Fonts" + File.separator;
        }else {
            prefixFont = "/usr/share/fonts/chinese" + File.separator;
        }
        BaseFont baseFont1 = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font songti09 = new Font(baseFont1, 9f); //宋體 小五

        document.open();

        //document.newPage();
        PdfPTable pdfPTable = new PdfPTable(1);
        // 爲報告添加頁眉,事件的發生是在生成報告以後,寫入到硬盤以前
        //Header headerTable = new Header(pdfPTable);
        //headerTable.setTableHeader(writer);
        //Footer footerTable = new Footer(pdfPTable);
        //footerTable.setTableFooter(writer, songti09);
        document.add(pdfPTable);

        for (int i = 0; i < 80; i++) {
            document.add(new Paragraph("the first page"));
        }

        //document.newPage();
        document.add(new Paragraph("the second page"));

        document.close();
        os.close();
    }
}

    
    
  
相關文章
相關標籤/搜索