I am using OpenPDF as a replacement for iText. It meets most of my requirements. I was trying to add click and navigate bookmarks for List and ListItems which I am unable to. Here is what I have tried. I added an event handler hook when a paragraph is added:
public class OPBookmarkBuilder extends PdfPageEventHelper {
public void onParagraph(PdfWriter writer, Document document, float position) {
PdfContentByte cb = writer.getDirectContent();
PdfDestination destination = new PdfDestination(PdfDestination.FITH, position);
new PdfOutline(cb.getRootOutline(), destination, "Para1-bookmark");
}
}
And when I add a List or ListItem, I am also adding a dummy paragraph so that the bookmarks get added for that paragraph like so:
public class PDFGenerator {
public static void main(String[] args) {
Document document = new Document();
final PdfWriter instance = PdfWriter.getInstance(document, new FileOutputStream("testbm.pdf"));
OPBookmarkBuilder bookmarkBuilder = new OPBookmarkBuilder();
instance.setPageEvent(bookmarkBuilder);
instance.setPageEvent(image);
document.open();
instance.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.add(new Paragraph(....));
List section = new Section();
section.add(new ListItem("test1",...));
section.add(new Table(..));
// Add a dummy empty paragraph so that PDFOutline is added.
document.add(new Paragraph("",...));
section.add(new ListItem("test2",...));
// ...
document.add(section);
document.close();
}
}
The position does not move until some element is added to document. Here the problem is that unless I add the list to the document (before document.close()), the position has not moved and hence adding the dummy paragraph is adding outlines at the same destination. In the generated a PDF, on clicking the bookmarks it does not navigate to the corresponding list item.
I have explored Chapters and Sections but that renders a numbered bullet which I don't want.
