I’m merging multiple Word documents together and adding a table of contents at the beginning to link to each merged document.
I don’t control the documents, so for the table of contents to work correctly I’m not using the headers and instead I’m manually inserting TC fields at the start of each merged document to identify the places I want to show up in the ToC.
This all works, except that the ToC is getting the formatting from the merged document instead of using the TOC1 style, which is contrary to what the documentation says:
The formatting of entries in the TOC do not use the original styles of the marked entries, instead each level is formatted using an equivalent TOC style
Here is a document with formatting at the beginning - this causes my ToC to have a bold and yellow highlighted entry:
ComplyVision_General Brochure.docx[826_978].docx (7.7 KB)
Here is my code:
var mergedDoc = new Document();
var mergedDocBuilder = new DocumentBuilder(mergedDoc);
mergedDocBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title;
mergedDocBuilder.Writeln(title);
mergedDocBuilder.InsertBreak(BreakType.PageBreak);
// https://support.microsoft.com/en-us/office/field-codes-toc-table-of-contents-field-1f538bc4-60e6-4854-9f64-67754d78d05c
mergedDocBuilder.InsertTableOfContents(@"\h \z \f p");
var policiesWithDocuments = policies
.Where(p => p.Document != null)
.ToList();
foreach (var policy in policiesWithDocuments)
{
using var vs = await _documentService.DownloadPolicyDocumentAsStreamAsync(policy.Document!.Id, false);
var policyDoc = new Document(vs.Stream);
policyDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
policyDoc.FirstSection.HeadersFooters.LinkToPrevious(false);
var builder = new DocumentBuilder(policyDoc);
builder.InsertField($@"TC ""{policy.Name}"" \f p");
foreach (var srcSection in policyDoc.Sections)
{
var dstSection = mergedDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
mergedDoc.AppendChild(dstSection);
}
}
mergedDoc.UpdateFields();
mergedDoc.UpdatePageLayout();
var ms = new MemoryStream();
mergedDoc.Save(ms, SaveFormat.Pdf);