java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to create bookmarks in a PDF file with Java E-mail
User Rating: / 15
PoorBest 

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);
  }
}


 Related Tips

 
Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.