Skip to content

Instantly share code, notes, and snippets.

@paratechnical
Created June 4, 2012 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paratechnical/2869131 to your computer and use it in GitHub Desktop.
Save paratechnical/2869131 to your computer and use it in GitHub Desktop.
Merging 2 word 2007(open xml) documents
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