Is there a way for keeping the headers together when tables split accross pages

Is there a way for keeping the headers together when tables split across pages?
This happens when we inserthtml with builder and the table is large is 1.5 pages long. The headers don’t push down to the next page. the solutions I have found aren’t html based. When we start building the html on the page, we don’t know when the next row starts on a new page. Also the table is html so unless we know where to edit from and a way to get the first row from the html to be inserted on the page. I am not sure how this can be done.

@nightcoder It sounds like you are talking about ParagraphFormat.KeepWithNext property. In HTML you can specify it using 'page-break-after:avoid' attribute:

<p style="page-break-after:avoid">
	<span>test</span>
</p>

If this does not help, please attach your input HTML, simple code that will allow us to reproduce the problem along with your current and expected output documents.

I have tried this, I can send code if you want. I added that style in the <table style = but has no affect.
Here is the html: and pdf.
A.7z (3.5 KB)

adding code:nightCoderPOC.7z (30.0 KB)

@nightcoder The mentioned style corresponds to the paragraph’s ParagraphFormat.KeepWithNext property. So it should be applied to the paragraph, not to the table. For example see the following HTML snippet.

<tbody>
    <tr height="23" style="border-bottom:1px; background-color:#ca0937; color: #FFFFFF; margin: 0 0 0 90px;">
        <th style=" padding-left: 5px;" align="left"><p style="page-break-after:avoid"><b>CICRA</b></p></th>

Alternatively, you can loop through the tables in your document before saving and apply this properties to all paragraphs of the first table’s rows:

foreach (Table t in Doc.GetChildNodes(NodeType.Table, true))
{
    // Do not allow row to break across pages
    t.FirstRow.RowFormat.AllowBreakAcrossPages = false;
    // Keep the row with the next row so the header row will not be left alone on the page
    foreach (Paragraph p in t.FirstRow.GetChildNodes(NodeType.Paragraph, true))
        p.ParagraphFormat.KeepWithNext = true;

    // You can also enable heading format to repeat header on each page.
    t.FirstRow.RowFormat.HeadingFormat = true;
}

So I am not sure this can even apply to the table I am working with. There is no <th only <tr Also no paragraphs. I would do this by hand, if I had foreknowledge of what row would move to next page.
Do you have any other ideas?

<tbody>
    <tr>
        <td colspan="10" style="text-align: left; font-weight: bold; background-color: #ca0937; padding-left: 2px; color:#FFFFFF; height:21px;font-size:10pt;">Arrow Basic LTM Spreads (Adjusted) - Rolling Twelve Months Data Items and Ratios</td>
    </tr>
    <tr style="border-bottom:.5px solid #626262; text-align:right; height:15.4px;">
        <td style="text-align:left; font-style: italic;padding-left:3px;">Amount in Millions</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="padding-left: .5px !important;">USD</td>
        <td style="min-width: 60px; padding-right: 1px; ">USD</td>
    </tr>

@nightcoder Have you tried using the C# code I have suggested in my previous answer? Using the code the following output is produced: testPoc.docx (41.4 KB)
Is this what is expected? If not, please modify the document and attach it here for our reference. This will allow us to better understand your requirements.

Alexey, I apologize. Your example is fine, however it doesn’t address the problem because of the table I am using. Let me share that. You can come to understand.
AA.7z (3.4 KB)
As you can see, the header consists of more than one row. tbody tr tr tr 3 rows in the larger table. Also it is possible there are 2 tables within a single html that have these attributes.
Although your solution is solid with the test table I was using, it doesn’t look like it will work with these other table types. If I weren’t so constrained on how I am receiving the data, I wouldn’t be here asking about these seemingly impossible questions. bty happy new year in the near future.

@nightcoder Is it possible to mark the rows, that should be considered as header rows, with thead tag in your HTML? Like this:

<table>
    <thead>
        <tr>
            <td>This is header</td>
            <td>This is header</td>
            <td>This is header</td>
        </tr>
        <tr>
            <td>This is header</td>
            <td>This is header</td>
            <td>This is header</td>
        </tr>
    </thead>
    <tr>
        <td>This regular data</td>
        <td>This regular data</td>
        <td>This regular data</td>
    </tr>
    <tr>
        <td>This regular data</td>
        <td>This regular data</td>
        <td>This regular data</td>
    </tr>
    <tr>
        <td>This regular data</td>
        <td>This regular data</td>
        <td>This regular data</td>
    </tr>
</table>

In this case you can identify such rows in the code using RowFormat.HeadingFormat flag and set the required attributes. For example see the following code:

// Get header rows in all table in the document.
List<Row> headerRows = doc.GetChildNodes(NodeType.Row, true).Cast<Row>()
    .Where(r => r.RowFormat.HeadingFormat).ToList();

foreach (Row r in headerRows)
{
    // Do not allow row to break across pages
    r.RowFormat.AllowBreakAcrossPages = false;
    // Keep the row with the next row so the header row will not be left alone on the page
    foreach (Paragraph p in r.GetChildNodes(NodeType.Paragraph, true))
        p.ParagraphFormat.KeepWithNext = true;
}

This is something I may have to figure out. Unfortunately I don’t have control of content. And may need to devise a scheme. I really appreciate your help. We have fixed several issues based on your knowledge.

1 Like