首页>代码>java生成pdf,加水印,加页眉页脚,使用itext 5+版本>/PdfConvertor/src/com/hz/weep/HeaderFooter.java
package com.hz.weep;

import java.io.IOException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
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.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class HeaderFooter extends PdfPageEventHelper {
	@Override
	public void onEndPage(PdfWriter writer, Document document) {
		Rectangle rect = writer.getBoxSize("art");
		// 如果不设置中文处理,页眉页脚让不会被显示出来
		BaseFont bfChinese = null;
		try {
			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
		// 设置字体颜色
		fontChinese.setColor(BaseColor.GRAY);
		// 页眉
		ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("我是页眉", fontChinese),
				rect.getLeft(), rect.getTop() + 35, 0);
		// 页脚
		ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
				new Phrase(String.format("第 %d 页", writer.getPageNumber()), fontChinese),
				(rect.getLeft() + rect.getRight()) / 2 + 120, rect.getBottom() - 40, 0);
	}
}
最近下载更多
微信网友_6569863629787136  LV1 2023年7月20日
18120344519  LV4 2023年3月20日
1358849392  LV21 2022年11月11日
bearloadprogress  LV7 2022年7月15日
703129100  LV2 2021年12月13日
nyb4613  LV8 2021年11月15日
8战魂5无双8  LV43 2021年10月30日
thenyoucan  LV2 2021年5月17日
1145304128  LV12 2021年5月14日
myielm  LV1 2021年1月27日
最近浏览更多
微信网友_6569863629787136  LV1 2023年7月20日
18120344519  LV4 2023年3月20日
zj0010722  LV2 2023年3月2日
SJIGNKLI  LV2 2022年12月15日
1358849392  LV21 2022年11月11日
whfuai  LV14 2022年7月22日
bearloadprogress  LV7 2022年7月15日
benyan  LV8 2022年6月23日
ma小跳  LV5 2022年5月19日
xytthy  LV3 2022年4月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友