Before in the Document the TOC is like this:
1 Ich bin eine Überschrift 1 1
1.1 Ich bin eine Überschrift 2 1
1.1.1 Ich bin eine Überschrift 3 1
1.2 Ich bin eine Überschrift 2 2
1.2.1 Ich bin eine Überschrift 3 2
2 Ich bin eine Überschrift 1 2
2.1 Ich bin eine Überschrift 2 2
And after it is like this:
Ich bin eine Überschrift 1 ......................................................................................................................... 1
Ich bin eine Überschrift 2 ..................................................................................................................... 1
Ich bin eine Überschrift 3 ................................................................................................................. 1
Ich bin eine Überschrift 2 ..................................................................................................................... 2
Ich bin eine Überschrift 3 ................................................................................................................. 2
Ich bin eine Überschrift 1 ......................................................................................................................... 2
Ich bin eine Überschrift 2 ..................................................................................................................... 2
2.1 Ich bin eine Überschrift 2 ............................................................................................................. 2
The output is different, but the main problem is the missing chapter numbers.
I'm using the following code
Code: Select all
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using SautinSoft.Document;
namespace Example
{
class Program
{
static void Main(string[] args)
{
string text = args.Length > 0 ? args[0] : "README.docx";
updateTOC(text);
}
/// <summary>
/// Creates a new document and saves it as DOCX file.
/// </summary>
/// <remarks>
/// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/save-document-as-docx-net-csharp-vb.php
/// </remarks>
static void updateTOC(string filePath)
{
string pathFile = @"/data/" + filePath;
string resultFile = "/data/" + filePath.Insert(filePath.LastIndexOf('.'), ".modified");
// Load document into DocumentCore
DocumentCore dc = DocumentCore.Load(@"/data/" + filePath);
// Update TOC (TOC can be updated only after all document content is added).
IEnumerable<Element> seq = dc.GetChildElements(true, ElementType.TableOfEntries);
if (seq.FirstOrDefault() != null) {
TableOfEntries toc = (TableOfEntries)seq.FirstOrDefault();
toc.Update();
}
// Update TOC's page numbers.
// Page numbers are automatically updated in that case.
dc.GetPaginator(new PaginatorOptions() { UpdateFields = true });
dc.Save(resultFile, new DocxSaveOptions());
// Open the result for demonstration purposes.
//System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
}
}
}