0
votes

I'm trying to embed an OpenXML word document inside of another OpenXML word document and it's creating corrupted files. I've already review this post http://blogs.msdn.com/b/brian_jones/archive/2009/06/30/embedding-an-open-xml-file-in-another-open-xml-file.aspx and this stack overflow post Embedding an OpenXML document within another OpenXml document and I think I'm following the instructions, but I'm not getting the same results.

My code looks something like this.

foreach (KeyValuePair<string, byte[]> docKV in files)
            {
                var embeddedPackagePart = document.MainDocumentPart.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "rfp" + docKV.key);
                var stream = new MemoryStream(docKV.Value);
                embeddedPackagePart.FeedData(stream);
                stream.Close();
}

So essentially I'm looping through a list of files stored in a dictionary and embedding each file. I've removed the code that creates the actual OpenXML markup to link to the embedded object to try and narrow down the error. As far as I can tell the actual embedding is what creates the issue.

I've also opened the generated document using the OpenXML SDK Productivity tool, and it is missing the embeddings section that I see on documents where I manually embedded a file.

Any thoughts on what I'm doing wrong?

2

2 Answers

0
votes

You can read Merging word processing documents. Sample code is available too.

0
votes

I figured out the issue. I was not calling WordprocessingDocument.Close before disposing of the WordprocessingDocument. If you are adding new document parts you have to invoke Close() for those to get written to the underlying stream.