ItextPdf實現頁碼(第x頁/共y頁)

一、先看一下效果:java

二、如下是代碼:git

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

/**
 *
 * @ClassName: PdfReportM1HeaderFooter
 * @Description: TODO(頁數展現 第x/共y)
 * @date 2017年9月5日 下午4:25:41
 *
 */
public class ItextPdfHeaderFooter extends PdfPageEventHelper {

    /**
     * 頁眉
     */
    public String header = "";

    /**
     * 文檔字體大小,頁腳頁眉最好和文本大小一致
     */
    public int presentFontSize = 12;

    /**
     * 文檔頁面大小,最好前面傳入,不然默認爲A4紙張
     */
    public Rectangle pageSize = PageSize.A4;

    // 模板
    public PdfTemplate total;

    // 基礎字體對象
    public BaseFont bf = null;

    // 利用基礎字體生成的字體對象,通常用於生成中文文字
    public Font fontDetail = null;

    /**
     *
     * 無參構造方法.
     *
     */
    public ItextPdfHeaderFooter() {
        
    }
    
    /**
     *
     * @param bf
     *         基礎字體大小
     * @param presentFontSize
     *         數據字體大小
     * @param pageSize
     *         文檔格式
     */
    public ItextPdfHeaderFooter(BaseFont bf, int presentFontSize, Rectangle pageSize) {
        this.bf = bf;
        this.presentFontSize = presentFontSize;
        this.pageSize = pageSize;
    }

    /**
     *
     *  構造方法.
     *
     * @param yeMei
     *            頁眉字符串
     * @param presentFontSize
     *            數據體字體大小
     * @param pageSize
     *            頁面文檔大小,A4,A5,A6橫轉翻轉等Rectangle對象
     */
    public ItextPdfHeaderFooter(String yeMei, int presentFontSize, Rectangle pageSize) {
        this.header = yeMei;
        this.presentFontSize = presentFontSize;
        this.pageSize = pageSize;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public void setPresentFontSize(int presentFontSize) {
        this.presentFontSize = presentFontSize;
    }

    /**
     *
     * TODO 文檔打開時建立模板
     *
     */
    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(50, 50);// 共 頁 的矩形的長寬高
    }

    /**
     * TODO 關閉每頁的時候,寫入頁眉,寫入'第幾頁共'這幾個字。
     */
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            if (fontDetail == null) {
                fontDetail = new Font(bf, presentFontSize, Font.NORMAL);// 數據體字體
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 1.寫入頁眉
        ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(header, fontDetail),document.left(), document.top() + 20, 0);

        // 2.寫入前半部分的 第 X頁/共
        int pageS = writer.getPageNumber();
        String foot1 = "第 " + pageS + " 頁/共";
        Phrase footer = new Phrase(foot1, fontDetail);

        // 3.計算前半部分的foot1的長度,後面好定位最後一部分的'Y頁'這倆字的x軸座標,字體長度也要計算進去 = len
        float len = bf.getWidthPoint(foot1, presentFontSize);

        // 4.拿到當前的PdfContentByte
        PdfContentByte cb = writer.getDirectContent();

        // 5.寫入頁腳1,x軸就是(右margin+左margin + right() -left()- len)/2.0F
        // ,y軸就是底邊界-20,不然就貼邊重疊到數據體裏了就不是頁腳了;注意Y軸是從下往上累加的,最上方的Top值是大於Bottom好幾百開外的。
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer,(document.rightMargin() + document.right() + document.leftMargin() - document.left() - len) / 2.0F + 20F,document.bottom() - 20, 0);
        
        // 6.寫入頁腳2的模板(就是頁腳的Y頁這倆字)添加到文檔中,計算模板的和Y軸,X=(右邊界-左邊界 - 前半部分的len值)/2.0F +
        // len , y 軸和以前的保持一致,底邊界-20
        cb.addTemplate(total,(document.rightMargin() + document.right() + document.leftMargin() - document.left()) / 2.0F + 20F,document.bottom() - 20);
        // 調節模版顯示的位置
    }

    /**
     *
     * TODO 關閉文檔時,替換模板,完成整個頁眉頁腳組件
     *
     */
    public void onCloseDocument(PdfWriter writer, Document document) {
        // 7.最後一步了,就是關閉文檔的時候,將模板替換成實際的 Y 值,至此,page x of y 製做完畢,完美兼容各類文檔size。
        total.beginText();
        total.setFontAndSize(bf, presentFontSize);// 生成的模版的字體、顏色
        String foot2 = " " + (writer.getPageNumber()) + " 頁";
        total.showText(foot2);// 模版顯示的內容
        total.endText();
        total.closePath();
    }
}

三、直接在你的使用類中加入以下代碼github

public static void setFooter(PdfWriter writer, BaseFont bf, int presentFontSize, Rectangle pageSize){  
    ItextPdfHeaderFooter headerFooter = new ItextPdfHeaderFooter(bf,presentFontSize,pageSize);
    writer.setPageEvent(headerFooter);  
}

四、最後在document.open()前面調用這個方法:字體

    參數說明 :this

        //writer : PdfWriter
        //bf : BaseFont
        // 文檔字體大小
        // 文檔頁面大小code

      

      下載地址:https://github.com/Willdas/itextpdf.git 對象

相關文章
相關標籤/搜索