I am using pagination in my visualforce page according to this example: pagination example and it's only showing me the four buttons: First, Previous, Next and Last, but i need also page numbers links so i can navigate to page 5 or 8.

How can i add this to visualforce ?

TL;DR:

Write a new method to set your offset to the appropriate page:

public void fifthPage()
{
offset = 40;  // simplified from offset = (5 - 1) * 10;
}


<apex:commandButton action="{!fifthPage}" value="Page 5" />


Looking at the implementation in the example, expanding it to accommodate jumping to a specific page should only be a matter of adjust the offset by a multiple of 10 (assuming 10 rows per page).

Consider the following:

Page First is offset = 0 (the first record in the list)

Page Next is offset = offset + 10 (the use of Math.min() ensures you don't go out of bounds of the list of records)

Page Previous is offset = offset - 10 (similar to Next, the use of Math.max() ensures you don't go below 0)

Page Last is a little tricky, but in the example, it is the size of the list minus 10, keeping with the idea of our pages being jumps of 10. (It seems to assume list results that are of a size that's always divisible by 10, assuming you want consistency in your page results.)

If we wanted a specific page, all you would need to do is change your offset adjustment to a multiple of the number of rows, in the case of the example, this is 10. So, mathematically:

Page 2 is offset = 10

Page 3 is offset = 20

etc.

If you wanted it to be dynamic based on the number of rows, you would just keep a rows variable and your adjustments would be like so:

Page 2 would be offset = rows (since Page 1 is just 0, Page 2 is technically 0 + (1 * rows))

Page 3 would be offset = 2 * rows

Page 4 would be offset = 3 * rows

etc.

This gives us a general formula for the specific page:

Page N is offset = (pageNum - 1) * (rows)

Going along with the example which uses 10 rows per page by default, you could ostensibly write a method that goes like this:

public void fifthPage()
{
offset = 40;  // simplified from offset = (5 - 1) * 10;
}


This will allow you to add another command button:

<apex:commandButton action="{!fifthPage}" value="Page 5" />


You can do this for as many page numbers as you want.

For example, since you mentioned page 8, you would add this method to your controller:

public void eighthPage()
{
offset = 70;  // simplified from offset = (8 - 1) * 10;
}


<apex:commandButton action="{!eighthPage}" value="Page 8" />

• Note that apex:param works with apex:commandLink and can be made work with apex:commandButton (see e.g. bobbuzzard.blogspot.ie/2011/07/…) so it is possible to just have one next and one previous method that reference the parameter. – Keith C Mar 17 '16 at 19:22