itext pdf 加頁眉和頁腳加頁數(二)

新建class:

import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
    /**
     * 設置打印單據的頭部和底部信息
     *
     * @author
     *
     */
public    class HeadFootInfoPdfPageEvent extends PdfPageEventHelper {
        //自定義傳參數
        public String pdfName;//出入庫名稱
        public String date;//日期
        public String type;//單號類型
        public String code;//單號
        public PdfTemplate tpl;
        BaseFont bfChinese;

        //無參構造方法
        public HeadFootInfoPdfPageEvent() {
            super();
        }

        //有參構造方法
        public HeadFootInfoPdfPageEvent(String PdfName,String Date,String Type,String Code) {
            super();
            this.pdfName=PdfName;
            this.date = Date;
            this.type=Type;
            this.code = Code;
            try {
                bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public void onOpenDocument(PdfWriter writer, Document document) {
            tpl = writer.getDirectContent().createTemplate(100, 20);
        }

        //實現頁眉和頁腳的方法
        public void onEndPage(PdfWriter writer, Document document) {
            try {
                String[] riqi = date.split("-");
                PdfContentByte headAndFootPdfContent = writer.getDirectContent();
                headAndFootPdfContent.saveState();
                headAndFootPdfContent.beginText();
                //設置中文
                headAndFootPdfContent.setFontAndSize(bfChinese, 12);
                //文檔頁頭信息設置  
                float x = document.top(-20);
                float x1 = document.top(-5);
                //頁頭信息中間  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, pdfName, (document.right() + document.left()) / 2, x, 0);
                //頁頭信息左面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_LEFT, riqi[0] + "年" + riqi[1] + "月" + riqi[2] + "日",
                        document.left() + 100, x1, 0);
                //頁頭信息中間  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, type+"庫單號:" +code+ "",
                        (document.right() + document.left()) / 2, x1, 0);
                //頁頭信息右面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_RIGHT, " 單位:冊", document.right() - 100, x1, 0);
                //文檔頁腳信息設置  
                float y = document.bottom(-20);
                float y1 = document.bottom(-35);
                //頁腳信息左面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_LEFT, "儲運部負責人:", document.left() + 100, y, 0);
                //頁腳信息中間  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, "   庫管員:    ", (document.right() + document.left()) / 2, y, 0);
                //頁腳信息右面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_RIGHT, " 經手人:", document.right() - 100, y, 0);
                //添加頁碼
                //頁腳信息中間  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, "--第" + document.getPageNumber(),
                        (document.right() + document.left()) / 2, y1, 0);
                //在每頁結束的時候把“第x頁”信息寫道模版指定位置  
                headAndFootPdfContent.addTemplate(tpl, (document.right() + document.left()) / 2 + 15, y1);//定位“y頁” 在具體的頁面調試時候需要更改這xy的座標  
                headAndFootPdfContent.endText();
                headAndFootPdfContent.restoreState();
            } catch (Exception de) {
                de.printStackTrace();
            }
        }

        public void onCloseDocument(PdfWriter writer, Document document) {
            //關閉document的時候獲取總頁數,並把總頁數按模版寫道之前預留的位置  
            tpl.beginText();
            tpl.setFontAndSize(bfChinese, 12);
            tpl.showText("頁,共" + Integer.toString(writer.getPageNumber() - 1) + "頁--");
            tpl.endText();
            tpl.closePath();//sanityCheck();  
        }
    }
    
方法調用如下:下document的open之前寫:

//設置pdf每頁的頁眉和頁腳
            writer.setPageEvent(new HeadFootInfoPdfPageEvent("頁眉的名稱",stolist.get(0).getDate("stsu_data").toString(),"入",stsu.getStr("stsu_code")));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章