I’m running into multiple frustrating issues when trying to merge a document and add in a ToC dynamically. Some of these issues seem to have been previously reported several years ago, which concerns me that they are still unresolved.
Issue 1: Bookmark to Table of Contents isn’t working when it spans more than 1 page. (Reported in 2015 PDFNEWNET-40033).
Issue 2: The ToC title shows up multiple times per page. This happens if I call document.ProcessParagraphs() manually, which I have to do in order to make the page numbering work correctly when a ToC is added (See issue #3).
Issue 3: ToC pages are not counted when stamping pages with a page number unless you call document.ProcessParagraphs() manually, which causes issue #2.
Perhaps I’m missing something, but I’ve struggled to find good examples. Most I find fragmented and lacking.
Additionally, the code documentation is often times confusing. Example: TocInfo.IsCountTocPages property says “Gets or sets is count or passed toc pages”. What??
I typically don’t complain, but I’m not going to beat around the bush here. I’m a bit surprised at the number of bugs plaguing this library, especially with the quantity I’ve run into regarding this single feature (Table of Contents). Looking at your bug fix log from previous releases, it is clear that you have a quality control issue. Please fix this, or I’ll be forced to move on to another product. Frankly, I’d advise any new users to carefully consider other options.
Example code:
var lic = new Aspose.Pdf.License();
lic.SetLicense(new FileStream(@“Aspose.Pdf.lic”, FileMode.Open));
using (var mergedDocument = new Document())
{
var tocBookmark = new OutlineItemCollection(mergedDocument.Outlines);
tocBookmark.Title = "Table of Contents";
mergedDocument.Outlines.Add(tocBookmark);
mergedDocument.ProcessParagraphs();
var tocInfo = new TocInfo();
tocInfo.LineDash = TabLeaderType.None;
var title = new TextFragment("Table Of Contents");
title.TextState.FontSize = 12;
title.TextState.FontStyle = FontStyles.Bold;
tocInfo.Title = title;
tocInfo.FormatArrayLength = 2;
tocInfo.FormatArray[0].Margin.Left = 0;
tocInfo.FormatArray[0].Margin.Right = 0;
tocInfo.FormatArray[0].Margin.Top = 5;
tocInfo.FormatArray[0].Margin.Bottom = 5;
tocInfo.FormatArray[0].LineDash = TabLeaderType.None;
tocInfo.FormatArray[1].Margin.Left = 10;
tocInfo.FormatArray[1].Margin.Right = 0;
tocInfo.FormatArray[1].Margin.Top = 5;
tocInfo.FormatArray[1].Margin.Bottom = 5;
tocInfo.FormatArray[1].LineDash = TabLeaderType.None;
tocInfo.FormatArray[1].TextState.FontSize = 10;
var tocPage = mergedDocument.Pages.Insert(1);
tocPage.TocInfo = tocInfo;
for (var i = 0; i < 200; i++)
{
var newPage = mergedDocument.Pages.Add();
var pageBookmark = new OutlineItemCollection(mergedDocument.Outlines);
pageBookmark.Title = "Page " + (i + 1);
pageBookmark.Action = new GoToAction(newPage);
mergedDocument.Outlines.Add(pageBookmark);
var heading = new Heading(1);
var segment = new TextSegment();
heading.DestinationPage = newPage;
heading.Segments.Add(segment);
heading.IsAutoSequence = false;
segment.Text = "Page " + (i + 1);
heading.TextState.Font = FontRepository.FindFont("Arial Unicode MS");
heading.IsInList = true;
tocPage.Paragraphs.Add(heading);
heading.TocPage=tocPage;
newPage.Paragraphs.Add(heading);
}
tocBookmark.Action = new GoToAction(tocPage);
mergedDocument.ProcessParagraphs();
var pageNumberStamp = new PageNumberStamp()
{
Background = false,
Format = "Page # of " + mergedDocument.Pages.Count,
BottomMargin = 10,
HorizontalAlignment = HorizontalAlignment.Center,
StartingNumber = 1
};
pageNumberStamp.TextState.Font = FontRepository.FindFont("Calibri");
pageNumberStamp.TextState.FontSize = 8.0F;
pageNumberStamp.TextState.FontStyle = FontStyles.Italic;
foreach (var page in mergedDocument.Pages.Cast<Page>())
{
page.AddStamp(pageNumberStamp);
}
mergedDocument.Save("mergedresult.pdf");
}