Add table of contents into second page of word document

Home Forums WordprocessingML Add table of contents into second page of word document

This topic contains 1 reply, has 2 voices, and was last updated by  Eric White 7 years, 10 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3437

    sahil joshi
    Participant

    Hello Eric,

    I have requirement of adding table of contents to second page of word document while exporting using Html string. My first page will always be Cover page. Below is the code which I am using currently for adding table of contents, currently it was adding to first page only. Please do needful and update me asap.

    using (WordprocessingDocument wdoc =
    WordprocessingDocument.Open(Filepath, true))
    {
    AddSettingsToMainDocumentPart(wdoc.MainDocumentPart);
    XElement firstPara = wdoc.MainDocumentPart.GetXDocument().Descendants(W.altChunk).FirstOrDefault();
    OpenXmlPowerTools.TocAdder.AddToc(wdoc, firstPara,
    @”TOC \o ‘1-3’ \h \z \u”, null, null);

    }

    public void AddSettingsToMainDocumentPart(MainDocumentPart part)
    {
    if (part.DocumentSettingsPart == null)
    {
    DocumentSettingsPart settingsPart = part.AddNewPart<DocumentSettingsPart>();
    FileStream settingsTemplate = new FileStream(Server.MapPath(“~/Context/settings.xml”), FileMode.Open, FileAccess.Read);
    settingsPart.FeedData(settingsTemplate);
    settingsPart.Settings.Save();
    }
    }

    #3438

    Eric White
    Keymaster

    Hi Sahil,

    The first question is how do you know the end of the cover page? Do you have a hard page break afterwards?

    The key point is that you need to find the first paragraph of the second page, and add the altChunk element immediately before.

    Unfortunately, my current schedules and deadlines prohibit me from writing this sample code, but I’m sure that you can work it out. Have you viewed the introduction videos for Open XML and WordprocessingML?

    Introduction to Open XML

    Introduction to WordprocessingML

    From those videos, you can learn how to make the appropriate changes to your code so that you can add the TOC anywhere you want to add it.

    Cheers, Eric

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.