Add bookmark : Bookmarks « PDF « Java Tutorial






import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.SimpleBookmark;

public class MainClass {

  public static void main(String[] args) throws Exception {
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfReader reader = new PdfReader("test.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("my.pdf"));
    stamper.insertPage(1, PageSize.A4);
    PdfContentByte cb = stamper.getOverContent(1);
    cb.beginText();
    cb.setFontAndSize(bf, 18);
    cb.setTextMatrix(36, 770);
    cb.showText("Inserted Title Page");
    cb.endText();

    List list = SimpleBookmark.getBookmark(reader);
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("Title", "Title Page");
    map.put("Action", "GoTo");
    map.put("Page", "1 FitH 806");
    list.add(0, map);
    stamper.setOutlines(list);
    stamper.close();
  }
}








29.33.Bookmarks
29.33.1.Add bookmark
29.33.2.Read PDF File to retrieve the bookmarks
29.33.3.Set bookmark title
29.33.4.Pdf SimpleBookmark
29.33.5.Copy book mark