iText5报表_页眉与页脚
1.概述
iText5中并没有之前版本HeaderFooter对象设置页眉和页脚,可以利用PdfPageEventHelper来完成页眉页脚的设置工作。PdfPageEventHelper中包含以下事件处理器。
onOpenDocument() — 当打开一个文档时触发,可以用于初始化文档的全局变量。
onStartPage() — 当一个页面初始化时触发,可用于初始化页面的设置参数,但是注意这个函数触发时,该页面并没有创建好,不用利用这个函数添加内容,最好利用onEndPage()处理页面的初始化。
onEndPage() — 在创建一个新页面完成但写入内容之前触发,是添加页眉、页脚、水印等最佳时机。
onCloseDocument() — 在文档关闭之前触发,可以用于释放一些资源。
2.重载PdfPageEventHelper类
定义一个类HeaderFooter,继承了父类PdfPageEventHelper
在文档的每个页面中,必须定义一个Ractangle矩形,其中参数为art,这样在HeaderFooter类中就可以通过这个矩形,获取文档的边框位置,从而设置页眉和页脚。
代码如下:
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Element;
- import com.itextpdf.text.Phrase;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.pdf.ColumnText;
- import com.itextpdf.text.pdf.PdfPageEventHelper;
- import com.itextpdf.text.pdf.PdfWriter;
- public class HeaderFooter extends PdfPageEventHelper{
- public void onEndPage (PdfWriter writer, Document document) {
- Rectangle rect = writer.getBoxSize("art");
- switch(writer.getPageNumber() % 2) {
- case 0:
- ColumnText.showTextAligned(writer.getDirectContent(),
- Element.ALIGN_RIGHT, new Phrase("even header"),
- rect.getRight(), rect.getTop(), 0);
- break;
- case 1:
- ColumnText.showTextAligned(writer.getDirectContent(),
- Element.ALIGN_LEFT, new Phrase("odd header"),
- rect.getLeft(), rect.getTop(), 0);
- break;
- }
- ColumnText.showTextAligned(writer.getDirectContent(),
- Element.ALIGN_CENTER, new Phrase(String.format("page %d", writer.getPageNumber())),
- (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
- }
- }
调用代码如下
- import com.itext.HeaderFooter;
- import com.itextpdf.text.pdf.PdfPageEventHelper;
- import com.itextpdf.text.pdf.PdfWriter;
- import com.itextpdf.text.pdf.ColumnText;
- import com.itextpdf.text.*;
- import java.io.FileOutputStream;
- public class HeaderAndFooterDemo {
public static void main(String[] args){ - Document document = new Document(PageSize.A4, 50, 50, 50, 50);
- try{
- PdfWriter writer=PdfWriter.getInstance(document,
- new FileOutputStream("C:\\testHeaderAndFooter.pdf") );
- Rectangle rect = new Rectangle(36, 54, 559, 788);
- rect.setBorderColor(BaseColor.BLACK);
- writer.setBoxSize("art", rect);
- HeaderFooter header=new HeaderFooter();
- writer.setPageEvent(header);
- document.open();
- document.newPage();
- Paragraph par = new Paragraph("first paragraph");
- document.add(par);
- document.newPage();
- Paragraph par2 = new Paragraph("second paragraph");
- document.add(par2);
- document.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
3. 解决第X页/共Y页问题
我们通过PdfWriter中的getPageNumber()函数获取当前是第几页,但是没有办法获取文档共多少页。
我们可以利用XObject对象,iText仅在调用释放模板方法后才将PdfTemplate写入到OutputStream中,否则对象将一直保存在内存中,直到关闭文档。
我们可以给第1个页面添加template,直到最后一个页面才将内容写入到这个模板。
- 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 TableHeader 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(527);
- table.setLockedWidth(true);
- table.getDefaultCell().setFixedHeight(20);
- table.getDefaultCell().setBorder(Rectangle.BOTTOM);
- table.addCell(header);
- table.getDefaultCell().setHorizontalAlignment(
- Element.ALIGN_RIGHT);
- table.addCell(String.format("page %d of",writer.getPageNumber()));
- PdfPCell cell = new PdfPCell(Image.getInstance(total));
- cell.setBorder(Rectangle.BOTTOM);
- table.addCell(cell);
- table.writeSelectedRows(0,-1,34,803,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()-1)),2,2,0);
- }
- }
调用代码如上面HeaderAndFooterDemo.java所示。
参考文献
1.Adding page events to PdfWriter (iText 5).http://what-when-how.com/itext-5/adding-page-events-to-pdfwriter-itext-5/
2iText5参考. http://api.itextpdf.com/itext/
iText5报表_页眉与页脚的更多相关文章
- NCreport报表控件教程:设计页眉和页脚
一.设计页眉 一般来说页眉部分一般是用于包含标题的内容, 首先我们会添加列标签到页眉部分,标签都是简单的文本,标签项一般是用于在报表上显示一些描述信息,标签都是静态项,所以它们的值不会有变化. 添加标 ...
- iText导出PDF(图片,水印,页眉,页脚)
项目需要导出PDF,导出的内容包含图片和文本,而且图片的数量不确定,在网上百度发现大家都在用iText,在官网发现可以把html转换为PDF,但是需要收费,那就只能自己写了. 在开始之前先在网上百度了 ...
- openxml(二) 添加页眉,页脚
openxml 中 word 文档的结构是如下图: 其中,页眉是 header,属于headerpart 部件,页脚是footer,属于footerpart 部件,图上还有其他的东西,之后会一一介绍. ...
- iOS开发——UI_swift篇&TableView实现页眉和页脚
TableView实现页眉和页脚 在UItableView中header和footer是很常见的,而且他能让你实现很复杂的功能,我们见过最多的就是下拉刷新和上啦加载更多,当然你还可以在上面添加一个 ...
- Swift - 给表格TableView添加页眉和页脚
UITableView具有var tableHeaderView:UIView?属性和var tableFooterView:UIView?属性,可以通过给其赋值来创建列表TableView的页眉和页 ...
- C# 操作Word 文档——添加Word页眉、页脚和页码
在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...
- C# 添加Word页眉、页脚和页码
在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...
- C# 给现有PDF文档添加页眉、页脚
概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...
- JQuery Mobile - 解决页面点击时候,页眉和页脚消失问题!
当点击页面时候,页眉和页脚会消失!解决方法,在页面和页脚中加入: data-quicklinks="true" 实际使用代码: <div data-role="pa ...
随机推荐
- JS实现图片懒加载插件
一.前言 我在前几篇博客的记录中,有说自己在做一个图片懒加载的功能,然后巴拉巴拉的遇到哪些问题,结果做完了也没对懒加载这个功能做一些记录,所以这篇文章主要针对我所实现的思路,以及代码做个记录,实现不佳 ...
- 关于fasterxml-jackson发生Can not deserialize instance of异常原因验证
关于fasterxml-jackson发生Can not deserialize instance of异常原因验证 这两天线上有大量的java.lang.IllegalArgumentExcepti ...
- 今天通过npm 安装 install 的时候出现的问题
E:\Workspace_WebStorm\angular2>npm install -gC:\Users\lyx\AppData\Roaming\npm`-- angular2@0.0.0 ` ...
- c# Cache 使用实例
/// <summary> /// 创建缓存项的文件 /// </summary> /// <param name="key">缓存Key< ...
- WebForm 【发送邮件】
C#实现简单的SmtpClient发送邮件 分析 需要什么 发送邮件 -- 发送内容 -- 接收邮件 流程(各功能都适用) 创建对象 -- ...
- [PHP]算法-替换空格的PHP实现
替换空格: 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 思路: 1.先循环一遍,找出 ...
- Vim settings file on Windows
Question: I can't believe I am typing a question for a simple thing like this but here we are. I can ...
- 给OkHttp Client添加socks代理
Okhttp的使用没有httpClient广泛,网上关于Okhttp设置代理的方法很少,这篇文章完整介绍了需要注意的方方面面. 上一篇博客中介绍了socks代理的入口是创建java.net.Socke ...
- web print
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmi
原因: 同一个ip在短时间内产生太多(超过mysql数据库max_connection_errors的最大值)中断的数据库连接而导致的阻塞: 解决方法: 1.提高允许的max_connection_e ...