[SalesForce] Reset Page Number in PDF generated using Visual force

Can we count page Number based on tag.

Say example visual force generates PDF from list button where 2 records are selected and result page is totally 5.
With help apex repeat tag, for the first record 3 pages and second record it is next 2 pages generated in a single file.
I want count a page number in a PDF doc as Page 1 of 3, 2 0f 3, 3 0f 3 and then again 1 of 2 and 2 of 2.

Is it possible? I am using below code to display page number it is counting from 1 to 5.

how to reset for the second record

@page { @bottom-center{ content: "Page " counter(page) " of " counter(pages); } } }

Best Answer

This seems to work nicely using the Flying Saucer CSS -fs-page-sequence: start;. I have created a very basic Visualforce page to illustrate how it can be done:

<apex:page renderas="PDF" applyHtmlTag="false" applyBodyTag="false" showHeader="false">
<head>
    <style>
        @page {
            @top-right {
               content: "Page " counter(page) " of " counter(pages);
               font-style: bold;
            }
        }

        .pageBreak {
            page-break-after: always
        }

        .counterReset {
            -fs-page-sequence: start;
        }

    </style>

</head>


<div class="pageBreak">
    1 of 1
</div>
<div class="pageBreak counterReset">
    1 of 2
</div>
<div class="">
    2 of 2
</div>

</apex:page>