开发者

Merge multiple <Body> (xml) word documents to 1 document

开发者 https://www.devze.com 2023-01-13 17:47 出处:网络
I have multiple <body> tags from an word document. I do this with the open xml sdk. So the new document should generated with openxml

I have multiple <body> tags from an word document. I do this with the open xml sdk. So the new document should generated with openxml

The body's comes from

WordprocessingDocument.Open("C:\Temp\Test.docx").MainDocumentPart开发者_运维问答.Document.Body.OuterXml

I have so different body's in a list. With al different values. Changed some text in the xml. And saved them in a new list.

Now must that list in an new word document. How can i do that? I tried altChunk. But my word document is always corrupt.

Somebody that can help me?


You can create a WordDocument using the sdk. I think it's

WordProcessingDocument.Create("path_and_name_with_.docx").MainDocumentPart.Document.append(yourBodyList);

You could also take the resulting documents that don't work and look at it's XML using the SDK tool or just rename the doc to .zip extension and see why it doens't work.


This function will throw exception if you are adding yourbodylist from another document without cloning. We have to use CloneNode(deep:true) for each body elements.

WordProcessingDocument.Create("path_and_name_with_.docx").MainDocumentPart.Document.append(yourBodyList); This might cause exception. Refer this post: Cannot insert the OpenXmlElement "newChild" because it is part of a tree

CloneNode(true) will create clone of the element and without any links or references to the parent. And for your multi Body problem. get the child elements of each body and add it to a new Body() element. Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消