TOC. The problem with Document.Net and DOCX

Get technical support of Document .Net in C# and VB.Net
Ryan
Posts: 2
Joined: Thu May 20, 2021 9:06 am
Contact:

TOC. The problem with Document.Net and DOCX

Post by Ryan »

Hi, I have an original document with a table of contents:
Contents, Section1 -1, Section2 -3, Section3 -8, Section4 -13, Section5 -18, Section6 -29.

Then in my code I do this to refresh the table of contents:

// Table of contents - ensure it is up to date
TableOfEntries toc = (TableOfEntries)dc.GetChildElements(true, ElementType.TableOfEntries).FirstOrDefault();
if (toc != null) toc.Update();

But the output has wrong PageNumbers and word no longer recognises it as a table of contents anymore:
Contents, Section1 -1, Section2 -1, Section3 -1, Section4 -1, Section5 -1, Section6 -1.

What is wrong? Thanks
Oliver
Posts: 27
Joined: Wed Aug 19, 2020 11:59 am
Contact:

Re: TOC. The problem with Document.Net and DOCX

Post by Oliver »

Hi Ryan.

You need to add the next code for updating TOC's page numbers also. The page numbers in TOC entries are updated using the Document.GetPaginator method:

Code: Select all

dc.GetPaginator(new PaginatorOptions() { UpdateFields = true });
The full code must be:

Code: Select all

TableOfEntries toc = (TableOfEntries)dc.GetChildElements(true, ElementType.TableOfEntries).FirstOrDefault();
toc.Update();
dc.GetPaginator(new PaginatorOptions() { UpdateFields = true });
Page numbers are automatically updated in that case and you will see the result as:
Contents, Section 1 -1, Section 2 -3, Section 3 -8, Section 4 -13, Section 5 -18, Section 6 -29.

More information about Document.Net and TableOfContent you may read right here: https://sautinsoft.com/products/documen ... ts-toc.php
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests