Page break only for certain table rows

Issue

We have a single continuous table where for each row we are avoiding page breaks within the row. However, rows that exceed the length of the page will overflow and be cut off at the bottom since the row is not allowed to page break.

Desired behavior

By default a row in the table should not page break, but if a row is longer than a page it should be allowed to page break.

Solution?

I have read Keeping Tables and Rows from Breaking across Pages, which contains a bit about setting page breaks for each row (row.getRowFormat().setAllowBreakAcrossPages(false);). But I am not sure how to determine whether a row will exceed the length of a page.

I have attached a zip file containing the HTML we use to generate the word document, our actual output and expected output. As you will see in actual.docx, item #2 continues off the page.
aspose_example.zip (15.1 KB)

Thanks in advance

@gordonca

We have converted the shared HTML to DOCX using the latest version of Aspose.Words for Java 19.7 and have not faced the shared issue.

Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue and provide you more information on it. Thanks for your cooperation.

@tahir.manzoor

I forgot to mention that our documents are landscape, if you converted the HTML to a portrait DOCX then the item probably was not long enough to cause the issue.

This is the Python code we are using to convert the HTML to DOCX:

java_input = ByteArrayInputStream(html_buf.read())
doc = Document(java_input)
java_input.close()

# convert to landscape
for section in doc.getSections().iterator():
    page = section.getPageSetup()
    page.setOrientation(Orientation.LANDSCAPE)
    for side in ['Left', 'Right', 'Bottom', 'Top']:
        getattr(page, 'set' + side + 'Margin')(
            ConvertUtil.inchToPoint(0.6))
doc.updateTableLayout()

# add page numbers
hf_col = doc.getFirstSection().getHeadersFooters()
hf = HeaderFooter(doc, PRIMARY_HEADER_FOOTER)
hf_col.add(hf)
para = Paragraph(doc)
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER)
hf.appendChild(para)
para.appendField('PAGE')
para.appendChild(Run(doc, '/'))
para.appendField('NUMPAGES')

java_output = ByteArrayOutputStream()
doc.save(java_output, SaveFormat.DOCX)

@gordonca

You are facing the expected behavior of Aspose.Words. Please use RowFormat.AllowBreakAcrossPages property as shown below to get the correct output.

Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);

// Disable breaking across pages for all rows in the table.
for (Row row : table) {
    row.getRowFormat().setAllowBreakAcrossPages(true);
}

If you still face problem, please ZIP and attach your expected output document here for our reference. We will then provide you more information abut your query along with code.

I do not want to set row.getRowFormat().setAllowBreakAcrossPages(true) for all rows, I only want to set this property for rows that will exceed the full length of a page.

Is there a way to calculate the height of a table row? I’m trying to use row.getRowFormat().getHeight() to determine whether a row will exceed the height of the page, but row.getRowFormat().getHeight() returns 0.0 for each row. I would like to use the height of a table row to decide whether to set row.getRowFormat().setAllowBreakAcrossPages to true or false.

Please see my original post for a ZIP of expected and actual output.

@gordonca

We are investigating your issue and will get back to you soon.

@gordonca

Please use the OoxmlSaveOptions as shown below to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "input.html");

for(Section section : doc.getSections())
{
    section.getPageSetup().setOrientation(Orientation.LANDSCAPE);
}

doc.updateTableLayout();

HeaderFooterCollection hf_col = doc.getFirstSection().getHeadersFooters();
HeaderFooter hf = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
hf_col.add(hf);
Paragraph para = new Paragraph(doc);
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
hf.appendChild(para);
para.appendField("PAGE");
para.appendChild(new Run(doc, "/"));
para.appendField("NUMPAGES");

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
doc.save(MyDir + "output.docx", options);
1 Like

@tahir.manzoor

That solved our issue, thank you very much!

1 Like