我使用altChunkId
元素合并DOCX文档文件,但无法看到控件XML元素的内容。
我认为这是因为获取内容的XML的引用来自它合并到的文件夹,而不是文件夹本身。
在Word 2007中,该文件显示内容控件的内容,而在Word 2010中则为空白。
重现的步骤:
a.docx
a.docx
复制到新文件b.docx
。要合并的代码:
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();
}
}
}
}
使用Word 2010打开b.docx
,您会看到纯文本内容控件为空。
b.docx
,您将看到纯文本内容控件不为空。