iText 页码、页眉、页脚工具

iText页码,页眉,页脚

DEMO下载及实现:https://download.csdn.net/download/iosweb/10623375

1,页码工具类

package excelPOI;

import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
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;

public class PageIndex extends PdfPageEventHelper{
	
	public PdfTemplate total;

    public BaseFont bfChinese;
    
    private float documentX;
    private float documentY;

    /***
     * 设置页码坐标
     * @param documentX
     * @param documentY
     */
	public PageIndex(float documentX,float documentY) {
		this.documentX = documentX;
		this.documentY = documentY;
	}

	/**
     * 重写PdfPageEventHelper中的onOpenDocument方法
     */
    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        // 得到文档的内容并为该内容新建一个模板
        total = writer.getDirectContent().createTemplate(500, 500);
        try {

            String prefixFont = "/Users/jlpmainstay/Downloads/font/SIMYOU.TTF";
//            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, 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 = this.documentX;
        float y = this.documentY;
        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 = "/Users/jlpmainstay/Downloads/font/SIMYOU.TTF";
            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,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()+"页"));
        total.endText();
    }
}

2,页眉工具类

package excelPOI;

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

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

	//页眉事件
	public class PageHeader extends PdfPageEventHelper {
        public static PdfPTable header;
        private  int rowStart;
        private  int rowEnd;
        private  float xPos;
        private  float yPos;
        /***
         * 设置位置 范围
         * @param rowStart
         * @param rowEnd
         * @param xPos
         * @param yPos
         */
        public void setTableRange(int rowStart,  int rowEnd,  float xPos,  float yPos) {
			this.rowStart = rowStart;
			this.rowEnd = rowEnd;
			this.xPos = xPos;
			this.yPos = yPos;
		}
        public PageHeader(PdfPTable header) {
        		PageHeader.header = header;
        }

        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            //把页眉表格定位
            header.writeSelectedRows(this.rowStart, this.rowEnd, this.xPos, this.yPos, writer.getDirectContent());
        }

        /***
         * 设置图片页眉
         * @param writer
         * @param imageAddress 图片路径
         * @throws MalformedURLException
         * @throws IOException
         * @throws DocumentException
         */
        public void setTableHeader(PdfWriter writer,String imageAddress) throws MalformedURLException, IOException, DocumentException {
            PdfPTable table = new PdfPTable(1);
            table.setTotalWidth(555);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(0);
            Image image01;
            image01 = Image.getInstance(imageAddress); //图片自己传
            //image01.scaleAbsolute(355f, 10f);
            image01.setWidthPercentage(80);
            cell.setPaddingLeft(30f);
            cell.setPaddingTop(-20f);
            cell.addElement(image01);
            table.addCell(cell);
            PageHeader event = new PageHeader(table);
            writer.setPageEvent(event);
        }   
    }

3,页脚工具类

package excelPOI;

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

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

	//页脚事件
	public class PageFooter extends PdfPageEventHelper {
        public static PdfPTable footer;
        
        private  int rowStart;
        private  int rowEnd;
        private  float xPos;
        private  float yPos;
        /***
         * 设置位置 范围
         * @param rowStart
         * @param rowEnd
         * @param xPos
         * @param yPos
         */
        public void setTableRange(int rowStart,  int rowEnd,  float xPos,  float yPos) {
			this.rowStart = rowStart;
			this.rowEnd = rowEnd;
			this.xPos = xPos;
			this.yPos = yPos;
		}
        
        @SuppressWarnings("static-access")
        public PageFooter(PdfPTable footer) {
            this.footer = footer;
        }

        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            //把页脚表格定位
            footer.writeSelectedRows(this.rowStart, this.rowEnd, this.xPos, this.yPos, writer.getDirectContent());
        }

        /***
         * 页脚是图片
         * @param writer 
         * @param imageAddress 图片文件地址
         * @throws MalformedURLException
         * @throws IOException
         * @throws DocumentException
         */
        public void setTableFooter(PdfWriter writer,String imageAddress) throws MalformedURLException, IOException, DocumentException {
            PdfPTable table = new PdfPTable(1);
            table.setTotalWidth(523);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(1);
            Image image01;
            image01 = Image.getInstance(imageAddress); //图片自己传
            image01.scaleAbsoluteWidth(523);
            image01.scaleAbsoluteHeight(30f);
            image01.setWidthPercentage(100);
            cell.addElement(image01);
            table.addCell(cell);
            PageFooter event = new PageFooter(table);
            writer.setPageEvent(event);
        }

        /***
         * 页脚是文字
         * @param writer
         * @param font
         * @param title
         */
        public void setTableFooter(PdfWriter writer, Font font,String title) {
            PdfPTable table = new PdfPTable(1);
            table.setTotalWidth(520f);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(1);
            Paragraph p = new Paragraph(title, font);
            cell.setPaddingLeft(10f);
            cell.setPaddingTop(-2f);
            cell.addElement(p);
            table.addCell(cell);
            PageFooter event = new PageFooter(table);
            writer.setPageEvent(event);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值