Page layout and formating

Hello,
I’m writing to you because I have problems to find documentation about what I would like to do.
Let me explain : I’m creating word files (docx) by injecting HTML. The users are writing html content, and I export it in several output formats. One of these formats is docx.
My users can create tables and lists and different blocks.
My problem is about formating. Sometimes, one of these blocks is at the end of a page or is cut in half.
I would like to be able to write a peace of code that would check each page to see if the last element is a table or a list, and check if the first element of the next page is the end of a table or half of a list.
If a block is cut in half or at the wrong position, I would like to insert empty lines to make it appear on the next page.

I’m writing in java.

Could you please give me some documentation links (or maybe an example) of code, that would identify blocks as table or lists or titles, and would check the position of blocks on the page (is it at the end of a page, or is it the first element, etc…) And finally insert empty lines…

Thanks for your help,
Rodrigue

I started to try to use LayoutCollector, to find the page number, but it doesn’t work properly.
I attached a simple word and a simple test. When I execute it, it doesn’t show me the right page.

The line below should point to the first page but it doesn’t (pageStart: 2, but it should be 1)

pageStart : 2, endPageIndex : 2, nodeRUN : com.aspose.words.Paragraph@c0e420, type : 8, text : Texte de la colonne 1a

Could you please tell me what I’m doing wrongexampleDoc.zip (36.1 KB)

@rodrigue.leopold

Thanks for your inquiry. In your case, we suggest you please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "ecahier_block_endOfPage_tmp.docm");

LayoutCollector layoutCollector = new LayoutCollector(doc);
Node node = doc.getFirstSection().getBody().getFirstChild();
while (node != null && node != doc.getFirstSection().getLastChild()) {
    int startPageIndex = layoutCollector.getStartPageIndex(node);
    int endPageIndex = layoutCollector.getEndPageIndex(node);
    System.out.println("pageStart : " + startPageIndex + ", endPageIndex : " + endPageIndex + ", nodeRUN : " + node + ", type : " + node.getNodeType() + ", text : " + node.getText());
    node = node.getNextSibling();
}

Thanks for your help, I’m gona checked asap if it works. But you didn’t answer my first questions…
It’s my fault, I should have opened a second ticket. Could you please help me about my first questions, there are much more important…

Finally I found the answer myself…
I did not know that you had the options keepWithNext or keepTogether. Once I added these words to my research I found a lot of documentation… It’s even possible in html by adding style=“page-break-after:avoid”
You can close the ticket.

@rodrigue.leopold

Thanks for your feedback. It is nice to hear from you that you have solved your query. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

We suggest you please read the following article. Hope this helps you.

Keeping Tables and Rows from Breaking across Pages