Search code examples
itextitext7

LayoutResult more one page in Itext7


i ask this: Remove the first and last lines properties in the paper Itext7

and if i do it:

 PdfWriter pdfWriter = new PdfWriter(dest);


    PdfDocument pdfDoc = new PdfDocument(pdfWriter);
    Div div = new Div();
    Document doc = new Document(pdfDoc, PageSize.A5);
    doc.setMargins(0,0,0,36);

    for (int i = 0; i <50 ; i++) {
        ListItem listItem = new ListItem();
        String s= "hello "+i;
        Paragraph p = new Paragraph();
        for (int j = 0; j <s.length() ; j++) {


            p.add("HELLO " +I);


        }


  LayoutResult result = div.createRendererSubTree().setParent(doc.getRenderer()).layout(new LayoutContext(new LayoutArea(0,PageSize.A5)));


List<IRenderer> childRendererParagraph = result.getSplitRenderer().getChildRenderers();

childRendererParagraph contain Paragraphs only from first page.And i don't know how many pages well be in pdf


Solution

  • As I mentioned in the answer to your previous question,

    split renderer represent the part of the content which iText can place on the area, overflow - the content which overflows.

    So if you want to layout the rest of the content, you can perform the same operation (layout) on your overflowRenderer.

    The code is as follows:

        LayoutResult firstPageResult = div.createRendererSubTree().setParent(doc.getRenderer()).layout(new LayoutContext(new LayoutArea(0, PageSize.A5)));
        LayoutResult secondPageResult  = firstPageResult.getOverflowRenderer().layout(new LayoutContext(new LayoutArea(1, PageSize.A5)));
    

    Once the content has been fully placed, the overflowRenderer will be null.