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

  • 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 – Daniel Ballinger Nov 6 '14 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 '14 at 0:44
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>
  • 1
    no longer lurking :D :D very crafty phil! – bigassforce Nov 19 '15 at 19:34
  • Does not work, any chance of obtaining the same result – user51525 Mar 27 '18 at 16:17
  • @karna works just fine for me – Phil Hawthorn Mar 27 '18 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.

  • 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 '14 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 '14 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.