itext5生成x/y页码

直接上代码


1,TableFooter类

package demo.util;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class TableFooter extends PdfPageEventHelper{

	
	String header;
	PdfTemplate total;

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

	public void onOpenDocument(PdfWriter writer, Document document) {
		total = writer.getDirectContent().createTemplate(30, 16);
	}

	public void onEndPage(PdfWriter writer, Document document) {
		PdfPTable table = new PdfPTable(3);
		try {
			table.setWidths(new int[] { 24, 24, 2 });
			table.setTotalWidth(505);
			table.setLockedWidth(true);
			table.getDefaultCell().setFixedHeight(20);
			table.getDefaultCell().setBorder(Rectangle.TOP);
			table.getDefaultCell().setBorderWidth(2);
			table.getDefaultCell().setBorderColor(BaseColor.BLACK);
			table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(header);
			table.addCell(String.format("%d/", writer.getPageNumber()));
			PdfPCell cell = new PdfPCell(Image.getInstance(total));
			cell.setBorder(Rectangle.TOP);
			cell.setBorderWidth(2);
			cell.setBorderColor(BaseColor.BLACK);
			table.addCell(cell);
			table.writeSelectedRows(0, -1, 45, 45, writer.getDirectContent());

		} catch (DocumentException de) {
			throw new ExceptionConverter(de);
		}
	}

	public void onCloseDocument(PdfWriter writer, Document document) {
		ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber())), 0, 2, 0);
	}
}


2,PdfUtil 类


package demo.util;

import java.io.FileOutputStream;
import java.util.Date;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfUtil {

	
	public static void main(String[] args) {
		try {
			Document document = new Document(PageSize.A4, 45, 45, 45, 45);
			String pdfFilePath = "F:\\itext5.5.10_demo"+new Date().getTime()+".pdf";
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
			Rectangle rectangle = new Rectangle(45, 45, 559, 788);
			rectangle.setBorderColor(BaseColor.BLUE);
			writer.setBoxSize("rectangle", rectangle);
			TableFooter footer = new TableFooter();
			footer.setHeader("www.junlenet.com");
			writer.setPageEvent(footer);
			
			document.open();
			
			document.newPage();
			document.add(new Paragraph("www.junlenet.com"));
			
			document.newPage();
			document.add(new Paragraph("www.junlenet.com"));
			
			document.newPage();
			document.add(new Paragraph("www.junlenet.com"));
			
			document.newPage();
			document.add(new Paragraph("www.junlenet.com"));
			
			//document.add(createTab());
			
			document.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static PdfPTable createTab(){
		PdfPTable table = new PdfPTable(6);
		try {
			table.setLockedWidth(true);
			table.setTotalWidth(505);
			//table.setWidthPercentage(100);
			int rw = 505/6;
			table.setWidths(new int[]{rw,rw,rw,rw,rw,rw});
			for (int i = 0;i<6*5;i++) {
				table.addCell(new Paragraph(i+"-"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return table;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

3,pom.xml

<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>


更多信息,可以访问 http://developers.itextpdf.com/content/itext-5-examples


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值