Код: Выделить всё
< /code>
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();
}
private Paragraph _AddTableOfContentFormatting()
< /code>
{ // 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 // Add TOC field with switches return new Paragraph( new ParagraphProperties( new ParagraphStyleId { Val = "TOCHeading" }, new Tabs( new TabStop { Val = TabStopValues.Right, Leader = TabStopLeaderCharValues.None, Position = 9000 } // Right-align page numbers without dots ) ), new Run( new FieldChar { FieldCharType = FieldCharValues.Begin, Dirty = true } // Mark as dirty ), 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 } ) ); }< /code>
how to update table of content when i open word for first time.
Подробнее здесь: https://stackoverflow.com/questions/797 ... in-c-sharp