How to Add Table of Contents for a Merged PDF Document?

Hi,

We are converting multiple excel and word templates ( these templates reside in SharePoint and have a property called ‘Section Number’) into PDF and then merging all the converted PDF’s into one single PDF document. Is it possible to add the table of contents for this single PDF document based on their section number?

Appreciate your help. Thank you in advance.

Regards
SLS

@s2205,

I will move your issues to the Aspose.PDF Cloud Forum.

@s2205

I am afraid currently Aspose.PDF Cloud API does not support adding TOC to a PDF document. We have logged a feature request PDFCLOUD-2552 for TOC support. We will notify you as soon as it is resolved.

@tilal.ahmad Thank you for your response. Can we expect this feature in the near future?

@s2205

I am afraid we can not share any ETA at the moment as we have recently logged the feature request. However, we will keep you updated about the issue resolution progress within this forum thread.

Thank you @tilal.ahmad. Is it possible to create bookmarks then for a PDF document using Aspose.PDF-Cloud API?

Little background again:
We are converting multiple excel and word templates ( these templates reside in SharePoint and have a property called ‘Section Number’ ranging from 01 to 15) into PDF and then merging all these converted PDF’s into one single PDF document. So the requirement is to be able to add bookmarks to this final PDF document with section 01, section 02 and so on until section 15.

Please advise.

@s2205

Yes, you can add a bookmark in a PDF document using PostBookmark API method of Aspose.PDF Cloud API. Please check sample C# code from the GitHub repository.

Thank you @tilal.ahmad. I am lost on the implementation and need your help please -

Here’s my requirement -
There are 15 excel and word documents in total that have a section id in SharePoint. Ex: Doc1 is having a property ‘section id’ = 1, Doc2 has ‘section id’ = 2 etc. We are converting these excel and word documents to PDF first and then merging into a single PDF. If the extension type is already .pdf, we are copying that file into the blob storage so that it is also included in the final merge. Now, after this merged document is generated, how can I accomplish the task of bookmarking the pages based on their section id’s? ( which means ex:when ‘Section 1’ is clicked on the left side, it should take the user to the doc1). Attaching an image of the requirement -

image.png (3.7 KB)

Note: Prior to the conversion of each of these templates to PDF, their section Id is known

Thank you for all your time.

@s2205

Please share your current sample output PDF document and expected PDF document. We will look into these and will guide you accordingly.

Hi @tilal.ahmad, Thank you for your response. Attaching a document that describes in detail about current pdf and expected pdf. Please let me know if you need any additional information.Requirement Info.docx (135.0 KB)

@s2205

Thanks for sharing the additional information. As you already know the section numbers of the documents. You may count the PDF document pages using Aspose.PDF Cloud API, store section number and page count of PDF documents in a collection and add bookmarks to the merged PDF document. Please find a sample code to add bookmarks based on the collection. You may improve and amend it as per your requirement. Hopefully, it will help you to accomplish the task.

PdfApi pdfApi = new PdfApi(MyAppKey, MyAppSid);
IDictionary<string, int> sectionPages = new Dictionary<string, int>();
sectionPages.Add("Section1",2);// add section number and pages of a PDF document
sectionPages.Add("Section2",4);
            
const string name = "MergedPDF.pdf";
int pagenumber = 0;
foreach (KeyValuePair<string,int> kv in sectionPages)
{
    pagenumber = ++pagenumber;
    string bookmarkPath = "";
    var bookmark = new Aspose.Pdf.Cloud.Sdk.Model.Bookmark();
    bookmark.Action = "GoTo";
    bookmark.Bold = true;
    bookmark.Italic = false;
    bookmark.Title = kv.Key;
    bookmark.PageDisplay = kv.Key;
    bookmark.PageDisplayBottom = 10;
    bookmark.PageDisplayLeft = 10;
    bookmark.PageDisplayRight = 10;
    bookmark.PageDisplayTop = 10;
    bookmark.PageDisplayZoom = 2;
    bookmark.PageNumber = pagenumber;
    bookmark.Color = new Aspose.Pdf.Cloud.Sdk.Model.Color(255, 255, 0, 0);
    var bookmarks = new List<Aspose.Pdf.Cloud.Sdk.Model.Bookmark>();
    bookmarks.Add(bookmark);

    var response = pdfApi.PostBookmark(name, bookmarkPath, bookmarks);
    pagenumber = kv.Value;
}

Thank you @tilal.ahmad.I think I am not clear on counting the page numbers part. These documents and sections are not static i.e. cannot say that section 01 starts at page 2 and section 03 starts at page 5 etc. Please advise.

@s2205

I am sorry for the confusion. It seems I did not get your requirements. Please share some sample input PDF documents with us, It will help us to understand your requirements exactly and guide you accordingly.

Hi @tilal.ahmad. I updated the document with some additional details. Please check. Thank you for all your help. Requirement Info.docx (136.1 KB)

@s2205

As already requested please share your sample input PDF documents. If you have any issue to share these documents publicly then you may share these in a private message.

Hi @tilal.ahmad,

Here’s to a more clear explanation of what I am looking for specifically -

My main question now is - Is it possible to add nested/child bookmarks using Aspose.PDF cloud API given that we know the section ID and the number of pages for each document? Ex: If there are 3 documents - testdoc 1,testdoc 2, testdoc 3 whose section id’s are 1, 1, 2 respectively and the page count of each document is 3, 2 and 4.

I want the bookmarks to be generated as shown in the attached picture -
image.png (4.1 KB)

When the Sections are expanded, it should display document names as shown in the screenshot below

capture.pdf (33.5 KB)

So, the user should be taken to the first page of testdoc 1 when clicked on both section 1 and the testdoc 1 bookmarks. The user should taken to the first page of testdoc 2 when testdoc 2 is clicked ( which means that the bookmark should be added at page 4 since testdoc1 has 3 pages)
Similarly, the user should be taken to the first page of testdoc3 when clicked on either section 2 or testdoc 3 ( which means that the bookmark should be added at page 6)
.
I need help in implementing this concept. Please let me know if you have any additional questions. Sincerely appreciate all your help.

@s2205

Thanks for sharing more details. Yes, you can add nested/child bookmarks to root bookmarks. Please pay attention to bookmarkPath property. If you leave this property empty then PostBookmark API method adds a root bookmark. Say you have added two root bookmarks Section 1 and Section 2 in a sequence then to add child bookmarks to Section 1 you need to set bookmarkPath property to 1 and for the child of Section 2 you need to set bookmarkPath property to 2. Hopefully, it will help you accomplish the task.

string bookmarkPath = "";

Thank you for your response @tilal.ahmad. I tried it but not able to implement the filenames under each section perfectly i.e. If I mention bookmarkPath = 1, then all the filenames are showing under ‘Section 1’ bookmark where in I need the documents to be displayed corresponding to their section ID. Can you please provide sample code snippet for the same. Appreciate your help very much.

@s2205

As stated above, if you want to add some child bookmark to Section 2 root bookmark then you need to set bookmarkPath = "2";. Mean you need to set bookmarkPath value as per the respective sequence of root bookmarks in the PDF document.

string bookmarkPath = "2";
var bookmark = new Aspose.Pdf.Cloud.Sdk.Model.Bookmark();
bookmark.Action = "GoTo";
bookmark.Bold = true;
bookmark.Italic = false;
bookmark.Title = "Docx2.pdf";
bookmark.PageDisplay = "Docx2.pdf";
bookmark.PageDisplayBottom = 10;
bookmark.PageDisplayLeft = 10;
bookmark.PageDisplayRight = 10;
bookmark.PageDisplayTop = 10;
bookmark.PageDisplayZoom = 2;
bookmark.PageNumber = 6;
bookmark.Color = new Aspose.Pdf.Cloud.Sdk.Model.Color(255, 255, 0, 0);
var bookmarks = new List<Aspose.Pdf.Cloud.Sdk.Model.Bookmark>();
bookmarks.Add(bookmark);

var response = pdfApi.PostBookmark(name, bookmarkPath, bookmarks);

Thank you @tilal.ahmad for the sample code. I have a question about section 01 mentioned in my previous question. This section 01 needs to show 2 child bookmarks (testdoc1 and testdoc2), how can this be achieved because it needs to know what documents come under each section and then add child bookmarks under the root along with their page numbers. This is where I am lost and cannot figure out how as each section can have multiple child bookmarks. Kindly help.