'Page breaks in html don't appear when embedded in a docx file with docx4j

I'm outputting a Word (docx) file using docx4j, and the page breaks aren't appearing in the document. I'm using:

hr {page-break-after: always}

for the css, but it isn't rendering as a page break in the Word document.

What html or css should I be using to get an html page break to transfer over to the docx file?



Solution 1:[1]

Worked for me, try to use this tag:

<br style=\"page-break-after: always; clear:both;\"></br>

All other tag not worked with style css.

Solution 2:[2]

Works with heading tags or paragraph tags (h1, h2, p):

I used this css style with h1 tags in my html content and successfully got the page-breaks applying after the heading in my exported word document:

String pageBreakMarker = "<h1 style=\"page-break-after: always;\"></h1>";

I think this is not working with html break tags has to do with how docx4j treats html break tags.

Even looking at the getting started sample on github, I only see the 'page-break-after' or 'page-break-before' being used with only html paragraph tags (p)

See link to Docx4j getting started guide on github below:

Does not work with html break tags:

Using docx4, I can confirm I the "page-break-after" or "page-break-before" does not work when trying to create page breaks in word docs from html content with break tags (hr):

<br style=\"page-break-after: always; clear:both;\"></br>

Solution 3:[3]

Simply add this line before heading tags or paragraph tags (h1, h2, p) :

<p style="line-height: 100%; margin-bottom: 0mm; page-break-before: always">    

When i open it in OpenOffice writer, it breaks the page.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 João Gustavo Ferreira
Solution 2
Solution 3 robert haryono