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

时间:2019-02-04 16:25:46

标签: java pdf itext

给出以下内容:

enter image description here

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

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

如果我可以评估这一点,可以使用document().newPage()

将我的内容推送到另一个页面

1 个答案:

答案 0 :(得分:0)

您可以使用PdfPageEvent

这是一个示例:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.*;

public class App {

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        Document document = new Document(PageSize.A6, 20, 20, 20, 20);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/tmp/out.pdf"));
        writer.setPageEvent(new PdfListener());
        document.open();
        for (int i = 0; i < 10; i++) {
            Paragraph paragraph = new Paragraph();
            paragraph.add(new Phrase("The chapter "+i));
            Chapter chapter = new Chapter(paragraph, 1);
            Section section1 = chapter.addSection("The section "+i+".1", 2);
            Section section2 = chapter.addSection("The section "+i+".2", 2);
            document.add(chapter);
            document.add(section1);
            document.add(section2);
        }
        document.close();
    }

    public static class PdfListener implements PdfPageEvent {

        public void onChapter(PdfWriter writer, Document dcmnt, float f, Paragraph prgrph) {
            if( f < 50.0f ){
                dcmnt.newPage();
            }
        }
        public void onChapterEnd(PdfWriter writer, Document dcmnt, float f) {}
        public void onSection(PdfWriter writer, Document dcmnt, float f, int i, Paragraph prgrph) {
            if( f < 50.0f ){
                dcmnt.newPage();
            }
        }
        public void onSectionEnd(PdfWriter writer, Document dcmnt, float f) {}
        public void onOpenDocument(PdfWriter writer, Document dcmnt) {}
        public void onStartPage(PdfWriter writer, Document dcmnt) {}
        public void onEndPage(PdfWriter writer, Document dcmnt) {}
        public void onCloseDocument(PdfWriter writer, Document dcmnt) {}
        public void onParagraph(PdfWriter writer, Document dcmnt, float f) {}
        public void onParagraphEnd(PdfWriter writer, Document dcmnt, float f) {}        
        public void onGenericTag(PdfWriter writer, Document dcmnt, Rectangle rctngl, String string) {}

    }
}

点(0/0)在左下角-不在页面开头