itext pdf 5-计算页面底部的剩余面积

u0sqgete  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(417)

鉴于以下内容:

如您所见,标题当前位于第4页,内容位于第5页。
我在这个文件中有大约25节,有没有一种方法,让一个页面上的区域,以评估如果我的标题和一些内容将能够适应它?
如果我可以评估一下,我可以使用 document().newPage()

qybjjes1

qybjjes11#

你可以用 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)位于左下角,而不是页面的开头

相关问题