How to create a link to a specific page 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();
p1.Add(click);
doc.Add(p1);
Anchor target = new Anchor("Target");
target.Name = "target";
doc.Add(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);
chunk.SetAction(action);


Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now