iText 生成pdf分页

iText生成pdf文件时,会遇到分页(page X of Y)的需求。
iText分页主要有2个方法:
1. 创建一个document(不含page X of Y信息)放内存中,新建一个PdfReader对象,通过PdfReader对象得到PdfStamper,使用PdfStamper把分页号写入每一页 的页眉或页脚。当需要生成pdf的页面较多时,计算页号就有可能发生不准确。
2. 在Page Event(如onEndPage)里增加Page X of 信息,同时增加一个空的模板应用到所有的页面上。当onCloseDocument事件发生时, 设置Y的值到模板上,并应用到每个页面上。

下 面重点来聊聊第二种方法。

1. onStartPage() - 当一个新的页面开始时触发 Triggered when a new page is started. Don’t add content in
this event, not even a header or footer . Use this event for initializing vari-
ables or setting parameters that are page specific, such as the transition or
duration parameters.
2. onEndPage()—Triggered just before starting a new page. This is the best place to add a header, a footer, a watermark, and so on.
3. onOpenDocument()—Triggered when a document is opened, just before onStartPage() is called for the first time. This is a good place to initialize variables that will be needed for all the pages of the document.
4. onCloseDocument()—Triggered just before the document is closed. This is the ideal place to release resources (if necessary) and to fill in the total number of pages in a page X of Y footer.
5. onParagraph()—In chapter 7, “Constructing columns,” you used  get-VerticalPosition() to retrieve the current Y coordinate. With the onParagraph() method, you get this value automatically every time a new Paragraph is started.
6. onParagraphEnd()—Differs from onParagraph() in that the Y position where the paragraph ends is provided, instead of the starting position.
7. onChapter()—Similar to onParagraph(), but also gives you the title of the Chapter object (in the form of a Paragraph).
8. onChapterEnd()—Similar to onParagraphEnd(), but for the Chapter object.
9. onSection()—Similar to onChapter(), but for the Section object.
10. onSectionEnd()—Similar to onChapterEnd(), but for the Section object.
11. onGenericTag()—See section 4.6, “Generic Chunk functionality.”
以上是生成pdf时会发生的事件。
我遇到的分页问题:
当批量产生pdf时, 即把多个文件生成到一个pdf时,有这样的需求。
对所有的页面生成总页码X/Y,对被包含的子文件, 也要对其内部分页,并显示相应的页码x/y。

原理:
在document里,应用多个模板, 他们分别对应显示总页码,下面的子页码。
建两个变量来统计子页码

  1. int msgpagecount = 0;
  2. int msgtotal = 1;
  1. PdfTemplate tmplmsg;
  1. public void onEndPage(PdfWriter writer, Document document) {
  2.   int pageN = writer.getPageNumber();
  3.   String text = "message " + (pageN-msgpagecount+1) + " of ";
  4.   float len = bf.getWidthPoint(text, 8);
  5.   cb.beginText();
  6.   cb.setFontAndSize(bf, 8);
  7.   cb.setTextMatrix(280, document.bottom());
  8.   cb.showText(text);
  9.   cb.endText();
  10.   cb.addTemplate(tmplmsg, 280+len, document.bottom());
  11. }
  1. public void onParagraph(PdfWriter writer, Document document, float position) {
  2.   msgpagecount = writer.getPageNumber();
  3.   tmplmsg = cb.createTemplate(50, 50);
  4. }
  1. public void onParagraphEnd(PdfWriter writer, Document document, float position){
  2.   msgtotal=writer.getPageNumber()-msgpagecount+1;
  3.   tmplmsg.beginText();
  4.   tmplmsg.setFontAndSize(bf, 8);
  5.   tmplmsg.showText(String.valueOf(msgtotal));
  6.   tmplmsg.endText();
  7. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
itextpdf是一个用于创建和操作PDF文档的开源Java库。它提供了丰富的功能和灵活性,可以满足各种生成和编辑PDF文件的需求。 在itextpdf中,生成PDF分页是通过设置页面布局和页面大小实现的。可以使用Rectangle类来创建页面大小,并使用Document类设置页面布局。要分页,可以使用Chunk或Phrase类来添加内容,并使用add()方法将它们添加到文档中。 首先,你需要创建一个Document对象,并使用PdfWriter类将其与PDF文件关联起来。然后,你可以通过设置页面大小和页面边距来调整文档的布局。接下来,你可以使用Font类和Paragraph类来设置内容的样式和格式。 在添加内容时,你可以使用Chunk或Phrase类来创建段落,并使用add()方法将它们添加到文档中。在每个页面的末尾,你可以使用newPage()方法创建新的页面,并在新页面上继续添加内容。 最后,你需要使用close()方法关闭文档,将其保存为PDF文件。 总结起来,itextpdf生成PDF分页的步骤如下: 1. 创建一个Document对象,并与PDF文件关联。 2. 设置页面大小和页面边距。 3. 使用Font类和Paragraph类设置内容的样式和格式。 4. 使用Chunk或Phrase类创建段落,并使用add()方法将它们添加到文档中。 5. 在每个页面的末尾,使用newPage()方法创建新的页面。 6. 使用close()方法关闭文档,将其保存为PDF文件。 希望这些信息对你有帮助。如果你需要更具体的实例或代码示例,可以参考引用提供的资料。<span class="em">1</span>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值