By following the instructions in this article, add table of contents to PDF using Java. Get all the details to set the development environment, a program logic in steps, and a sample code to add clickable table of contents to PDF using Java. You will learn a variety of customizations applicable to the table of contents in a PDF file.
Steps to Create Table of Content in PDF using Java
- Set the IDE to use Aspose.PDF for Java to add a table of contents at the start
- Load the PDF file into the Document object, get the total number of pages and insert a new page at the start of the PDF
- Use TocInfo and TextFragment to define the properties of the table of contents
- Generate the list of strings having headings text for the TOC
- Iterate through the pages of the PDF and add one entry in TOC for each page in the PDF file
- Link the respective page with the entry in TOC and set the destination coordinate on the linked page
- Save the output PDF file with TOC on the first page
These steps define the process to create clickable table of contents in PDF using Java. Initiate the process by loading the PDF file, getting the count of its pages, and declaring and configuring the TocInfo object to set the properties of the table of contents. Parse through all the pages in the PDF file, add an entry in TOC with a hyperlink for each page, set link text and define the page coordinates where control moves when we click the entry in the table of contents.
Code to Add Table of Contents in PDF using Java
import com.aspose.pdf.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Create TOC in PDF in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Load the pdf document | |
Document inputDoc = new Document("Document1.pdf"); | |
// Get count of pages in the PDF | |
int count = inputDoc.getPages().size(); | |
// Insert a page for table of contents | |
Page pageTOC = inputDoc.getPages().insert(1); | |
// Instantiate an object of TocInfo for TOC information | |
TocInfo tocInfo = new TocInfo(); | |
// Create an object of TextFragment for setting TOC title | |
TextFragment title = new TextFragment("Table Of Contents"); | |
title.getTextState().setFontSize(20); | |
// Set the title for Table of contents | |
tocInfo.setTitle(title); | |
pageTOC.setTocInfo(tocInfo); | |
// Create a list of strings for TOC | |
List<String> tocTitles = new ArrayList<String>(); | |
for(int j = 1; j <= count; j++) | |
tocTitles.add("Page " + (j + 1)); | |
int i = 0; | |
while (i < count) | |
{ | |
// Instantiate an object of the Heading class | |
Heading heading = new Heading(1); | |
TextSegment textSegment = new TextSegment(); | |
heading.setTocPage(pageTOC); | |
heading.getSegments().add(textSegment); | |
// Set the destination page for the heading object | |
heading.setDestinationPage(inputDoc.getPages().get_Item(i + 2)); | |
// Set the destination coordinates for TOC item | |
heading.setTop(inputDoc.getPages().get_Item(i + 2).getRect().getHeight()); | |
// Set TOC item text | |
textSegment.setText(tocTitles.get(i)); | |
// Add heading to the TOC page | |
pageTOC.getParagraphs().add(heading); | |
i += 1; | |
} | |
// Save PDF Document | |
inputDoc.save("TOC.pdf"); | |
System.out.println("Done"); | |
} | |
} |
This article has taught us how to add table of content in PDF using Java. To add header and footer in the PDF, refer to the article on how to add header and footer in PDF using Java.