iText章节中的新页面 [英] New page within chapter in iText
问题描述
我有一个包含几个部分的大章节。我需要拆分一个部分内容,使其更漂亮和可读。我尝试在预期的分页符之前使用setPageEmpty(false)和newPage(),但页面没有中断:
I have a large chapter containing several sections. I need to split one section content to make it more pretty and readable. I tried to use setPageEmpty(false) and newPage() before expected page break but the page doesn't breaks:
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(filename));
writer.setPageEvent(new PageEventHandler(doc));
doc.open();
Chapter chapter = new ChapterAutoNumber("Main info");
chapter.add(new Paragraph("Lorem ipsum dolor sit amet", font));
writer.setPageEmpty(false);
itextDocument.newPage();
在这段代码之后我将填写章节内容,最后我要写:
After this code I'm going on to fill the chapter content and finally I'm going to write:
doc.add(chapter);
但在第一段后我需要分页符。如何拆分部分内容?我使用iText 5.5
But after first paragraph I need a page break. How to split section content? I use iText 5.5
推荐答案
使用 newPage()$是没有意义的c $ c>方法,如果您想在
章
中添加新页。请看下面的代码:
It doesn't make sense to use the newPage()
method if you want to add a new page inside a Chapter
. Take a look at the following snippet:
Chapter chapter = new ChapterAutoNumber("Main info");
chapter.add(p1);
document.newPage();
chapter.add(p2);
document.add(chapter);
你看到了什么?
你看到一个章节中填充了对象 p1
和 p2
。两个对象 p1
和 p2
,不会呈现到文档
直到最后一行: document.add(章节);
仅当触发此行时, p1
实际上已添加到文档
,因为该文档不知道章节
,然后再添加它。
You see a chapter being populated with objects p1
and p2
. Both objects, p1
and p2
, aren't rendered to the document
until the very last line: document.add(chapter);
Only when this line is triggered, p1
is actually added to the document
because the document isn't aware of what happens with the chapter
before you add it.
这意味着 document.newPage()
在 <$ em> c> 之前触发,而不是 p1
和 p2
。
This means that document.newPage()
is triggered before p1
is rendered, instead of between p1
and p2
.
要解决此问题,您需要使用 Chunk.NEXTPAGE
object:
To solve this, you need to use the Chunk.NEXTPAGE
object:
Chapter chapter = new ChapterAutoNumber("Main info");
chapter.add(p1);
chapter.add(Chunk.NEXTPAGE);
chapter.add(p2);
document.add(chapter);
特殊的 Chunk
对象现在是章节
对象,将在 p1
和 p2 $之间触发新页面c $ c>。
That special Chunk
object is now part of the chapter
object, and a new page will be triggered between p1
and p2
.
这篇关于iText章节中的新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!