您可以在更新 TOC 元素后“手动”对 TOC 条目进行排序,如下所示:
var document = DocumentModel.Load("input.docx");
var toc = (TableOfEntries)document.GetChildElements(true, ElementType.TableOfEntries).First();
toc.Update();
// Take current TOC entries.
var entries = toc.Entries.ToList();
// Remove them.
toc.Entries.Clear();
// Then place them back in sorted order.
entries.Sort((e1, e2) => e1.Content.ToString().CompareTo(e2.Content.ToString()));
entries.ForEach(e => toc.Entries.Add(e));
document.Save("output.docx");
但请注意,当再次更新 TOC 元素时,无论是使用 GemBox.Document 还是使用某些 Word 应用程序,您都会得到未排序的条目。
您在这里唯一可以做的就是防止其他人更新您的 TOC 元素,例如通过删除 TOC 并仅保留其条目(也就是取消链接目录):
toc.Content.Set(toc.Entries.Content);
或者您可以将 TOC 放在受保护的部分中,从而防止 Word 应用程序更新 TOC(请注意,您仍然可以使用 GemBox.Document 更新它):
// You mentioned that you're inserting the TOC element at the beginning
// which means that the "toc.Parent" should be document's first Section.
var section = document.Sections[0];
// If TOC is not the only element in a section then place it inside its own section.
if (section.Blocks.Count > 1)
{
var tocSection = new Section(document);
tocSection.PageSetup = section.PageSetup.Clone();
tocSection.Blocks.Add(toc.Clone(true));
document.Sections.Insert(0, tocSection);
section.PageSetup.SectionStart = SectionStart.Continuous;
toc.Content.Delete();
}
document.Protection.StartEnforcingProtection(EditingRestrictionType.FillingForms, "optional password");
// Unprotect all other Sections except the on that contains TOC element.
foreach (var otherSection in document.Sections.Skip(1))
otherSection.ProtectedForForms = false;