Dear All,
Hi there,
//Load an existing PDF files<o:p></o:p>
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(myDir+"input.pdf");
//Get access to first page of PDF file
com.aspose.pdf.Page tocPage = doc.getPages().insert(1);
//Create object to represent TOC information
com.aspose.pdf.TocInfo tocInfo = new com.aspose.pdf.TocInfo();
com.aspose.pdf.TextFragment title = new com.aspose.pdf.TextFragment("Table Of Contents");
title.getTextState().setFontSize(20);
title.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
//Set the title for TOC
tocInfo.setTitle(title);
tocPage.setTocInfo(tocInfo);
//Create string objects which will be used as TOC elements
String[] titles = new String[4];
titles[0] = "First page";
titles[1] = "Second page";
titles[2] = "Third page";
titles[3] = "Fourth page";
for (int i = 0; i < 4; i++) {
// Create Heading object
com.aspose.pdf.Heading heading2 = new com.aspose.pdf.Heading(1);
com.aspose.pdf.TextSegment segment2 = new com.aspose.pdf.TextSegment();
heading2.setTocPage(tocPage);
heading2.getSegments().add(segment2);
// Specify the destination page for heading object
heading2.setDestinationPage(doc.getPages().get_Item(i + 2));
// Destination page
heading2.setTop(doc.getPages().get_Item(i + 2).getRect().getHeight());
// Destination coordinate
segment2.setText(titles[i]);
// Add heading to page containing TOC
tocPage.getParagraphs().add(heading2);
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
doc.save(output);
doc= new Document(new ByteArrayInputStream(output.toByteArray()));
//Save the updated document
doc.save(myDir+"TOC_Output_Java.pdf");
Thanks for the code and precise definition.