package com.dcsoft.system.utils;

import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class PdfHeaderFooterEvent extends PdfPageEventHelper {


    /**
     * 重写页面结束时间  分别添加页眉、页脚
     */
    @Override
    public void onEndPage(PdfWriter writer, Document docment){
        try{
            this.addPageFooter(writer, docment);
        }catch(Exception e){
            log.error("添加页脚出错", e);
        }
    }

    /*
    /**
     * 页脚
     */
    private void addPageFooter(PdfWriter writer, Document docment){
        try {
            //创建字体
            BaseFont fs = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font = new Font(fs, 10);
            Phrase pageNumberPh = new Phrase("- " + writer.getPageNumber() + " -",font);
            float center = writer.getPageSize().getRight()/2;
            float bottom = writer.getPageSize().getBottom() + 10;
            ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER,pageNumberPh,center,bottom,0);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
