Http://www.geek-tutorials.com/java/itext/itext_bookmark_anchor.php
Package Com. Geek. Tutorial. itext. bookmarks;
Import Com. lowagie. Text. Document;
Import Com.lowagie.text.html. writable writer;
Import Com. lowagie. Text. Chunk;
Import Com. lowagie. Text. Font;
Import Com. lowagie. Text. Paragraph;
Import Com.lowagie.text.html. pdfoutline;
Import Com.lowagie.textmetadata. Metadata contentbyte;
Import Com.lowagie.text=. pdfaction;
Import Java. Io. fileoutputstream;
Import Java. AWT. color;
Public Class Anchor {
Public Anchor () Throws Exception {
Document document = New Document ();
Pdfwriter writer =
Using writer. getinstance (document,
New Fileoutputstream ( " Anchor.pdf " ));
Document. open ();
// Code 1
Font font = New Font ();
Font. setcolor (color. Blue );
Font. setstyle (font. Underline );
Document. Add ( New Chunk ( " Chapter 1 " ));
Document. Add (
New Paragraph (
New Chunk ( " Press here to go Chapter 2 " , Font)
. Setlocalgoto ( " 2 " ))); // Code 2
Document. Newpage ();
Document. Add ( New Chunk ( " Chapter 2 " )
. Setlocaldestination ( " 2 " ));
Document. Add (
New Paragraph (
New Chunk ( " Http://www.geek-tutorials.com " , Font)
. Setanchor ( " Http://www.geek-tutorials.com " ))); // Code 3
Document. Add (
New Paragraph (
New Chunk ( " Open outline.pdf chapter 3 " , Font)
. Setremotegoto ( " Outline.pdf " , " 3 " ))); // Code 4
Document. Close ();
}
Public Static Void Main (string [] ARGs ){
Try {
Anchor anchor = New Anchor ();
} Catch (Exception e ){
System. Out. println (E );
}
}
}
Code 1
Define a font instance with blue and underline styles. Just to make the links in this example look like HTML hyperlink.
Code 2
You can set a chunk of text to act as a link to direct reader to particle content on the same PDF as they click on the link.
First you set a link to a chunk. Code below instruct the chunk of text to be link to a destination labeled as "2 ".
Document. Add (new paragraph (New chunk ("press here to go chapter 2", font). setlocalgoto ("2 ")));
Then set a destination.
Document. Add (New chunk ("Chapter 2"). setlocaldestination ("2 "));
When reader click on the "press here to go chapter 2", PDF reader will jump to the location where destination "2" is set.
Code 3
This Code create a external link that displays Web Content in default Internet browser.
Document. Add (new paragraph (New chunk ("http://www.geek-tutorials.com", font). setanchor ("http://www.geek-tutorials.com"); // code 3
Code 4
This Code create a remote link to external PDF file that located in the local drive. It will open outline.pdf file and jump to destination "3 ".
Document. Add (new paragraph (New chunk ("Open outline.pdf Chapter 3", font). setremotegoto ("outline.pdf", "3"); // code 4