PDF table of contents with a template

Sorry for the lame question. Looking to convert some old code from QuickPDF to Aspose.PDF.

I’ve seen the example(s) on how to generate a TOC on a blank page. Is there a way to use an existing page as a template?
For example - the template page might have the company letterhead, logo etc. There might be different templates of course, but for the sake of simplicity, let’s assume that the TOC should be written below certain element/position on that template page.

@Grandpa

You can surely use an existing page to add TOC. Can you please share the sample PDF with us which template you want to use for adding TOC?

The final PDFs are dynamically generated by concatenating other pdf files and optionally include a cover page and a TOC page. The TOC page template would have fields or two that will get populated, then based on some logic an image is drawn (or not) and then the actual TOC is written.
The shared sample has the source files and the result, currently produced by using QuickPDF.
4Aspose.zip (762.4 KB)

@Grandpa

Thanks for providing sample files. The TOC in your file does not seem to be similar to the TOC that can be generated using Aspose.PDF. Except using the TOC functionality, you can simply obtain similar layout using tables in Aspose.PDF. Please allow us to try to generate similar output using Aspose.PDF and we will get back to you soon with our feedback.

1 Like

Any progress?

@Grandpa

Since your expected TOC was in table format, we tried a workaround using Aspose.Pdf.Table:

// this is not producing correct results because its rotated on90
//Page tocPage = new Document(dataDir + "TOCPage.pdf").Pages[1];
Page tocPage = new Document().Pages.Add();
Page coverPage = new Document(dataDir + "CoverPage.pdf").Pages[1];
Document unitDirectoryDoc = new Document(dataDir + "Unit Directory.pdf");
Document residentDirectoryDoc = new Document(dataDir + "Resident Directory.pdf");
Document expenseAnalysisDoc = new Document(dataDir + "Expense Analysis.pdf");
Document finalDocument = new Document();

finalDocument.Pages.Add(coverPage);
Page newTOCPage = finalDocument.Pages.Add(tocPage);
finalDocument.Pages.Add(unitDirectoryDoc.Pages);
Page unitDirectoryFirstPage = finalDocument.Pages[3];
finalDocument.Pages.Add(residentDirectoryDoc.Pages);
Page residentDirectoryFirstPage = finalDocument.Pages[unitDirectoryDoc.Pages.Count + 3];
finalDocument.Pages.Add(expenseAnalysisDoc.Pages);
Page expenseAnalysisFirstPage = finalDocument.Pages[unitDirectoryDoc.Pages.Count + expenseAnalysisDoc.Pages.Count + 3];

var unitDirLink = new TextFragment(unitDirectoryFirstPage.Number.ToString());
unitDirLink.Hyperlink = new LocalHyperlink(findTextFragment(unitDirectoryFirstPage, "Unit Directory"));

Table tocTable = new Table()
{
    ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,
    ColumnWidths = "90 10"
};

Row tocRow = tocTable.Rows.Add();
tocRow.Cells.Add("Unit Directory");
tocRow.Cells.Add().Paragraphs.Add(unitDirLink);

newTOCPage.Paragraphs.Add(tocTable);

finalDocument.Save(dataDir + "finalDocument.pdf");
private static TextFragment findTextFragment(Page page, string text)
{
    TextFragmentAbsorber absorber = new TextFragmentAbsorber(text);
    page.Accept(absorber);
    return absorber.TextFragments[1];
}

It does produce a table on the desired page (existing or new) with the links to the respective pages. You can please check and modify it as per your desired formatting e.g. font size, width, alignment, etc. In case it does not work and you cannot use any other TOC Page which is not rotated, please let us know. We will log a dedicated ticket in our issue management system for further and deep investigation against this case and share the ID with you.

Sorry for the late reply. Busy with other stuff. Back to this…

Manually generating the TOC is fine. However your example does not use the TOC template, which was the whole point of my initial inquiry (and therefore does not produce output matching the one from QuickPDF). The template might be rotated (as it is in this case) or not. I am not at liberty to change what I am given. Basically, if I’m to switch to Aspose, I cannot break existing functionality.

@Grandpa

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56793

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.