【问题标题】:OpenXML 2.5 - WordProcessing - How to copy the styles from template document when creating a new document?OpenXML 2.5 - WordProcessing - 创建新文档时如何从模板文档中复制样式?
【发布时间】:2014-12-29 23:47:56
【问题描述】:

我有一个模板文档,其中包含一些段落和表格以及一些与之相关的样式。

我需要根据序列从模板文档中选择元素,然后将它们附加到我的新文档的正文中。下面是我的复制代码。我还需要以某种方式复制与元素关联的样式。尽管应用了一些样式,但字体大小和表格边框等内容并未复制到新文档中。任何帮助将非常感激。谢谢

   Dictionary<string, string> sequence = GetSequence();
            using (WordprocessingDocument templateDocument = WordprocessingDocument.Open(sourceFileLocation, false))
            {
                Body templateBody = templateDocument.MainDocumentPart.Document.Body;
                using (
                    WordprocessingDocument wordDoc = WordprocessingDocument.Create(destinationFileLocation,
                        WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
                    mainPart.Document = new Document();
                    Body wordDocDocBody = mainPart.Document.AppendChild(new Body());
                   //don't think the below two lines work as I intended.
                    ThemePart themePart1 = templateDocument.MainDocumentPart.ThemePart;
                    mainPart.AddPart(themePart1);

                    foreach (var item in sequence)
                    {
                     var block = templateBody.Elements().TakeWhile(x => x.InnerText == item.Key);
                        foreach (var blockItem in block)
                        {
                            wordDocBody.Append(blockItem.CloneNode(true));
                        }
                    }
                }
            }

【问题讨论】:

    标签: c# .net ms-word openxml openxml-sdk


    【解决方案1】:

    以下代码将具有固定名称的样式(在本例中为“固定”表格样式)从一个 Word 文档复制到另一个。

    void CopyTableStyles( WordprocessingDocument source, WordprocessingDocument destination )
            {
                var sourceStyles = source.MainDocumentPart.StyleDefinitionsPart.RootElement;
                var tableStyle = sourceStyles.Descendants<Style>()
                                    .Where<Style>( s => s.StyleName.Val == "Light List - Accent 11")
                                    .First<Style>();
                if ( tableStyle != null )
                {
                    var destinationStyles = destination.MainDocumentPart.StyleDefinitionsPart.RootElement;
                    destinationStyles.AppendChild( tableStyle.CloneNode( true ) );
                    destination.MainDocumentPart.StyleDefinitionsPart.PutXDocument();
                }
                return;
            }
    

    您应该能够从这里开始工作,以使其更加健壮并适合您的需要。

    编辑:抱歉,PutXDocument 是一种扩展方法,最初取自 Eric White 在 Open XML Developer 站点上的一个示例。

    public static void PutXDocument(this OpenXmlPart part)
    {
        XDocument xdoc = part.GetXDocument();
        if (xdoc != null)
        {
            // Serialize the XDocument object back to the Package.
            using (XmlWriter xw = XmlWriter.Create( part.GetStream( FileMode.Create, FileAccess.Write )))
            {
                xdoc.Save( xw );
            }
        }
    }
    

    上面的代码在我当前的“技巧包”中的一个 OpenXmlPart 静态扩展类中——我怀疑自从阅读了 White 先生提供的博客文章/示例后我还没有更改它。

    【讨论】:

      【解决方案2】:

      在这一行:var block = templateBody.Elements().TakeWhile(x =&gt; x.InnerText == item.Key); 您只选择了Text 对象。因此,格式被遗漏了。您还需要将此 Text 对象的父对象复制到新文档中。您可能需要将 Paragraph children 复制到 RunText 以复制格式。

      【讨论】:

      • 您的解决方案只能部分工作。如果样式已应用于文本,则 OP 必须引用 Styles.xml 部分中的样式并将它们也添加到目标文档中。
      • 从我上面的代码的上下文中,在将 blockItem 附加到目标文档的正文之前,我是否需要创建一个 ParagraphProperties 类型的对象并从模板文档中克隆实际段落的属性?谢谢。
      【解决方案3】:

      您的问题的解决方案将相当大,所以我只是想说明需要做什么,您可以为此编写代码并在遇到困难时返回。

      正如@tr4nc3 所提到的,考虑将父母复制到Text,如果您要复制整个段落,请将Paragraph 对象从源复制到目标,否则Run 对象就足够了。

      如果存在将样式应用于段落的情况,那么您必须获取该段落的ParagraphStyle.Val 并使用该引用将Style 对象从StylePart 复制到目标对象.

      我知道有很多事情需要完成,但是,如果您使用 Open XML Productivity Tool,它会让您的生活更轻松。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多