繁体   English   中英

使用INCLUDETEXT字段访问包含在文档中的CustomXMLPart

[英]Accessing CustomXMLPart in document included using INCLUDETEXT field

我有一个docx Word文档,其中包含与CustomXMLPart中的数据绑定的内容控件。

然后,使用INCLUDETEXT将这个文档(或其中的书签)包含在另一个Word文档中。

当第一个文档包含在第二个文档中时,有什么方法可以从原始文档中获取CustomXMLPart(我已经有一个VSTO Word Addin在Word中运行着,看着该文档)?

我想要做的是将其与第二个文档中已经存在的CustomXMLParts合并,以便内容控件仍绑定到XMLPart中的数据。

另外,还有另一种方法可以不使用INCLUDETEXT字段来执行此操作吗?

我认为使用VSTO和IncludeText字段可能无法实现这一点,并使用altChunks作为替代方法进行了调查。

在打开文件之前,我已经使用Open XML SDK 2对文件进行了一些处理,因此可以进行额外的工作以将文档合并在一起。

尽管使用altChunk方法将整个第二个文档(包括其自己的CustomXmlParts)嵌入到第一个文档中,但是在打开文档并将第二个文档与第一个文档合并时,Word会丢弃CustomXmlParts。

我最终得到了类似于以下内容的代码。 它用altChunk数据替换了定义的Content Controls,并将特定的CustomXmlParts合并在一起。

    private static void CreateAltChunksInWordDocument(WordprocessingDocument doc, string externalDocumentPath)
    {
        foreach (var control in doc.ContentControls().ToList()) //Have to do .ToList() on this as when we update the Doc in the loop it stops enumerating otherwise
        {
            SdtProperties props = control.Elements<SdtProperties>().FirstOrDefault();
            if (props == null)
                continue;

            SdtAlias alias = props.Elements<SdtAlias>().FirstOrDefault();
            if (alias == null || !alias.Val.HasValue || alias.Val.Value != "External Template")
                continue;

            using (WordprocessingDocument externaldoc = WordprocessingDocument.Open(externalDocumentPath, false))
            {
                //Replace the Content Control with an AltChunk section, and stream in the external file
                string altChunkId = "AltChunkId" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "");

                AlternativeFormatImportPart chunk = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                chunk.FeedData(File.OpenRead(externalDocumentPath));

                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;

                OpenXmlElement parent = control.Parent;
                parent.InsertAfter(altChunk, control);
                control.Remove();

                XDocument xDocMain;
                CustomXmlPart partMain = MyCommon.GetMyXmlPart(doc.MainDocumentPart, out xDocMain);

                XDocument xDocExternal;
                CustomXmlPart partExternal = MyCommon.GetMyXmlPart(externaldoc.MainDocumentPart, out xDocExternal);

                if (xDocMain != null && partMain != null && xDocExternal != null && partExternal != null)
                {
                    MyCommon.MergeXmlPartFields(xDocMain, xDocExternal);

                    //Save the updated part
                    using (Stream outputStream = partMain.GetStream())
                    {
                        using (StreamWriter ts = new StreamWriter(outputStream))
                        {
                            ts.Write(xDocMain.ToString());
                        }
                    }
                }
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM