Search code examples
c#openxmlwordml

Heading 1, Heading 2 is not highlighted in style ribbon of document after merging docx file


I am merging few docx files, those files were created using openxml and wordml through C#. Those files having heading tag as heading 1 , heading 2 etc. along with some text with these tags. When those files are created individually then if we click or select those text which are tagged with heading 1 and heading 2, then the Heading 1, Heading 2 etc are getting highlighted and the navigation pan are also showing against those Heading 1, Heading 2 tags, but after merging those documents when we click or select these text the Heading 1 and Heading 2 is not getting highlighted. in the style ribbon. The code for that merging is given here,

        MemoryStream ms = new MemoryStream();

        using (WordprocessingDocument myDoc =
WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
            mainPart.Document = new Document { Body = new Body() };
            int counter = 1;
            foreach (var sectionOutput in sectionOutputs)
            {
                foreach (var outputFile in sectionOutput.Files)
                {
                    Paragraph sectionBreakPara = null;
                    if (!sectionOutput.SectionType.Equals(sectionOutputs[sectionOutputs.Count - 1].SectionType))
                    {
                        if (outputFile == sectionOutput.Files.Last())
                        //check whether this is the last file in this section
                        {
                            using (
                                WordprocessingDocument pkgSourceDoc =
                                    WordprocessingDocument.Open(outputFile.OutputStream, true))
                            {
                                var sourceBody = pkgSourceDoc.MainDocumentPart.Document.Body;

                                SectionProperties docSectionBreak =
                                    sourceBody.Descendants<SectionProperties>().LastOrDefault();
                                if (docSectionBreak != null)
                                {
                                    var clonedSectionBreak = (SectionProperties)docSectionBreak.CloneNode(true);
                                    clonedSectionBreak.RemoveAllChildren<FooterReference>();
                                    clonedSectionBreak.RemoveAllChildren<HeaderReference>();
                                    sectionBreakPara = new Paragraph();
                                    ParagraphProperties sectionParaProp = new ParagraphProperties();
                                    sectionParaProp.AppendChild(clonedSectionBreak);
                                    sectionBreakPara.AppendChild(sectionParaProp);
                                }
                            }
                        }
                    }

                    string altChunkId = string.Format("altchunkId{0}", counter);
                    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                        AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                    outputFile.OutputStream.Seek(0, SeekOrigin.Begin);

                    chunk.FeedData(outputFile.OutputStream);
                    AltChunk altChunk = new AltChunk(new AltChunkProperties(new MatchSource { Val = new OnOffValue(true) })) { Id = altChunkId };

                    mainPart.Document.Body.AppendChild(altChunk);

                    if (sectionBreakPara != null)
                    {
                        mainPart.Document
                           .Body
                           .AppendChild(sectionBreakPara);
                    }

                    counter++;
                }
            }


            mainPart.Document.Save();
        }

        return ms;

Solution

  • In general, this symptom arises when the style definition is not present in the styles.xml part. If during the merge process the document content was carried over but the styles parts weren't, that could cause this problem.

    In a new Word document, there are only a very few basic styles, like Normal. A style definition like Heading 1 is not added to the styles.xml until you assign that style to a paragraph. When a paragraph element contains a style assignment for a style not present in the package, the style is ignored.

    It can also arise in table cells, where a table setting is overriding the style. For example, in a table you can say the first row (like headings) should appear in a particular font and color, and that will override a style setting.

    If neither of those works, if you post a smallish amount of the XML that's generated, right around one of the paragraphs and its immediate context, that might give some clues.