无法从Word 2010文件合并XML文档内容

时间:2011-08-29 23:46:34

标签: c# openxml office-2007 openxml-sdk office-2010

我使用altChunkId元素合并DOCX文档文件,但无法看到控件XML元素的内容。

我认为这是因为获取内容的XML的引用来自它合并到的文件夹,而不是文件夹本身。

在Word 2007中,该文件显示内容控件的内容,而在Word 2010中则为空白。

重现的步骤:

  1. Open word 2010。
  2. 转到开发人员标签。
  3. 添加“纯文本内容控制”。
  4. 另存为a.docx
  5. 使用Word 2007 content control toolkit
  6. 创建XML绑定
  7. a.docx复制到新文件b.docx
  8. 要合并的代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.IO.Packaging;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    class Class1
    {
        static void Main(string[] args)
        {
            string doc2 = @"b.docx";
            string doc1 = @"a.docx";
            using (var myDoc = WordprocessingDocument.Open(doc2, true))
            {
                string altChunkId = "AltChunkId1";
                var mainPart2 = myDoc.MainDocumentPart;
    
                var chunk = mainPart2.AddAlternativeFormatImportPart(
                AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                using (var fileStream = File.Open(doc1, FileMode.Open))
                {
                    chunk.FeedData(fileStream);
                    AltChunk altChunk = new AltChunk();
                    altChunk.Id = altChunkId;
                    mainPart2.Document.Body.InsertAfter(altChunk, mainPart2.Document.Body.Elements<Paragraph>().Last());
                    mainPart2.Document.Save();
                }
            }
        }
    }
    
  9. 使用Word 2010打开b.docx,您会看到纯文本内容控件为空。

  10. 使用Word 2007打开b.docx,您将看到纯文本内容控件不为空。

0 个答案:

没有答案