Salesforce Visualforce pages support rendering as PDF for printing or downloading using the renderAs="pdf" attribute on the apex:page component. To include a persistent header or footer element on every page of the rendered PDF — such as "Page 1 of 2" or an account name header — you can use CSS @page rules with running elements. This approach defines header and footer content that automatically repeats on each page of the PDF output.
To add persistent headers and footers to a PDF-rendered Visualforce page, use CSS @page rules with running elements. This approach leverages the position: running() CSS property — supported in Salesforce's PDF rendering engine — to define content that repeats on every page of the output.
The implementation involves three key components:
Understanding the sample code structure:
The @top-center rule inside the @page block places the header content (defined by the div.header element) at the top center of every PDF page. The @bottom-left rule places the footer content (defined by the div.footer element) at the bottom left. The div.header class uses position: running(header) to register the element as a running element — meaning it repeats across all pages rather than appearing only on the first page where it is defined in the HTML source. The div.footer class works the same way with position: running(footer). The {!Account.name} and {!TODAY()} Visualforce merge fields in the header div inject dynamic Salesforce record data into the PDF header. The .pagenumber:before and .pagecount:before CSS pseudo-elements use the counter(page) and counter(pages) CSS functions to auto-populate the current page number and total page count respectively in the footer.
To implement this in your Visualforce page:
<apex:page renderAs="pdf" applyBodyTag="false" standardController="Account">
<head>
<style type="text/css" media="print">
@page {
@top-center {
content: element(header);
}
@bottom-left {
content: element(footer);
}
}
div.header {
padding: 10px;
position: running(header);
}
div.footer {
display: block;
padding: 5px;
position: running(footer);
}
.pagenumber:before {
content: counter(page);
}
.pagecount:before {
content: counter(pages);
}
</style>
</head>
<div class="header">
<div>Account name is: {!Account.name} ----------- and the date is {!TODAY()}</div>
</div>
<div class="footer">
<div>Page <span class="pagenumber"/> of <span class="pagecount"/></div>
</div>
<div class="content">
<p>Actual page body information.</p>
</div>
</apex:page>
000385370

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.