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 ...
随机推荐
- 使用Asp.Net Core MVC 开发项目实践[第二篇:EF Core]
在项目中使用EF Core还是比较容易的,在这里我们使用的版本是EF Core 2.2. 1.使用nuget获取EF Core包 这个示例项目使用的是SQLSERVER,所以还需要下载Microsof ...
- [AGC 018 E] Sightseeing plan
STO ZKY ORZ Description 给定一张网格图和三个矩形,每次只能向上或向右走.你需要从矩形 \(A\) 中的一个点 \(S\) 出发,到达矩形 \(B\) 中的一个点 \(P\) , ...
- [转]Ionic国际化解决方案
本文转自:http://www.cnblogs.com/crazyprogrammer/p/7904436.html 1. 核心内容 使用Angular2的国际化(i18n)库:ngx-tra ...
- Linux中inotify软件部署及参数事件演示
声明:博主使用的是CentOS6.9的系统 参考资料: https://github.com/rvoicilas/inotify-tools/wiki http://www.ibm.com/devel ...
- SQL查询语句如何能够让指定的记录排在最后
方法如下:select * from <表名> order by case when <条件> then 1 else 0 end asc 举例:把threads表中列id值小 ...
- 【RabbitMQ】7、RabbitMQ主备复制是异步还是同步?
转自:https://yq.aliyun.com/articles/73040?spm=5176.100240.searchblog.116.RcXYdl 我们知道RabbitMQ可以配置成Queue ...
- java_查找里程
题目内容: 下图为国内主要城市之间的公路里程: 你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程. 注意:任何两个城市之间的里程都已经给出,不需要计算经第三地中转. ...
- Tomcat意外宕机分析
之前在网上看过一篇文章,是讲Tomcat进程意外退出的,我看完感觉好奇,自己也测试了下,果然是有这种问题,所以自己也借此总结一下. 先简单说下测试过程,先创建一个web服务启动 test.sh,内容如 ...
- 微信服务号获取openid方法
public function tetst(){ if(!isset($_GET['code'])){ $APPID = $this->app_id; $ran = rand(1,100); / ...
- html一些标签在不同浏览器或者不同版本浏览器的注意事项
最近在IE10下运行一个以前的做web系统发现了两个小问题: 一.图片上使用"alt"属性来添加一些文字提示信息在IE10下无法正常显示出来 上网查了一下原因:原来是现在一 ...