How to make the header and table data on the same page when there are fewer rows in the table

How to make the header and table data on the same page when there are fewer rows in the table
file.zip (23.8 KB)

@Mikeykiss Unfortunately, your requirements are not clear enough. As I can see both attached documents look the same on my side:

Could you please elaborate the requirements in more details. Do you need the first row of the table to be moved to the next page? If so you can set keep with next option in paragraphs in the first row of the table:

@alexey.noskov Hello, as you mentioned, this option can indeed move the header to the same page as the table data. How is it implemented in Java code

@Mikeykiss You can use the following code to achieve the same programmatically:

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

// Get the table.
Table t = doc.getFirstSection().getBody().getTables().get(0);
// Set "Keep with next" in the first row of the table.
for (Paragraph p : (Iterable<Paragraph>)t.getFirstRow().getChildNodes(NodeType.PARAGRAPH, true))
    p.getParagraphFormat().setKeepWithNext(true);

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

@alexey.noskov Thank you, this code is very helpful to me

1 Like