Free Support Forum - aspose.com

How to create Table of contents in the merged pdf?

HI ,

I have 10 pdf document.After merging the document I want to create a table of contents in the merged document with individual file name.When I click the name in the table of contents it should go to corresponding page number.Could you please help me how to do that??

Hi Anand,


Thanks for contacting support.

I am afraid currently Aspose.Pdf.Kit for Java does not support the feature to automatically create Table Of Contents in existing PDF document. However in order to fulfill this requirement, you need to follow the steps specified below.

  1. Insert a blank page at the beginning of first PDF document.
  2. Once the blank page is added, get the page count of PDF file, and save the value in some variable i.e. Page_Count.
  3. Create a loop which will subsequently add other PDF files.
  4. Concatenate a new PDF file.
  5. Create a local hyperlink on first page using createLocalLink(…) method of PdfContentEditor class and point it to start of PDF file newly added. You can get the location where newly added page starts using Page_Count+1 variable.
  6. In order to include the file name behind the hyperlink, you may try using createFreeText(…) method of PdfContentEditor class.
  7. Once the hyperlink is added, repeat steps from # 4.
Please take a look over the following code snippet which shows the steps to create local hyperlink and create free text inside PDF file.

[Java]

// create Pdf Content Editor object<o:p></o:p>

com.aspose.pdf.kit.PdfContentEditor editor = new com.aspose.pdf.kit.PdfContentEditor();

// create rectangle object which will indicate the location where link needs to be added

Rectangle rect = new Rectangle(130, 130, 150, 150);

// path of output file

String outputFile = "output_Linked.pdf";

// bind the souce PDF file

editor.bindPdf("input.pdf");

try

{

// create a local link pointing to first page of PDF file

editor.createLocalLink(outputFile, rect, 1, 2);

// Create a free text which shows the link to page 1 of File.

// Origin are similar to create local link

editor.createFreeText(rect, "Link to Page 2", 2);

// save updated PDF file

editor.save(outputFile);

}

catch (Exception e)

{

e.printStackTrace();

}



In the event of any further query, please feel free to contact. We are sorry for this inconvenience.

Hi,

Is it possible to create a link based on the title name?The rectangular area and the title name is not fitting.Also I can insert the an empty PDF before merging.But I have more table of contents in that case it is go beyond the the empty PDF File that i have created before.I dont know upto how much page the table of contents will go.Also instead of displaying as a link in the title of table of contents it is displayed like a rectangular area link.The look itself not good.

Also I want to add a tiltle and just want to intend some of the titles.

Could you please help to correct the above things?

Hi Anand,


Thanks for sharing the details.

In order to create a link in PDF file, we need to specify
the location where link needs to be displayed. We use Rectangle object to
specify the location where the link needs to be displayed. However as a workaround,
you may consider specifying the border color for Rectangle object as white or
equal to current document location and the Rectangle will not be visible in
updated PDF file.

<o:p></o:p>

Concerning to your query on adding blank page in concatenated PDF file to accommodate TOC hyperlinks, you can programmatically get an idea about the number of links that can be placed over a page and in case the number of links are in large number which cannot be accommodated over single page, multiple pages can be placed/inserted at the beginning of document. We are sorry for your inconvenience.


Please take a look over following code snippet in which I have placed a Rectangle with white border color.


[Java]

// create Pdf Content Editor object

com.aspose.pdf.kit.PdfContentEditor editor = new com.aspose.pdf.kit.PdfContentEditor();

// create rectangle object which will indicate the location where link needs to be added

Rectangle rect = new Rectangle(130, 130, 150, 150);

// specify action for linnk.

String[] actionName = { MenuActionName.DOCUMENT_ATTACHFILE, MenuActionName.DOCUMENT_EXTRACTPAGES };

// set color object as white

Color clr = new Color(255, 255, 255);

// path of output file

String outputFile = "d:/output_Linked.pdf";

// bind the souce PDF file

editor.bindPdf("d:/Input.pdf");

try

{

// create a local link pointing to first page of PDF file

editor.createLocalLink(rect, 2, 1, clr, actionName);

// Create a free text which shows the link to page 1 of File.

// Origin are similar to create local link

editor.createFreeText(rect, "Link to Page 2", 2);

// save updated PDF file

editor.save(outputFile);

}

catch (Exception e)

{

e.printStackTrace();

}

I have specified border color of rectangle as white.But the border is still visible when I zoom the PDF to 100%.Please help to correct this.

Hi Anand,


Can you please share the sample output document. We are sorry for this inconvenience.

Document attached.

Hi Anand,


Thanks
for sharing the sample document.

I
have also tested the scenario and I am able to reproduce the same problem. For the
sake of correction, I have logged it in our issue tracking system as
PDFKITJAVA-33236. We
will investigate this issue in details and will keep you updated on the status
of a correction.

We
apologize for your inconvenience.