DocX icon indicating copy to clipboard operation
DocX copied to clipboard

Table header on each page

Open valimaties opened this issue 3 years ago • 2 comments

Hi. Can be made such thing? To insert a table which repeats its header on each page? Thanks

valimaties avatar Jul 15 '22 05:07 valimaties

Hi,

If you want to add a table in an header and each pages contains this table in the header, here's how:

var doc = DocX.Create("test.docx");

  doc.AddHeaders();

  var table = doc.Headers.Odd.InsertTable( 1, 2 );
  table.Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "First" );
  table.Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "Second" );

  doc.InsertParagraph().InsertPageBreakAfterSelf();
  doc.InsertParagraph().InsertPageBreakAfterSelf();
  doc.InsertParagraph().InsertPageBreakAfterSelf();

  doc.Save();

But if you have a big table, which splits on many pages and want to display the Table header on each page, you can use: table.Rows[ 0 ].TableHeader = true; var doc = DocX.Create("test.docx");

  var table = doc.InsertTable( 100, 2 );

  table.Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "HEADER_1" );
  table.Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "HEADER_2" );
  table.Rows[ 0 ].TableHeader = true;

  for( int i = 1; i < 100; ++i )
  {
    for( int j = 0; j < 2; ++j )
    {
      table.Rows[ i ].Cells[ j ].Paragraphs[ 0 ].Append( i.ToString() );
    }
  }

  doc.Save();

XceedBoucherS avatar Aug 10 '22 18:08 XceedBoucherS

Thank you. The table is big I don't know how many rows it has. So the second approach helps me. Right now I'm in a small vacantion, but I will try it when I came back to home. Thanks again.

valimaties avatar Aug 13 '22 07:08 valimaties