How can I repeat the first two rows in a table?

Hi @alexey.noskov,

I have a requirement that I need to do repeat header rows for each table in the document. but for the first row have done the code but I have to take the first two rows of the table and do the repeat header for each table. will you help me …this is the code I have attached screen shoot, document, and highlighted which two rows I need to repeat

doc = new Document(rObj.DestFilePath);
LayoutCollector layout = new LayoutCollector(doc);
NodeCollection table = doc.GetChildNodes(NodeType.Table, true);
foreach (Table tab in table)
{
    Row Rw1 = tab.FirstRow;
    Row Rw2 = tab.LastRow;
    if (layout.GetStartPageIndex(Rw1) != layout.GetStartPageIndex(Rw2))
    {
        if (tab.FirstRow.RowFormat.HeadingFormat == false)
        {
            tab.FirstRow.RowFormat.HeadingFormat = true;
            IsFixed = true;
        }
    }
}

header 2.PNG (10.7 KB)
Output_Source_3.2.S.4.4 Batch-analyses (ZHOPL).docx (46.0 KB)

how can I repeat the first two rows in a table? help me with this issue

Hi @alexey.noskov,

will you help me to repeat only two rows from table

@k.sukumar You can easily achieve this by setting HeadingFormat of the first two rows in your table like shown in the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Get the table.
Table table = doc.FirstSection.Body.Tables[0];

// Set HeadingFormat property for the first two rows.
table.Rows[0].RowFormat.HeadingFormat = true;
table.Rows[1].RowFormat.HeadingFormat = true;

doc.Save(@"C:\Temp\out.docx");

in.docx (15.0 KB)
out.docx (12.6 KB)

Hi @alexey.noskov,

Table table = doc.FirstSection.Body.Tables[0];

with this code, I can get only the first table right but i want to get all tables to make a repeat header rows

@k.sukumar Yes, the code is for demonstration purposes and process only the first table in the document. If you need to process all tables, you can easily achieve this by looping the tables in your document, just like you do in your code example.

Thank you @alexey.noskov.

1 Like