Itext笔记之pdf页眉页脚
来源:互联网 发布:软件测试就业培训 编辑:程序博客网 时间:2024/05/02 01:29
效果展示:
源码:
public class App { public static void main( String[] args ) throws Exception { Document document = new Document(PageSize.A4,50,50,50,50); PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("/home/hzq/a.pdf")); BaseFont baseFont = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED); Font font = new Font(baseFont,12); Rectangle rect = new Rectangle(36, 54, 559, 788); rect.setBorderColor(BaseColor.BLACK); writer.setBoxSize("art", rect); HeaderFoot headerFoot = new HeaderFoot(); writer.setPageEvent(headerFoot); document.open(); Paragraph paragraph = new Paragraph("This is First Page 第一页",font); document.add(paragraph); document.newPage(); document.add(new Paragraph("This is Second Page 第二页",font)); document.newPage(); document.add(new Paragraph("This is Second Page 第三页",font)); document.close(); }}
HeaderFoot类:
public class HeaderFoot extends PdfPageEventHelper { private String header; private PdfTemplate template; public void setHeader(String header){ this.header = header; } @Override public void onOpenDocument(PdfWriter writer, Document document) { template = writer.getDirectContent().createTemplate(30,16); } @Override public void onEndPage(PdfWriter writer, Document document) { BaseFont baseFont = null; try { baseFont = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Font font = new Font(baseFont,12); Rectangle rect = writer.getBoxSize("art"); //添加页眉 switch(writer.getPageNumber() % 2) { case 0: ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase("偶数页页眉",font), rect.getLeft(230), rect.getTop(), 0); break; case 1: ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase("奇数页页眉",font), rect.getLeft(230), rect.getTop(), 0); break; } //添加页码 try { PdfPTable table = new PdfPTable(3); //设置表格的宽度有两种方法,分别如下 table.setTotalWidth(new float[]{80,12,40}); //将宽度锁定 table.setLockedWidth(true); table.getDefaultCell().setFixedHeight(12); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); PdfPCell cell = new PdfPCell( new Paragraph("第 "+writer.getPageNumber()+" 页/共 ",font)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); PdfPCell cell1 = new PdfPCell(Image.getInstance(template)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setBorder(Rectangle.NO_BORDER); table.addCell(cell1); PdfPCell cell2 = new PdfPCell(new Paragraph("页",font)); cell2.setBorder(Rectangle.NO_BORDER); table.addCell(cell2); table.writeSelectedRows(0,-1,235,50,writer.getDirectContent()); } catch (Exception e) { e.printStackTrace(); } } @Override public void onCloseDocument(PdfWriter writer, Document document) { ColumnText.showTextAligned(template,Element.PARAGRAPH,new Phrase(String.valueOf(writer.getPageNumber()-1)),2,2,0); }}
jar包:
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.4.5</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.4.5</version> </dependency>
项目下载路径:http://download.csdn.net/detail/mmd0308/9886919
阅读全文
1 0
- Itext笔记之pdf页眉页脚
- itext页眉页脚
- itext生成PDF设置页眉页脚等
- itext生成页眉页脚
- IText导出word之页眉页脚页码
- [转]IText导出word之页眉页脚页码
- 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页
- itext高版本的pdf的页眉和页脚信息显示方法
- ITEXT 页眉页脚页码三件套
- iText操作PDF之学习笔记
- 导出pdf之--itext
- itext pdf 加页眉和页脚加页数(二)
- PDF编辑之文字编辑以及页眉页脚的添加
- JAVA解析PDF之IText
- Java操作PDF之iText
- PDF生成方案之iText
- (转)IText导出word之页眉页脚页…
- 怎么编辑pdf文件的页眉及页脚
- 组合模式
- 类里含有静态初始化对象的执行过程和顺序
- 位运算
- mac下安装python-lxml
- JS框架 获取页面传递过来的参数
- Itext笔记之pdf页眉页脚
- GNU Radio资料链接
- SQL Server 数据导入Mysql详细教程
- Mina框架实例
- 用Tomcat发布项目供外网访问
- Nginx+FastDFS图片服务器的搭建
- 利用 TensorFlow 实现上下文的 Chat-bots
- windowSoftInputMode 简介
- jdk1.6 的 HashMap 源码分析及1.7,1.8的主要更改