Ravi Brahmbhatt
Ravi Brahmbhatt

Reputation: 93

Prevent Table split in apache poi

I am facing an issue while generating the docx file using apache poi library. I have multiple tables of variable sizes to be included dynamically in the document.The issue is that table gets splited between the pages.My requirement is to prevent the table from splitting between the pages.In poi library i found the method table.getRow(0).setCantSplitRow(true); but does not changes anything.Any suggestions for how to implement this.Thanks in advance.

Upvotes: 3

Views: 1708

Answers (2)

trom
trom

Reputation: 61

To make table unsplittable you should set for all paragraphs in table property "isKeepNext = true" like this (for kotlin):

fun XWPFTable.setUnsplittable(): XWPFTable {
return apply {
    rows.map { row ->
        row.tableCells.map { cell ->
            cell.paragraphs.map { paragraph ->
                if (paragraph.ctp?.pPr == null) paragraph.ctp.addNewPPr().addNewKeepLines()
                paragraph.isKeepNext = true
            }
        }
    }
}

}

So if you want to check how it works in MS Word, you can select table and include "keep with next" in paragraph settings, and finally save docuemnt in .xml and see that isKeepNext is used for all paragraphs =)

Upvotes: 0

Rahul khanvani
Rahul khanvani

Reputation: 401

Try using Table Row Function :

**setCantSplitRow(boolean split)

This attribute controls whether to allow table rows to split across pages.**

tableRow.setCantSplitRow(true);

Upvotes: 1

Related Questions