IText PDF 5 - 计算页面底部的剩余区域

鉴于以下内容:

http://img4.mukewang.com/629b233d0001b6d007190177.jpg

如您所见,标题当前位于第 4 页,其内容位于第 5 页。

我在本文档中有大约 25 个部分,有没有办法让页面上留下的区域,以评估我的标题和某些内容是否能够适应它?

如果我可以评估,我可以使用将我的内容推送到另一个页面document().newPage()


FFIVE
浏览 277回答 1
1回答

慕码人2483693

您可以使用PdfPageEvent这是一个示例:import com.itextpdf.text.*;import com.itextpdf.text.pdf.*;import java.io.*;public class App {&nbsp; &nbsp; public static void main(String[] args) throws FileNotFoundException, DocumentException {&nbsp; &nbsp; &nbsp; &nbsp; Document document = new Document(PageSize.A6, 20, 20, 20, 20);&nbsp; &nbsp; &nbsp; &nbsp; PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/tmp/out.pdf"));&nbsp; &nbsp; &nbsp; &nbsp; writer.setPageEvent(new PdfListener());&nbsp; &nbsp; &nbsp; &nbsp; document.open();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Paragraph paragraph = new Paragraph();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paragraph.add(new Phrase("The chapter "+i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Chapter chapter = new Chapter(paragraph, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Section section1 = chapter.addSection("The section "+i+".1", 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Section section2 = chapter.addSection("The section "+i+".2", 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.add(chapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.add(section1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.add(section2);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; document.close();&nbsp; &nbsp; }&nbsp; &nbsp; public static class PdfListener implements PdfPageEvent {&nbsp; &nbsp; &nbsp; &nbsp; public void onChapter(PdfWriter writer, Document dcmnt, float f, Paragraph prgrph) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( f < 50.0f ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dcmnt.newPage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void onChapterEnd(PdfWriter writer, Document dcmnt, float f) {}&nbsp; &nbsp; &nbsp; &nbsp; public void onSection(PdfWriter writer, Document dcmnt, float f, int i, Paragraph prgrph) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( f < 50.0f ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dcmnt.newPage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void onSectionEnd(PdfWriter writer, Document dcmnt, float f) {}&nbsp; &nbsp; &nbsp; &nbsp; public void onOpenDocument(PdfWriter writer, Document dcmnt) {}&nbsp; &nbsp; &nbsp; &nbsp; public void onStartPage(PdfWriter writer, Document dcmnt) {}&nbsp; &nbsp; &nbsp; &nbsp; public void onEndPage(PdfWriter writer, Document dcmnt) {}&nbsp; &nbsp; &nbsp; &nbsp; public void onCloseDocument(PdfWriter writer, Document dcmnt) {}&nbsp; &nbsp; &nbsp; &nbsp; public void onParagraph(PdfWriter writer, Document dcmnt, float f) {}&nbsp; &nbsp; &nbsp; &nbsp; public void onParagraphEnd(PdfWriter writer, Document dcmnt, float f) {}&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public void onGenericTag(PdfWriter writer, Document dcmnt, Rectangle rctngl, String string) {}&nbsp; &nbsp; }}点 (0/0) 位于左下角 -不在页面的开头
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java