4

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); } } }

2
  • I'm not sure if the version of flying saucer that Salesforce uses to do PDF generation supports it, but you could try using counter-reset and counter-increment. w3.org/TR/CSS2/generate.html#counters Nov 6, 2014 at 20:37
  • hi had used counter-reset : page; but it is not getting reflected on the PDF, if any working example will be highly appreciated
    – vikram
    Nov 7, 2014 at 0:44

2 Answers 2

3

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>
3
  • 1
    no longer lurking :D :D very crafty phil! Nov 19, 2015 at 19:34
  • Does not work, any chance of obtaining the same result
    – user51525
    Mar 27, 2018 at 16:17
  • @karna works just fine for me Mar 27, 2018 at 16:51
1

If you use the method I referenced in this answer to a question on rendering PDFs where you essentially compile each page in your controller before sending it for rendering, you can do what you're asking.

2
  • thanks for responding... how can we know in the controller, that a single page will contain how any records if the single record has complex content show child information in sub tables.
    – vikram
    Nov 7, 2014 at 0:40
  • That will depend on the flow of your page. You'd need to keep track of the variables that are being repeated in order to do that. My recommendation would be to create a separate method in your controller for that purpose. You might even need several that call one another depending on the complexity of the page. Would be like keeping track of seconds, minutes and hours in a day with 60 seconds, 60 mins, 12 hrs for AM, then PM, etc to get a day. The more complex your page and object relationships, the more sophisticated your controller methods might need to be.
    – crmprogdev
    Nov 7, 2014 at 0:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .