Created
June 4, 2012 15:42
-
-
Save paratechnical/2869131 to your computer and use it in GitHub Desktop.
Merging 2 word 2007(open xml) documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void MergeDocs(string doc1Path,string doc2Path) | |
{ | |
using (var doc2FileStream = File.Open(doc2Path, FileMode.Open)) | |
{ | |
using (WordprocessingDocument doc2 = WordprocessingDocument.Open(doc2FileStream, true)) | |
{ | |
var doc2Body = (Body)doc2.MainDocumentPart.Document.Body.CloneNode(true); | |
using (var doc1FileStream = File.Open(doc1Path, FileMode.Open)) | |
{ | |
using (WordprocessingDocument doc1 = WordprocessingDocument.Open(doc1FileStream, true)) | |
{ | |
var mainPart = doc1.MainDocumentPart; | |
foreach (var elem in doc2Body.ChildElements) | |
{ | |
if (!(elem is SectionProperties)) | |
mainPart.Document | |
.Body | |
.InsertAfter(elem.CloneNode(true), mainPart.Document.Body.Elements<paragraph>().Last()); | |
} | |
mainPart.Document.Save(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment