Click or drag to resize
Pdf Library for .NET

Repeat Table Headers

Some web pages might contain large html tables that will split over several pages. Select.Pdf Library can be set to repeat the header of the html table at the top of every pdf page that contains a part of that table.

This can be done setting a CSS property for the thead element of the html table. The property that needs to be set is: display: table-header-group.

The header is repeated when the page is rendered with the print media type, so also set CssMediaType to Print. With the default screen media type the header appears only on the first page of the table.

Sample Code

The following html table will repeat the table header at the top of each pdf page when converted to pdf.

XML
<table>
  <thead style="display: table-header-group;">
   <tr>
      <th>Month</th>
      <th>Savings</th>
   </tr>
  </thead>
  <tbody>
   <tr>
      <td>January 2009</td>
      <td>$100</td>
   </tr>
   <tr>
      <td>February 2009</td>
      <td>$80</td>
   </tr>

   <!--// ..... many table rows that will split over several pages ...... -->

   <tr>
      <td>February 2015</td>
      <td>$200</td>
   </tr>
  </tbody>
</table>

The converter renders the page with the print media type, so the header repeats on every page.

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// repeat the table headers: render with css @media print
converter.Options.CssMediaType = HtmlToPdfCssMediaType.Print;

// create a new pdf document converting the html
PdfDocument doc = converter.ConvertHtmlString(html);

// save pdf document
doc.Save("Sample.pdf");

// close pdf document
doc.Close();
See Also

Other Resources

Media Types