Table of Contents

Hi,

I have successfully implemented both bookmarks and table of contents when merging pdf's (sometimes of 200 documents). However, when the table of contents is created, if it spans more than 1 page, the numbering a hyperlinks go out of sync.

How would we stop this happening or how would we recursively go back and update correctly?

Thansk, Martin

12 Replies

SL Sowmiya Loganathan Syncfusion Team June 12, 2020 01:25 PM UTC

Hi Martin,   
 
Thank you for contacting Syncfusion support.   
 
We have analyzed your requirement “Create TOC more than one page when merging PDF documents”. We have created the sample to achieve your requirement and please find the download link from below,   
 
 
 
Please try the above sample in your end and let us know the result.   
 
Regards,  
Sowmiya Loganathan 



MA Martin replied to Sowmiya Loganathan June 13, 2020 09:05 AM UTC

Hi Martin,   
 
Thank you for contacting Syncfusion support.   
 
We have analyzed your requirement “Create TOC more than one page when merging PDF documents”. We have created the sample to achieve your requirement and please find the download link from below,   
 
 
 
Please try the above sample in your end and let us know the result.   
 
Regards,  
Sowmiya Loganathan 


Thank you for your response, however this does not address the issue. I already create the table of contents when merging PDF's. The issue is that when the table of contents spans more than 1 page, the numbering of the table of content is incorrect for all items on the first page due to the additional table of contents page being required. How do you update a table of contents to be correct after you have added all of the entries?


SL Sowmiya Loganathan Syncfusion Team June 15, 2020 01:02 PM UTC

 
Hi Martin,  
 
We have analyzed your requirement. We can able to identify the page of the additional table of contents by drawing the text using PdfTextElement and PdfLayoutResult. Please refer the below code snippet for more details,  
 
PdfDocument document = new PdfDocument(); 
 
//Add page to the document 
PdfPage page = document.Pages.Add(); 
 
PdfGraphics graphics = page.Graphics; 
 
//Create a text element with the text and font 
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
//Draw the text in the first column 
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height)); 
 
textElement = new PdfTextElement(text2, new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
result = textElement.Draw(result.Page, new RectangleF(0, result.Bounds.Bottom+10, page.GetClientSize().Width, page.GetClientSize().Height)); 
 
 
So kindly add the table of contents using PdfTextElement and add the text in resultant PDF (using PdfLayoutResult) if it exceeds the first page. If you still facing the issue, please provide us the below details, It will helpful for us to provide the precise solution.  
 
  • Complete sample or code snippet
  • Expected Output
  • Product version
 
 
Regards, 
Sowmiya Loganathan 



MA Martin June 22, 2020 03:24 PM UTC

Hi, please find attatched code snippet.

I can create a table of contents that spans multiple pages. When each line is created for the table of contents, the page number is added and also a hyperlink to that page. This works correctly for the first page of the table of contents, but when the table of contents spans a second page, the hyperlinks and page numbers are incorrect now due to the extra page added at the start. Can you loop back through the table of contents and update the hyperlinks/page numbers or would I need to calculate the quantity of pages the table of contents will span prior to creating so that i can offset the page numbering accordingly?

Thanks, Martin

Attachment: Example_2150efa3.rar


SL Sowmiya Loganathan Syncfusion Team June 23, 2020 02:22 PM UTC

Hi Martin,  
 
Thank you for the details.  
 
Currently we are working on this and will update the further details on June 24, 2020.  
 
Regards, 
Sowmiya Loganathan 



SL Sowmiya Loganathan Syncfusion Team June 24, 2020 12:37 PM UTC

Hi Martin,    
   
Thank you for your patience.    
   
We have analyzed your code snippet and found that you are trying to add a table of contents on the same page. So we need to check whether the table of contents is exceeded the page height or not. So we can achieve this by calculating the text height and get the PdfLayoutResult while drawing the text element. Please refer the below code snippet for your reference,    
   
Calculating the text size based on the font:    
SizeF size = font.MeasureString(text);   
   
Getting the layout result while drawing the text element:    
//Create a text element with the text and font    
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));    
//Draw the text in the first column    
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height));    
    
textElement = new PdfTextElement(text2, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));    
result = textElement.Draw(result.Page, new RectangleF(0, result.Bounds.Bottom+10, page.GetClientSize().Width, page.GetClientSize().Height));    
   
  
So based on the above calculation, we can able to add the TOC in pagination. If the contents exceed the first page, you can add the upcoming TOC on the resultant page of existing content. We have created the sample to illustrate this and please find the download link from below,   
   
   
Please check the above sample and let us know if you have any concerns about this.    
   
Regards, 
Sowmiya Loganathan  



MA Martin replied to Sowmiya Loganathan June 26, 2020 03:22 PM UTC

Hi Martin,    
   
Thank you for your patience.    
   
We have analyzed your code snippet and found that you are trying to add a table of contents on the same page. So we need to check whether the table of contents is exceeded the page height or not. So we can achieve this by calculating the text height and get the PdfLayoutResult while drawing the text element. Please refer the below code snippet for your reference,    
   
Calculating the text size based on the font:    
SizeF size = font.MeasureString(text);   
   
Getting the layout result while drawing the text element:    
//Create a text element with the text and font    
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));    
//Draw the text in the first column    
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height));    
    
textElement = new PdfTextElement(text2, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));    
result = textElement.Draw(result.Page, new RectangleF(0, result.Bounds.Bottom+10, page.GetClientSize().Width, page.GetClientSize().Height));    
   
  
So based on the above calculation, we can able to add the TOC in pagination. If the contents exceed the first page, you can add the upcoming TOC on the resultant page of existing content. We have created the sample to illustrate this and please find the download link from below,   
   
   
Please check the above sample and let us know if you have any concerns about this.    
   
Regards, 
Sowmiya Loganathan  


Hi, I think we have confusion. My TOC will draw on multiple pages without issue, this is successful.

When I create TOC for existing document, I know i am adding 1 page to start of document, so i can set page number correctly in TOC. When TOC goes onto 2 or 3 pages in size, this increases pages at start of document as is correct. But page number on TOC is now incorrect as TOC as added extra page.

Example: TOC is 1 page in size - First line TOC is page 2

If TOC is big and goes 2 pages in size, first entry in TOC still says page 2, but now it is really page 3 as TOC is 2 pages long not 1


SL Sowmiya Loganathan Syncfusion Team June 29, 2020 12:40 PM UTC

Hi Martin,  

We have analyzed your requirement and this can be achieved by using the event handler PageAddedEventArgs. While adding a new page to PDF document (while pagination), this event triggers. So in this event, we can increase the page count (based on the TOC) and assign to TOC content. Please refer the below code snippet for more details,  

//Add the event 
document.Pages.PageAdded += new PageAddedEventHandler(Pages_PageAdded); 
 
//Event handler for PageAdded event 
static void Pages_PageAdded(object sender, PageAddedEventArgs args) 
{ 
    pageCount++; 
} 


Please try the above solution in your end and let us know the result.  

Regards, 
Sowmiya Loganathan 
 



MA Martin replied to Sowmiya Loganathan June 29, 2020 02:39 PM UTC

Hi Martin,  

We have analyzed your requirement and this can be achieved by using the event handler PageAddedEventArgs. While adding a new page to PDF document (while pagination), this event triggers. So in this event, we can increase the page count (based on the TOC) and assign to TOC content. Please refer the below code snippet for more details,  

//Add the event 
document.Pages.PageAdded += new PageAddedEventHandler(Pages_PageAdded); 
 
//Event handler for PageAdded event 
static void Pages_PageAdded(object sender, PageAddedEventArgs args) 
{ 
    pageCount++; 
} 


Please try the above solution in your end and let us know the result.  

Regards, 
Sowmiya Loganathan 
 


Hi, thanks for the update. I can already increase the page count when the TOC moves to a new page. As the TOC is inserted at the start of the document, once the TOC goes onto page 2, all of the TOC content on the first page of TOC is incorrect by 1 page. How do i update these entries? Is it easier to work out how many entries there will be in TOC, then artificially create the numbering instead of looping back through the original TOC page and update?


SL Sowmiya Loganathan Syncfusion Team June 30, 2020 11:22 AM UTC

Hi Martin,    
   
To draw the page number of the destination page in the TOC page (if it is paginated), we need to add the TOC contents before all functionalities (adding of bookmarks, document link annotation, page number) to get the pagination details. So based on these details, we can achieve your requirement and please refer the below sample which illustrates the same,    
   
   
Please the above sample in your end and let us know the result.    
   
Regards, 
Sowmiya Loganathan 



MA Martin replied to Sowmiya Loganathan July 6, 2020 01:04 PM UTC

Hi Martin,    
   
To draw the page number of the destination page in the TOC page (if it is paginated), we need to add the TOC contents before all functionalities (adding of bookmarks, document link annotation, page number) to get the pagination details. So based on these details, we can achieve your requirement and please refer the below sample which illustrates the same,    
   
   
Please the above sample in your end and let us know the result.    
   
Regards, 
Sowmiya Loganathan 


Thanks Sowmiya,

I have managed to convert it to VB.Net and also replaced the dictionary with a list as when you moved to a third page of ToC the PointF was identical to that on second page thus causing an eror on the dictionary.

Thnaks, Martin


SL Sowmiya Loganathan Syncfusion Team July 7, 2020 11:04 AM UTC

Hi Martin,   
 
We have modified the sample without using dictionary and please find the download link from below,   
 
Please try the above sample in your end and let us know the result.   
 
Regards,  
Sowmiya Loganathan 


Loader.
Up arrow icon