Vertically merged cells do not keep Row from Breaking across Pages

I refer to this to do https://docs.aspose.com/words/java/working-with-columns-and-rows/ But when encountering merged cells, it is not possible to keep Row fromtest.zip (9.4 KB)
Breaking across Pages, refer to the documentation in the zip file

@yjsdfsdf The vertically merged cells are actually in different rows. So if you need to keep the vertically merged cells on the same page, it is required to keep the rows on the same page. You can achieve this using KeepWithNext and KeepTogether properties of paragraphs in the rows. For example see the following screenshot:

I’m using aspose.word, does it have an api to do it ?

@yjsdfsdf Sure it have, You can achieve this using KeepWithNext and KeepTogether properties of paragraphs in the rows.

however, the merged cells spans 3 pages, It will not work,Refer to this documenttest.zip (9.5 KB)

@yjsdfsdf And what would be the expected output if the vertically merged cell is spanned 3 pages? Could you please create the expected output in MS Word and attach it here for our reference? Please note Aspose.Words is limited to what is allowed by MS Word document format.

How can I detect this kind of cross-3-page table situation, this kind of table I don’t want to set up keepwithnext properties of paragraphs in the rows. Thank you very much!

@yjsdfsdf You can use LayoutCollector class to determine page index where node starts or ends. For example:

Document doc = new Document("C:\\Temp\\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
        
// Get the table.
Table table = doc.getFirstSection().getBody().getTables().get(0);
        
// Determine page index where the first cell starts and ends.
System.out.println("Starts at: " + collector.getStartPageIndex(table.getFirstRow().getFirstCell().getFirstParagraph()));
System.out.println("Ends at: " + collector.getEndPageIndex(table.getFirstRow().getFirstCell().getLastParagraph()));

Using KeepWithNext and KeepTogether properties of paragraphs in the rows,will result in the entire table being on one page, but that’s not what I want, are there any other suggestions

@yjsdfsdf Could you please provide you current output and expected output (you can create in MS Word)? This will allow us to better understand your requirement.

I converted doc to PDF like this, but the cells are still spread across pages, refer to the documentation。

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
for (Cell cell : (Iterable<Cell>)table.getChildNodes(NodeType.CELL, true))
{
    cell.ensureMinimum();
    for (Paragraph para : cell.getParagraphs())
        if (!(cell.getParentRow().isLastRow() && para.isEndOfCell()))
            para.getParagraphFormat().setKeepWithNext(true);
}
doc.save(dataDir + "Table.KeepTableTogether_out.pdf");

Downloads.zip (404.3 KB)

@yjsdfsdf You can try using LayoutColletor to detect whether content is spanned several pages and in this case move whole row to the next page. For example see the following code:

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

Iterable<Cell> cells = doc.getChildNodes(NodeType.CELL, true);
for (Cell cell : cells)
{
    if (collector.getNumPagesSpanned(cell) > 0)
    {
        cell.getParentRow().getFirstCell().getFirstParagraph()
                .getParagraphFormat().setPageBreakBefore(true);

        collector.clear();
        doc.updatePageLayout();
    }
}

doc.save("C:\\Temp\\out.pdf");