Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding bookmarks for lists and list items #573

Closed
manugbhat opened this issue Jul 11, 2021 · 1 comment · Fixed by #577
Closed

Adding bookmarks for lists and list items #573

manugbhat opened this issue Jul 11, 2021 · 1 comment · Fixed by #577

Comments

@manugbhat
Copy link

manugbhat commented Jul 11, 2021

Unable to add bookmarks at list and list item level.

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 the consuming class,

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

I am using Lists because chapters and sections are always rendered with numbered bullers. I dont need numbered bullets.
The problem here is that unless I add the list to the document, the position does not move and hence the adding the dummy paragraph is adding outlines at the same destination. In the generated pdf, on clicking the bookmarks it does not navigate to the corresponding the list item.

@mluppi
Copy link
Contributor

mluppi commented Jul 17, 2021

@manugbhat
I have answered your question on StackOverflow: https://stackoverflow.com/a/68419054/5024726
Additionally, I'm adding a new example to the existing collection in PR #577

A short sample (see the example above or the answer on StackOverflow for details):

Document document = new Document(PageSize.A4);
PdfWriter instance = PdfWriter.getInstance(document, new FileOutputStream("out.pdf"));
instance.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();

List list = new List();
list.add(new ListItem(new Chunk("ABC").setLocalDestination("dest1")));
list.add(new ListItem(new Chunk("XYZ").setLocalDestination("dest2")));
document.add(list);

// add outline items
PdfOutline root = instance.getDirectContent().getRootOutline();
new PdfOutline(root, PdfAction.gotoLocalPage("dest1", false), "abc-item");
new PdfOutline(root, PdfAction.gotoLocalPage("dest2", false), "xyz-item");

document.close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants