Combine PDFs and add Table of Contents

Hi,


I’m wondering if anyone has come across this before. I want to combine some PDFs together and create a customized table of contents. I have looked through all the examples I could find.

The related examples I have found:
Manipulate PDF Document in C#|Aspose.PDF for .NET
Concatenate PDF documents in C#|Aspose.PDF for .NET

The first one uses Aspose.PDF, the second Aspose.PDF.Fascades
My problem with these is that they limit the formatting of the Table of Contents (or I just do not know how to use them)
In the Aspose.PDF.Generator I found everything I need except support to combine PDFs

The main functionality I need is:
Formatted Table of Contents
Combined PDFs
Links to the Combined documents

What I have working now is a variant of the first link, it has horrible formatting and I have to determine when to insert a new page. [See attached screenshot: TOC.png]

I would prefer to format it more like:
1. Section
1.1 Description
1.2 Description
Which might take up lots of space
2. Section
2.1 Description

Where the subsections (Description) themselves have links to the appended documents.

Any help would be greatly appreciated!

Hi,


Thanks for contacting support.

In order to accomplish your requirements, I would recommend you to please try using the approach specified over Add TOC in existing PDF file. However you need to perform PDF concatenation and TOC generation side by side.

Following are the steps which you can follow to accomplish your requirements.
  1. Load first document, get its page count, save the information in some variable (preferably in integer array).
  2. Then add a blank page at the beginning of first document and then again get the total page count of document. This will help in identifying the location from which next document will start (n +1 where n is page count of initial loaded document).
  3. Concatenate another file and get the total page count. Save all values in array so that you have reference of locations where all new documents are concatenated/merged.
  4. Once all the files are concatenated, you can create the TOC over first page of the final merged document.
  5. Use the values from integer array to set the destination for TOC items.
You may consider visiting the following link for further information on Concatenate PDF Files. In the event of any further query, please feel free to contact.

Thanks for the reply, but I believe you missed my questions. How would I go about formatting the TOC as specified in my post?


I have done the concatenation and TOC creation.

Hi,

You can set the formatting of TOC items by using Heading object. Please take a look at the following code lines. In case I have still not understood your requirement or you have any further query, please feel free to contact.

[C#]

// Create Heading object
Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
heading2.TextState.FontStyle = FontStyles.Bold;
heading2.TextState.ForegroundColor = Aspose.Pdf.Color.Red;

Hello,


I’m not sure if I am explaining myself correctly. I know how to format the font and color.
I would prefer to format it more like:
1. Section
1.1 Description
1.2 Description
Which might take up lots of space
2. Section
2.1 Description

Where the subsections (Description) themselves have links to the appended documents.

In the solution you provided, an error will be thrown if the TOC is longer than one page.
Currently I handle this problem by imposing a hard limit of items per TOC page. I would prefer this to happen dynamically as some descriptions may need several lines.
Is it possible to format how I have above or do I need to find another PDF library?
Hi,

As per my understanding, you need to have different indentation (left margin) for TOC elements (i.e. Description etc) and you also need to have TOC items with length more than one line. Can you please share some sample application which can help us in replicating the above stated issue. We are sorry for your inconvenience.

PS, Can you please share some PDF or image which can help us in understanding the exact requirement (layout of TOC) which you need to generate.

That is correct, I would also like to not worry about if the information will spill onto another page (because then I have to create a new page or the application fails on save) but that is a more minor concern for now.


Example PDF
Note: I do not own this PDF nor do I have any affiliation with Mission, I just found this looking for examples.

Hi Aaron,


Thanks for sharing additional information. We are looking into the issue and will get back to you soon.

Best Regards,

I found a way that I can generate the TOC with links and have formatting.


Instead of using the TOC objects that are built in, I am generating all the formatting normally.
Note: before saving, the TextFragment.Rectangle is useless

After saving, I go back through the TOC pages and use a TextFragmentAbsorber to find the items name and insert a link.

This still has a problem I need to work out but it is different topics and should probably be a different thread.
If there is overflow onto another page it is ignored. If the TOC has more content than will fit on one page it will just overflow.

Also, this implementation means saving the document twice but I do not mind that.

Hi Aaron,


Sorry for the delayed response.

We are glad to hear that you have managed to accomplish your requirements. Please continue using our API’s and in the event of any further query, please feel free to contact.