Skip to main content

Posts

Showing posts from November, 2019

Add Table of Contents (TOC) to PDF in Java

Adding table of contents (TOC) to our document is a useful way to show readers what chapters are included in our document. When readers click on the table of contents, they can quickly navigate to the specific parts that they’re interested. In this blog, I will introduce how to add table of contents (TOC) to a PDF document programmatically in Java using Free Spire.PDF for Java library. import com.spire.pdf.*; import com.spire.pdf.actions.PdfGoToAction; import com.spire.pdf.annotations.*; import com.spire.pdf.general.PdfDestination; import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.geom.*; public class TableOfContent {     public static void main(String[] args) throws Exception     {         //load PDF document         PdfDocument doc = new PdfDocument( "sample.pdf" );         int pageCount = doc.getPages().getCount();         //Insert a page as the first page to draw TOC         PdfPageBase tocPage = doc.getPages().insert( 0 );