iText PdfTemplate的使用

在开发系统时,需要在PDF上写入总页数。于是在网上搜索到

 

iText加入页码

 

这篇文章。但是仍然不知道PdfTemplate是什么使用的。

 

在Itext in action 2006版 第14章刚好有个这个例子(14.2.3 PageXofY)

 

Java代码 复制代码  收藏代码
  1. /* chapter14/PageXofY.java */  
  2.   
  3.   
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import com.lowagie.text.Document;  
  8. import com.lowagie.text.DocumentException;  
  9. import com.lowagie.text.Element;  
  10. import com.lowagie.text.ExceptionConverter;  
  11. import com.lowagie.text.Paragraph;  
  12. import com.lowagie.text.Phrase;  
  13. import com.lowagie.text.Rectangle;  
  14. import com.lowagie.text.pdf.BaseFont;  
  15. import com.lowagie.text.pdf.PdfContentByte;  
  16. import com.lowagie.text.pdf.PdfPageEventHelper;  
  17. import com.lowagie.text.pdf.PdfTemplate;  
  18. import com.lowagie.text.pdf.PdfWriter;  
  19.   
  20. /** 
  21.  * This example was written by Bruno Lowagie. It is part of the book 'iText in 
  22.  * Action' by Manning Publications.  
  23.  * ISBN: 1932394796 
  24.  * http://itext.ugent.be/itext-in-action/  
  25.  * http://www.manning.com/lowagie/ 
  26.  */  
  27.   
  28. public class PageXofY extends PdfPageEventHelper {  
  29.   
  30.     /** The PdfTemplate that contains the total number of pages. */  
  31.     protected PdfTemplate total;  
  32.   
  33.     /** The font that will be used. */  
  34.     protected BaseFont helv;  
  35.   
  36.     /** 
  37.      * @see com.lowagie.text.pdf.PdfPageEvent#onOpenDocument(com.lowagie.text.pdf.PdfWriter, 
  38.      *      com.lowagie.text.Document) 
  39.      */  
  40.     public void onOpenDocument(PdfWriter writer, Document document) {  
  41.         total = writer.getDirectContent().createTemplate(100100);  
  42.         total.setBoundingBox(new Rectangle(-20, -20100100));  
  43.         try {  
  44.             helv = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,  
  45.                     BaseFont.NOT_EMBEDDED);  
  46.         } catch (Exception e) {  
  47.             throw new ExceptionConverter(e);  
  48.         }  
  49.     }  
  50.   
  51.     /** 
  52.      * @see com.lowagie.text.pdf.PdfPageEvent#onEndPage(com.lowagie.text.pdf.PdfWriter, 
  53.      *      com.lowagie.text.Document) 
  54.      */  
  55.     public void onEndPage(PdfWriter writer, Document document) {  
  56.         PdfContentByte cb = writer.getDirectContent();  
  57.         cb.saveState();  
  58.         String text = "Page " + writer.getPageNumber
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sjmz30071360

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值