Back to Blog

How to Repeat Table Headers and Page Headers Across Pages in a Salesforce PDF

How to Repeat Table Headers and Page Headers Across Pages in a Salesforce PDF

Make table column headings, page headers, footers, and page numbers repeat on every page of a long Salesforce PDF or Word document. Covers Word, Visualforce CSS, and the rows-split-across-pages problem.

Rahul Gawale

A three-page line item table with column headings on page one and nothing on pages two and three is hard to read. So is a contract with no page numbers, or a quote where the logo appears once and the rest of the document looks anonymous.

"Repeat the header on every page" sounds like a one-click setting. In Salesforce document generation, it depends on which technology produces the file. This guide covers the three common ones, and the pagination bugs that come with them.

There are really two different things people mean:

  1. Table headers: the column headings row of a long table, repeated each time the table continues on a new page.
  2. Page headers and footers: the logo, document title, page number, and confidentiality line printed in the margin of every page.

Repeating table headers

In Word

Word has a native setting for this, and it is the right fix for any template built in Word:

  1. Click in the first row of the table (the heading row).
  2. Open Table Properties, go to the Row tab.
  3. Check Repeat as header row at the top of each page.

If your heading takes two rows, select both rows before applying it. The setting only works for rows at the very top of the table, so a heading placed below a title row will not repeat unless you include the title row too.

Two things stop it from working:

  • The table is not a real table. Tab-aligned text and nested layout tricks do not paginate as a table.
  • The table has text wrapping around it. A floating table, set to wrap text, can ignore the header row setting. Set text wrapping to None.

If the merge engine generates rows by repeating a row, make sure the heading row stays outside the repeated region. Otherwise the heading is duplicated on every row, not once per page.

In a Visualforce PDF

PDFs rendered with renderAs="pdf" are produced by an engine that supports a subset of CSS. Use a real thead element and ask for pagination:

<style>
  table { -fs-table-paginate: paginate; width: 100%; }
  thead { display: table-header-group; }
  tr { page-break-inside: avoid; }
</style>

<table>
  <thead>
    <tr><th>Product</th><th>Qty</th><th>Price</th></tr>
  </thead>
  <tbody>
    <apex:repeat value="{!lines}" var="l">
      <tr><td>{!l.name}</td><td>{!l.qty}</td><td>{!l.price}</td></tr>
    </apex:repeat>
  </tbody>
</table>

What each line does:

  • thead with display: table-header-group marks the row as the heading to repeat.
  • -fs-table-paginate: paginate tells the PDF engine to split the table across pages and repeat the header on each.
  • page-break-inside: avoid on rows keeps a single row from being cut in half across a page boundary.

The most common mistake is putting headings in a normal tr inside tbody, so the engine has nothing to repeat.

In a drag-and-drop or visual builder

Some builders expose this as a table property. If you cannot find it, check whether the table is a native table element or an image or text block laid out to look like one. Only real tables can repeat headings.

Page headers, footers, and page numbers

In Word

Use the built-in header and footer areas (Insert, Header and Insert, Footer). Anything placed there repeats on every page. Insert page numbers with the Page Number field instead of typing them, so they update automatically. If the cover page should have no header, enable Different First Page.

In a Visualforce PDF

Use the @page rule with margin boxes:

<style>
  @page {
    size: A4;
    margin: 30mm 15mm 20mm 15mm;

    @top-center {
      content: element(header);
    }
    @bottom-right {
      content: "Page " counter(page) " of " counter(pages);
      font-size: 9pt;
    }
  }
  div.header {
    position: running(header);
  }
</style>

<div class="header">
  <img src="{!URLFOR($Resource.Logo)}" height="30" />
  Master Services Agreement
</div>

Key points:

  • counter(page) and counter(pages) produce "Page 2 of 5".
  • position: running(header) pulls a block of content into the margin box so it repeats everywhere.
  • Set the page margin large enough to hold the header. If the top margin is too small, the running header overlaps the body text.
  • Images used in headers must be reachable by the rendering engine, so a static resource is more reliable than an external URL.

Keep header CSS simple. This engine does not support modern layout features such as flexbox or grid, so use floats, tables, or fixed widths.

Pagination problems that come up

Heading orphaned at the bottom of a page. Prevent it with page-break-after: avoid on headings, so a heading is never the last thing on a page.

A row cut across two pages. Add page-break-inside: avoid to rows, as above. In Word, clear Allow row to break across pages.

Totals row alone on a new page. Keep the final rows together by wrapping the totals in their own small table with page-break-inside: avoid, or by lowering the content above it.

Header repeats but the first page looks wrong. Different first page, or a cover page with no header, needs its own setting: Different First Page in Word, or a @page :first rule in CSS.

Page numbers show as 1 of 1. The page count is calculated at render time, so a preview in HTML will not show it. Check the real PDF output.

Long text in a cell breaks the layout. Set a fixed table layout and let long words wrap:

table { table-layout: fixed; }
td { word-wrap: break-word; }

Frequently asked questions

Why doesn't my table header repeat in the PDF? Almost always because the heading is not marked as a header: it is a normal row in Word, or a tr outside thead in Visualforce. Mark it as a header row and make sure the table is a real table.

How do I add "Page X of Y" to a Salesforce PDF? In Word, insert the Page and NumPages fields in the footer. In a Visualforce PDF, use counter(page) and counter(pages) in an @page margin box.

Can I have a different header on the first page? Yes. Use Different First Page in Word, or a @page :first rule in CSS.

Why does my header overlap the content? The page margin is smaller than the header. Increase the top margin until the header fits.

Summary

Repeating headings comes down to marking them as headings: a header row in Word, a thead with pagination in CSS, and the header and footer areas for everything that belongs in the margin. Once the engine knows what is a header, it repeats it for you, and the remaining work is keeping rows and headings from splitting at awkward places.

For more on long tables, see complex table rendering in Salesforce documents.

Ready to try ZeroExport?

Start generating documents directly in your Salesforce org. No integrations, no setup overhead, no complexity.