How can I create a header in a table for each new page with OpenXml Wordprocessing

RRR picture RRR · Sep 7, 2011 · Viewed 7.1k times · Source

I am trying to create a table with a header. I want this header to be repeated for each new page that the table takes. How can I do this in C# and OpenXml Wordprocessing?

DocumentFormat.OpenXml.Packaging.WordprocessingDocument internalDoc = 
DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(stream, true);

var tables = wordDoc.MainDocumentPart.Document.Descendants<SdtBlock>().Where
( r => r.SdtProperties.GetFirstChild<Tag>().Val.Value.StartsWith(DATA_TABLE_TAG));

Table table = tables.Descendants<Table>().Single();
//Here can I set some property to repeat the header of the table? 

Answer

Michael Csikos picture Michael Csikos · Jun 27, 2012

As Chris said, an instance of the TableHeader class is what you need. It needs to be appended to the header row's TableRowProperties:

var row = table.GetFirstChild<TableRow>();

if (row.TableRowProperties == null)
    row.TableRowProperties = new TableRowProperties();

row.TableRowProperties.AppendChild(new TableHeader());