Insertion of page breaks at each page

@alexey.noskov
I am using the repeating section table in my code which is not of builder it is as :slight_smile:

Table table = (Table) repeaterTemplate.getAncestor(NodeType.TABLE);

where repeaterTemplate is a structuredDocumentTag how can I close this table so that I can insert page break at each and every page by starting a new table for every page and closing it in the same page as we did in builder.closeTable()?

@Sri_Harsha It is required to split the table into parts. For example you can use LayoutColletor to detect where the table flows to the next page and split the table. Please see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
LayoutCollector collector = new LayoutCollector(doc);

// Get the table that needs to be stlit in parts.
Table table = doc.getFirstSection().getBody().getTables().get(0);

while (table != null)
{
    table = splitTalbe(table, collector);
    collector.clear();
    doc.updatePageLayout();
}

doc.save("C:\\Temp\\out.docx");
private static Table splitTalbe(Table table, LayoutCollector collector) throws Exception
{
    int startPageIndex = collector.getStartPageIndex(table.getFirstRow());
        
    int breakIndex = -1;
    int firstDataRowIndex = -1;
    // Determine index of row where page breaks. And index of the first data row.
    for (int i = 1; i < table.getRows().getCount(); i++)
    {
        Row r = table.getRows().get(i);
        if (!r.getRowFormat().getHeadingFormat() && firstDataRowIndex < 0)
            firstDataRowIndex = i;
            
        int rowPageIndex = collector.getStartPageIndex(r);
        if (rowPageIndex > startPageIndex)
        {
            breakIndex = i;
            break;
        }
    }
        
    if (breakIndex > 0)
    {
        Table clone = (Table)table.deepClone(true);
            
        // Insert a cloned table after the main table.
        Paragraph para = new Paragraph(table.getDocument());
            
        table.getParentNode().insertAfter(para, table);
        para.getParentNode().insertAfter(clone, para);
            
        // Remove content after the breaking row from the main table.
        while (table.getRows().getCount() > breakIndex)
            table.getLastRow().remove();
            
        // Remove rows before the breaking row from the clonned table.
        for (int i = 1; i < breakIndex; i++)
            clone.getRows().removeAt(firstDataRowIndex);
            
        return clone;
    }
        
    return null;
}

@alexey.noskov Does the above code which you have provided works if the table is already existed with all the rows generated or else it will also works if we are generating rows for the existing table but all the rows are not been added to the table yet? For example I am currently working on a existing table with 25 rows but actually I need to fit 74 rows I am inserting a page break after 25th row then after I want to split the table from 27th row position which will start from 2nd page will the code of splitTable works in my case?

@Sri_Harsha The provided code works for existing table in the document and split the document where it overflows to the next page. If it is required to split the table after the certain number of rows, the approach is similar, but the condition of splitting will be number of rows instead of change of page number.

@alexey.noskov Can you send the sample code with an example like if the page breaks at 26th position ?

@Sri_Harsha You can use the following code to split the table:

Document doc = new Document("C:\\Temp\\in.docx");

// Get the table that needs to be split in parts.
Table table = doc.getFirstSection().getBody().getTables().get(0);

int rowsAtPage = 26;
while (table != null)
{
    table = splitTableByRowsCount(table, rowsAtPage);
}

doc.save("C:\\Temp\\out.docx");
private static Table splitTableByRowsCount(Table table, int rowsAtPage)
{

    if (table.getRows().getCount() <= rowsAtPage)
        return null;

    Table clone = (Table)table.deepClone(true);

    // Insert a cloned table after the main table.
    Paragraph para = new Paragraph(table.getDocument());
    para.appendChild(new Run(table.getDocument(), ControlChar.PAGE_BREAK));

    table.getParentNode().insertAfter(para, table);
    para.getParentNode().insertAfter(clone, para);

    // Remove content after the breaking row from the main table.
    while (table.getRows().getCount() > rowsAtPage)
        table.getLastRow().remove();

    // Remove rows before the breaking row from the cloned table.
    for (int i = 0; i < rowsAtPage; i++)
        clone.getRows().removeAt(0);

    return clone;
}