How to Convert table Row HeadingFormat to Normal row

Hello team,

I’m currently in the process of converting the table’s Row HeadingFormat to a standard row. However, upon doing so, I’ve encountered an issue where the table header row is being removed from the second page. My objective is to retain this header as a normal row on the second page. Could you please advise on how I can achieve this?

Snippet :
table.FirstRow.RowFormat.HeadingFormat = false;

Document :
TableSample.docx (16.7 KB)

@AlpeshChaudhariDev To achieve this it is required to either split the table into multiple tables or add a copy of the header row at the beginning of each page in the table. You can use the following code to split the table at page break and then reset RowFormat.HeadingFormat flag for all rows in the document.

Document doc = new Document(@"C:\Temp\in.docx");
TableUtil.SplitTableByPages(doc);
doc.GetChildNodes(NodeType.Row, true).Cast<Row>().ToList()
    .ForEach(r => r.RowFormat.HeadingFormat = false);
doc.Save(@"C:\Temp\out.docx");
/// <summary>
/// Splits the tables in the document on page basis.
/// </summary>
public static void SplitTableByPages(Document doc)
{
    LayoutCollector collector = new LayoutCollector(doc);

    NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
    foreach (Table t in tables)
    {
        // Process only top level table in the main document's body.
        if (t.ParentNode.NodeType != NodeType.Body)
            continue;

        Table table = t;
        while (table != null)
        {
            table = SplitTalbe(table, collector);
            if (table != null)
            {
                // Do not update layout if it is not required.
                collector.Clear();
                doc.UpdatePageLayout();
            }
        }
    }
}
private static Table SplitTalbe(Table table, LayoutCollector collector)
{
    int startPageIndex = collector.GetStartPageIndex(table.FirstRow);

    int breakIndex = -1;
    int firstDataRowIndex = -1;
    // Determine index of row where page breaks. And index of the first data row.
    for (int i = 1; i < table.Rows.Count; i++)
    {
        Row r = table.Rows[i];
        if (!r.RowFormat.HeadingFormat && firstDataRowIndex < 0)
            firstDataRowIndex = i;

        int rowPageIndex = collector.GetEndPageIndex(r);
        if (rowPageIndex > startPageIndex)
        {
            breakIndex = i;
            break;
        }
    }

    if (breakIndex > 0)
    {
        Table clone = (Table)table.Clone(true);

        // Insert a cloned table after the main table.
        Paragraph para = new Paragraph(table.Document);
        para.ParagraphFormat.PageBreakBefore = true;

        table.ParentNode.InsertAfter(para, table);
        para.ParentNode.InsertAfter(clone, para);

        // Remove content after the breaking row from the main table.
        while (table.Rows.Count > breakIndex)
            table.LastRow.Remove();

        // Remove rows before the breaking row from the clonned table.
        for (int i = 1; i < breakIndex; i++)
            clone.Rows.RemoveAt(firstDataRowIndex);

        return clone;
    }

    return null;
}
1 Like

Thanks, @alexey.noskov this solution is working fine.

1 Like