Legacy notice!

iText 5 is the previous major version of iText's leading PDF SDK. iText 5 has been EOL, and is no longer developed. Switch your project to iText 7, integrating the latest developments.
Check related iText 7 content!

How can I target a page based on its number?

I know how to target any text of any PDF page using code:

Anchor click = new Anchor("Click to go to Target");
click.Reference = "#target";
Paragraph p1 = new Paragraph();
Anchor target = new Anchor("Target");
target.Name = "target";
My question is how to target a page based on its number. For example if targeted page number is 6, clicking on the Anchor text should take to 6th page.

Posted on StackOverflow on Feb 20, 2014 by Yogesh

Instead of an Anchor, you need a Chunk. To this Chunk you need to add a PdfAction. The action needs to be a gotoLocalPage() action.

For instance:

Chunk chunk = New Chunk("Go to page 5");
PdfAction action = PdfAction.GotoLocalPage(5, New PdfDestination(0), writer);