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 ...
随机推荐
- Beego学习笔记
Beego学习笔记 Go 路由(Controller) 路由就是根据用户的请求找到需要执行的函数或者controller. Get /v1/shop/nike ShopController Get D ...
- 实验吧 php
Once More 题目地址:http://ctf5.shiyanbar.com/web/more.php 打开直接有源码: <?php if (isset ($_GET['password'] ...
- Deep learning with Python 学习笔记(8)
Keras 函数式编程 利用 Keras 函数式 API,你可以构建类图(graph-like)模型.在不同的输入之间共享某一层,并且还可以像使用 Python 函数一样使用 Keras 模型.Ker ...
- EXISTS 执行顺序
select * from a where a.s_status=1 and exists (select orderid from b where a.orderid=b.orderid) exis ...
- Asp.Net MVC3 简单入门详解过滤器Filter(转载)
前言 在开发大项目的时候总会有相关的AOP面向切面编程的组件,而MVC(特指:Asp.Net MVC,以下皆同)项目中不想让MVC开发人员去关心和写类似身份验证,日志,异常,行为截取等这部分重复的代码 ...
- Linux-iconv命令之批处理(18)
iconv命令是用来转换文件的编码方式的,比如它可以将UTF8编码的转换成GB18030的编码,反过来也行 常用选项 -f font1 :(from)将font1型的字符编码进行转换 -t font2 ...
- canvas-5Bezier-QuadraticCurveTo.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 在vue中赋值的路径没有被编译
当我们跑起来的时候,f12会看到相对路径,但是此时会报错,说找不到图片,这时候有其中一个办法,直接 require进去. 这时候就可以成功显示图片,但是路径会不一样,因为编译出来. 至于如何props ...
- Linux环境下运行简单java程序
一.安装java 1.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213315 ...
- Windows中几个内存相当的指标
以下几个内存大小相当: IS:虚拟内存任务管理器:提交内存进程对象上的:PrivateMemorySize64,性能计数器:Process\Private Bytes