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.
- Insert a blank page at the beginning of first PDF document.
- Once the blank page is added, get the page count of PDF file, and save the value in some variable i.e.
Page_Count
.
- Create a loop which will subsequently add other PDF files.
- Concatenate a new PDF file.
- 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.
- In order to include the filename behind the hyperlink, you may try using
createFreeText(...) method
of PdfContentEditor
class.
- 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
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 source 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 a PDF file, we need to specify the location where the link needs to be displayed. We use a 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 the Rectangle object as white or equal to the current document location, and the Rectangle will not be visible in the updated PDF file.
Regarding your query on adding a blank page in a concatenated PDF file to accommodate TOC hyperlinks, you can programmatically get an idea about the number of links that can be placed on a page. If the number of links is large and cannot be accommodated on a single page, multiple pages can be placed/inserted at the beginning of the document. We are sorry for any inconvenience.
Please take a look at the following code snippet, in which I have placed a Rectangle with a 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 the link needs to be added
Rectangle rect = new Rectangle(130, 130, 150, 150);
// specify action for the link.
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 source PDF file
editor.bindPdf("d:/Input.pdf");
try {
// create a local link pointing to the first page of the PDF file
editor.createLocalLink(rect, 2, 1, clr, actionName);
// Create a free text which shows the link to page 1 of the file.
// Origin is 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.
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.