Add Page number using Page Property of CSS

20 / Aug / 2010 by Uday Pratap Singh 3 comments

In my recent Nodejs project, we were required to generate the pdf document from HTML page. We are using iText renderer for doing this. The client had the requirement that each page bottom right needed to have a number on it. All these things need to be replicated on every page.

So for doing this we need to know more about the CSS3 page properties. I found the solution in this documentation http://www.w3.org/TR/css3-page/ and http://www.w3.org/TR/CSS21/generate.html#counters

For doing this I did something like

@page {
        margin-top: 149px;
        margin-left: 2px;
        margin-bottom: 40px;
        margin-right: 2px;
        size: landscape;
        counter-increment: page;

     @bottom-right {
padding-right:20px;
        content: "Page " counter(page);
      }

    }

We added an incremental page counter in the page property which can be reset as well. As my counter was incrementing after each page so I defined counter-interment at page level, although depending on the cases you can increment the counter before or after an occurrence of any element like.

.incrementClass:before {
   counter-increment: page;
}  

in above example, counter will be incremented before the incrementClass appears on the page.
Hope it helps

FOUND THIS USEFUL? SHARE IT

comments (3)

Leave a Reply

Your email address will not be published. Required fields are marked *