Настройка UpdateFieldsonopen < /code> программно: < /li>
< /ul>
Код: Выделить всё
private void SetAutoUpdateFields(WordprocessingDocument doc)
{
if (doc.MainDocumentPart.DocumentSettingsPart == null)
{
doc.MainDocumentPart.AddNewPart();
}
if (doc.MainDocumentPart.DocumentSettingsPart.Settings == null)
{
doc.MainDocumentPart.DocumentSettingsPart.Settings = new Settings();
}
var settings = doc.MainDocumentPart.DocumentSettingsPart.Settings;
// Clear existing settings
settings.Elements().ToList().ForEach(x => x.Remove());
// Add field update setting
settings.AppendChild(new UpdateFieldsOnOpen() { Val = true });
settings.Save();
}
< /code>
Добавление поля TOC с грязным флагом: < /li>
< /ul>
private Paragraph _AddTableOfContentFormatting()
{
// Add TOC field with switches
// \h - Creates hyperlinks for entries
// \z - Hides tab leader and page numbers in Web layout view
// \u - Uses the applied paragraph outline level
// \n - No tab leader
// \p - Removes page numbers
return new Paragraph(
new ParagraphProperties(
new ParagraphStyleId { Val = "TOCHeading" },
new Tabs(
new TabStop { Val = TabStopValues.Right, Leader = TabStopLeaderCharValues.None, Position = 9000 }
)
),
new Run(new FieldChar { FieldCharType = FieldCharValues.Begin, Dirty = true }),
new Run(new FieldCode("TOC \o \"1-3\" \h") { Space = SpaceProcessingModeValues.Preserve }),
new Run(new FieldChar { FieldCharType = FieldCharValues.Separate }),
new Run(new FieldChar { FieldCharType = FieldCharValues.End })
);
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... in-c-sharp