Manually Creating Table of Contents

Hi


I am wondering if it is possible to manually create a table of contents in a PDF document using Aspose?

I’m currently using version 8.5 of the Aspose PDF generator, Visual Studio 2010, and .NET 4.0.

I am generating a larger document from a collection of individual chapters, which are defined in our database using our own XML format, and then transformed into the Aspose XML format, and finally bound into a PDF document using the Pdf generator object. Our requirements dictate that our users be able to download individual chapters, or the entire document as a single PDF.
The first chapter in any given document contains preliminary information, such as a title page, a foreword, terms of reference, copyright information, and (most importantly for this post) a table of contents. Since this first chapter could be downloaded on its own, I can’t use the inbuilt table of contents stuff, as the actual pages won’t exist. While I can generate the entire document, deleting the extra pages may not work (I haven’t tried it, but I suspect the TOC would be updated to remove the deleted pages).

What I think I need (and I’m open to alternate suggestions, if anyone has any) is a method of extracting the text and page number of headings throughout the document, and manually updating my “table of contents” page.

The only part I can’t figure out is the extraction of text and page numbers.

Can anyone provide any assistance?

Thanks
Chris Fairall.

It’s interesting. I did a little experimentation, and it seems that my suggestion of creating the entire document and deleting the pages I don’t want wasn’t so far-fetched after all. It seems you can do just that.


If anyone has any better ideas, I’d be open to hearing them.

Chris

Hi Chris,


Thanks for the feedback. Its good to know that you have managed to accomplish your requirement. It seems it’s the best solution for the scenario and would be good reference for others having similar requirement.

Best Regards,

I have discovered an interesting problem (bug?) with this approach.


If the pages of the document that I wish to retain are not at the start of the full document, there can be a problem if you delete the pages in the wrong order. If I delete the pages at the start of the document that precede the pages I wish to retain, and then delete the pages after the pages I wish to retain, I am left with a blank page. In this particular case, I only wish to retain one page:

				int pagesBefore = GetPdfPagesBefore();
			<span style="color:#2b91af;">PdfDocument</span> fullDoc = GetFullDocument();
			<span style="color:blue;">while</span> (pagesBefore > <span style="color:magenta;">0</span>)
			{
				fullDoc.Pages.Delete(<span style="color:magenta;">1</span>);
				pagesBefore--;
			}
			<span style="color:blue;">while</span> (fullDoc.Pages.Count > doc.Pages.Count)
			{
				fullDoc.Pages.Delete(fullDoc.Pages.Count);
			}


However, if I do it the other way - delete the pages at the end of the document first, and then the pages at the start - I get the one-page document I expect:

				int pagesBefore = GetPdfPagesBefore();
			<span style="color:#2b91af;">PdfDocument</span> fullDoc = GetFullDocument();
			<span style="color:blue;">while</span> (fullDoc.Pages.Count > doc.Pages.Count + pagesBefore)
			{
				fullDoc.Pages.Delete(fullDoc.Pages.Count);
			}
			<span style="color:blue;">while</span> (pagesBefore > <span style="color:magenta;">0</span>)
			{
				fullDoc.Pages.Delete(<span style="color:magenta;">1</span>);
				pagesBefore--;
			}


Am I doing something wrong here? I’ve looked over my code, and can’t find a problem with it. Perhaps you can suggest why it works one way and not the other? Is this a bug in the Aspose PDF generator?

Chris

Hi Chris,


Thanks for your feedback. Can you please share your sample documents here? So we will test the scenario at our end and will provide you more information accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Hi Tilal

I’m happy to provide some documents, but I’m not really sure what I can provide. It essentially requires building a multi-page, multi-section document using the XML method, and then deleting a bunch of pages from the start and end of the document, leaving only one page.
I have attached the sample XML that I used to generate the PDF. Sorry about the size.
The steps to recreate the problem involve:
  1. Create a PDF in memory using the XML
  2. Using the code in my previous post, remove the first two pages, and all the pages from the end, except one. This should be the table of contents. Note that the “doc” variable is a document containing just the section to be preserved. You should replace “doc.Pages.Count” with “1”.
  3. Save
The result should be:
  • when deleting from the start of the document first, a single blank page (wrong)
  • when deleting from the end, leaving three pages, and then deleting the first two pages, single page with table of contents (correct)
I hope this is helpful.
Chris


Hi Chris,


Thank you for providing source document and additional information. We are looking into the issue and will get back to you ASAP.

Best Regards,

Hi Chris

We are sorry for the inconvenience caused. We have tested the scenario at our end and managed to reproduce the page deletion order issue. We have logged the issue in our bug tracking system as PDFNEWNET-36092 for further investigation and resolution. We will notify you via this thread as soon as it is resolved.

Please feel free to contact us for any further assistance.

Best Regards,

The issues you have found earlier (filed as PDFNEWNET-36092) have been fixed in Aspose.Pdf for .NET 8.9.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
(4)