Lets imagine we add one line of text to a document. It will be positioned 200pt below the top of the page and indented 70pt from the left side of the document. I'm going to assume you know all this already and this is by no means the complete code.
float yPos = reader.getPageSize(1).getHeight() - 200;
float xPos = 70.0f;
...
PdfContentByte cb = writer.getDirectContent();
...
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "This is some text", xPos,yPos, 0);
...
This might look something like this:
Now we want to be able to click on that text and jump to another page within the document. We can use the "setAction" method to achieve this.
This works by overlaying an interactive rectangle over the text (or whatever else) where the user can click and be taken to a defined destination. This destination can be within the same document, as we are interested in here, or in an external pdf file.
//go to page one
cb.setAction(PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FIT,0), writer), xPos, yPos, reader.getPageSize(1).getWidth() - 70, yPos+15);
setAction() defines the action to be taken (gotoLocalPage) and overlay an interactive rectangle whose bottom corner starts at (xPos, yPos) and whose top right corner sits 70pts away from the right side of the document and 15pts above the bottom of the text. (Note that the text size is 15.0 so this is a good height to choose for the overlay).
PdfAction.gotoLocalPage() specifies the pages to go to, which part of the page to show on the screen after jumping and the writer to be used. In this example we go to page 1.
We use a new PdfDestination to specify which part of the page to show after the jump. PdfDestination.FIT shows the page with its contents sized to fit the document window horizontally and vertically.
It's worth mentioning what the other PdfDestination view options are.
FIT = Fits the document window horizontally and vertically
FITB = Fits the bounding box of the contents (no margins)
FITH = Fits horizontally, with a definable parameter for the vertical coordinate of the page's top edge
FITBH = Same as above but fits the bounding box i.e no margins
FITV = Fits the document window vertically, with a definable parameter for the horizontal coordinate of the left edge of the page
FITBV = Same as above but fits the bounding box
This is very important - make sure to use PdfDestination.XYZ,-1,-1,0 if you want to keep the view etc the same when clicking on links. It will break and be off by one page otherwise :/ Depending on the view you are using - continuous, show one page.
In-depth iText information can be found in Bruno Lowagie's excellent book iText In Actionhttp://www.manning.com/lowagie2/