DEV Community

IDRSolutions
IDRSolutions

Posted on

How to add a table of contents to a PDF in Java (Tutorial)

To add a table of contents to a PDF file in Java, you can use our PDF toolkit JPedal.

Once you have downloaded the JPedal jar, you can use the following lines of code to generate a table of contents based on the bookmarks that already exist in the file. See our other blog post on how to add bookmarks to a PDF file.

final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.addTableOfContents(1, PaperSize.A4_PORTRAIT, BaseFont.Helvetica, 14, new float[] {0, 0, 0});
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));
pdf.closeDocument();

Enter fullscreen mode Exit fullscreen mode

You can customise the page dimensions, font, font size, and font colour. Learn more on our support site.

Resources

JPedal contains a powerful PDF Manipulator which you can use to perform bulk edits to your documents.

We can help you better understand the PDF format as developers who have been working with the format for more than 2 decades!

Top comments (0)