Free Support Forum - aspose.com

Adding custom columns to table of contents

I am currently working on adding a table of contents to a concatenated PDF. We iterate over the files and append them into the Document and that works as expected.

Now when it comes to adding a table of contents via the TocInfo + Heading classes that also works as expected. The problem is we would like to add additional columns to the table between the title and the page number if possible.

Is this feasible given the current API? If not what would be the best approach. Ideally I would hold a reference to each item in the table and update it with the destination page once built but I can’t see a way to do that with additional columns. An alternative would be to create multiple Headings for each row BUT all the formatting information is on the TocInfo instance and so I can’t have multiple headings attached to the same table with different formatting.

@dcoxall

Thanks for contacting support.

We will surely investigate the feasibility of your requirements. However, could you please provide us a sample source PDF document, sample code snippet that you are already using to add formatted TOC and an expected output PDF. We will test the scenario in our environment and address it accordingly.

That’s just it… I haven’t yet built anything that achieves what I want… My best option currently is to build something similar to the Heading class. Storing references to the TextSegment or TextFragment the represents each cell. When I then encounter the correct destination page I can then update the page number by calling setText and add hyperlinks to each of the cells contents.

It just a bit clunky when Heading and TocInfo gets me 70% of the way there but seems to be missing the last bit that would make this cleaner. With aspose being closed source too then I don’t see an easy way of building my own class that extends BaseParagraph that could encapsulate all of the logic.

@dcoxall

Thanks for sharing more details.

A standard TOC contains headings and page numbers parallel to respective headings (as you also mentioned). However, your certain requirements to achieve a 3 Columns TOC need some investigation that whether it is possible via Heading and TocInfo or not.

As an alternative approach, Aspose.PDF also offers a feature to add local links where you can link a text fragment with another text fragment or page. Now, these text fragments can be added to a table, formatted in a way to look like a TOC. You can try this approach and let us know in case it does not fulfill your demands completely. We will further proceed to assist you accordingly.

So I have been able to achieve this. As stated @asad.ali whilst iterating over the PDF pages I store an object that holds references to each rows page number TextFragment as well as separate references to each of the TextFragments that are present in all of the cells for a particular row.

When I locate a page that is listed in the table of contents, I update the pagenumber text fragment using setText and I loop over each of the stored text fragments for the other cells and call setHyperlink.

This is working. I think it would be fantastic if this could be encapsulated into the existing TocInfo and Heading but I understand its likely an edge case for a table of contents (my particular use case is that the table represents a document used during a meeting and so each section has a start date and speaker)

@dcoxall

It is nice to hear that you were able to achieve your requirement with suggested approach. Yes, you are right - the TocInfo and Headings particularly represent the standard TOC in the PDF documents. For some special and custom cases, you can use some other workaround or alternative approach offered by the API to achieve your requirements. Please feel free to let us know in case you need further assistance.