iText library allows you to create and manipulate PDF, RTF or HTML documents from Java classes at runtime. com.lowagie.text.pdf package from iText library helps to manipulate PDF files.

The example below adds new pages to a PDF document and creates bookmarks for every page.

 import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

public class PDFBookmarks extends PdfPageEventHelper
{
  int i = 0;
  
  public static void main(String[] args) 
  {
    Document document = new Document();
    try 
    {
      PdfWriter writer = PdfWriter.getInstance(document, 
        new FileOutputStream("Bookmarks.pdf"));

      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      writer.setPageEvent(new Bookmarks());

      document.add(new Paragraph("This is Page 1", 
        new Font(Font.HELVETICA, 12)));
      document.newPage();
      document.add(new Paragraph("This is page 2", 
        new Font(Font.HELVETICA, 12)));
      document.newPage();
      document.add(new Paragraph("This is page 3", 
        new Font(Font.HELVETICA, 12)));
    }
    catch (Exception de) {}

    document.close();

  }

  public void onParagraph(PdfWriter writer, Document document, float position) 
  {
    i++;

    PdfContentByte cb = writer.getDirectContent();
    PdfDestination destination = new PdfDestination(PdfDestination.FITH);
    PdfOutline outline = new PdfOutline(cb.getRootOutline(), destination,
      "paragraph " + i);
  }
}