【问题标题】:Heading 1, Heading 2 is not highlighted in style ribbon of document after merging docx file合并 docx 文件后,文档样式功能区中标题 1、标题 2 未突出显示
【发布时间】:2014-08-28 01:04:58
【问题描述】:

我正在合并几个 docx 文件,这些文件是通过 C# 使用 openxml 和 wordml 创建的。标题标签为标题 1 、标题 2 等的文件以及带有这些标签的一些文本。当这些文件被单独创建时,如果我们单击或选择那些标记有标题 1 和标题 2 的文本,那么标题 1、标题 2 等会被突出显示,并且导航盘也会显示在标题 1、标题 2 标记上,但是在我们单击或选择这些文本时合并这些文档后,标题 1 和标题 2 没有突出显示。在样式功能区。此处给出了合并的代码,

        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;

【问题讨论】:

  • 标题 1 的样式对于两个不同的源文件是否不同?
  • No Heading 1 的样式对于两个不同的源文件没有不同。

标签: c# openxml wordml


【解决方案1】:

一般来说,当styles.xml 部分中不存在样式定义时,就会出现这种症状。如果在合并过程中,文档内容被继承,但样式部分没有,这可能会导致这个问题。

在一个新的 Word 文档中,只有极少数的基本样式,例如 Normal。在您将该样式分配给段落之前,不会将像标题 1 这样的样式定义添加到 styles.xml。当段落元素包含包中不存在的样式的样式分配时,将忽略该样式。

它也可能出现在表格单元格中,其中表格设置覆盖了样式。例如,在表格中,您可以说第一行(如标题)应该以特定的字体和颜色出现,这将覆盖样式设置。

如果这些都不起作用,如果您在其中一个段落及其直接上下文周围发布少量生成的 XML,这可能会提供一些线索。

【讨论】:

  • 您好,scanny,感谢您的建议,style.xml 包含 Heading1 和 Heading2 样式,实际上在单个文档中没有这样的问题,Heading1 和 Heading2 在其中突出显示,但合并后文件中出现了这个问题。那么,如果你有的话,你能提出任何想法吗?
  • 添加了几个响应的可能性。
  • 嗨,Scanny,感谢您的建议,问题现已解决,我们在合并不同部分时添加了 style.xml,并且我们在 OnOffValue 中传递了 false 值,语法为 AltChunk altChunk = new AltChunk(新的 AltChunkProperties(new MatchSource { Val = new OnOffValue(false) })) { Id = altChunkId };
  • 很高兴听到您成功了 :) 如果您觉得它对您有帮助,请不要忘记接受答案。
猜你喜欢
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多