编程语言
首页 > 编程语言> > Java itext5预览PDF设置页眉页脚

Java itext5预览PDF设置页眉页脚

作者:互联网

如果是maven项目需要添加依赖,普通项目在官网(http://itextpdf.com/)下载对应的jar包加入即可。依赖如下:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

  

预览:

public void review(HttpServletResponse response) throws Exception {
	//预览
	response.setHeader("content-Type", "application/pdf");
	response.setCharacterEncoding("utf-8");
	response.setHeader("Content-Disposition", "inline;filename=text.pdf");

	//新建Document对象并设置纸张大小和边距(左,右, 上,下)
	Document document = new Document(PageSize.A4, 10, 20, 30, 40);

	//PDF属性(可写可不写)
	document.addAuthor("author");//作者
	document.addTitle("title");//标题
	document.addSubject("subject");//主题
	document.addKeywords("keywords");//关键字
	try {
		//建立书写器(Writer)与document对象关联,以字节流输出
		//创建 PdfWriter对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
		PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
		writer.setPageEvent(new MyHeaderFooter());// 页眉页脚(需要时设置)
		//打开document
		document.open();
		//添加内容
		setpdf(document, writer);
		//关闭Document对象和书写器(Writer)
		if(document != null){
			document.close();
			writer.close();
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}

  

内容设置:

/**
 * 具体内容设置
 */
public Document setpdf(Document document, PdfWriter writer) throws Exception {
	float hlead = 28;
	Font font1 = new Font(BaseFont.createFont( "/zhttfs/2.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED), 22);//小标宋2号

	float pleft = 45;
	float pright = 35;

	Paragraph p1 = new Paragraph("我是标题", font1);
	p1.setLeading(32);//行间距
	p1.setAlignment(Element.ALIGN_CENTER);//居中
	p1.setIndentationLeft(pleft);
	p1.setIndentationRight(pright);
	document.add(p1);

	document.newPage();

	Paragraph p2 = new Paragraph("我是标题", font1);
	p2.setLeading(32);//行间距
	p2.setAlignment(Element.ALIGN_CENTER);//居中
	p2.setIndentationLeft(pleft);
	p2.setIndentationRight(pright);
	document.add(p2);

	return document;
}

  

页眉页脚:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.IOException;

public class MyHeaderFooter extends PdfPageEventHelper {
	//总页数
	PdfTemplate totalPage;
	//字体
	Font hfFont;
	{
		try {
			hfFont = new Font(BaseFont.createFont( "/zhttfs/1.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED), 14);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//打开文档时,创建一个总页数的模版
	public void onOpenDocument(PdfWriter writer, Document document) {
		PdfContentByte cb = writer.getDirectContent();
		totalPage = cb.createTemplate(50, 14);//共 页的宽高
	}
	//一页加载完成触发,写入页眉和页脚
	public void onEndPage(PdfWriter writer, Document document) {
		//创建一个两列的表格
		PdfPTable table = new PdfPTable(2);
		try {
			table.setTotalWidth(PageSize.A4.getWidth());//总宽度为A4纸张宽度
			table.setLockedWidth(true);//锁定列宽
			table.setWidths(new int[]{50, 50});//设置每列宽度
			PdfPCell cell = new PdfPCell(new Phrase("第"+ document.getPageNumber() +"页/", hfFont));
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);//设置水平右对齐
			cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
			cell.disableBorderSide(15);//隐藏全部边框
			table.addCell(cell);
			PdfPCell cell1 = new PdfPCell(Image.getInstance(totalPage));//共 页
			cell1.setHorizontalAlignment(Element.ALIGN_LEFT);//设置水平左对齐
			cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
			cell1.disableBorderSide(15);//隐藏全部边框
			table.addCell(cell1);
			table.writeSelectedRows(0, -1, 0, 50, writer.getDirectContent());
		} catch (Exception e) {
			throw new ExceptionConverter(e);
		}

		//生成左侧页眉
		ColumnText.showTextAligned(writer.getDirectContent(),
			Element.ALIGN_LEFT, new Paragraph("左页眉", hfFont),
			document.left(), PageSize.A4.getHeight() -30, 0);

		//生成右侧页眉
		ColumnText.showTextAligned(writer.getDirectContent(),
			Element.ALIGN_RIGHT, new Paragraph("右页眉", hfFont),
			document.right(), PageSize.A4.getHeight() - 30, 0);
	}

	// 全部完成后,将总页数的pdf模版写到指定位置
	public void onCloseDocument(PdfWriter writer,Document document) {
		String text = "共" + (writer.getPageNumber()) + "页";
		ColumnText.showTextAligned(totalPage, Element.ALIGN_MIDDLE, new Paragraph(text, hfFont), 0, 0, 0);
	}

}

  

 

标签:Document,Java,页脚,ALIGN,writer,页眉,new,document
来源: https://www.cnblogs.com/waft/p/15603802.html