Combine .docx Word 2007 documents with the Microsoft Office Open XML SDK 2.0

First install the Microsoft Office Open XML SDK 2.0 (I installed the December 2009 CTP). Now open up Visual Studio 2008, create a project and add a reference to “DocumentFormat.OpenXml” (right click on project -> Add Reference). I also had to add a reference to “WindowsBase” at “C:Program FilesReference AssembliesMicrosoftFrameworkv3.0″.

Now add the following to your “using” statements at the top of your code:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.IO.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.IO.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

Now if you are combining “c:doc1.docx” and “c:doc2.docx” by adding doc2 to the end of doc1 you use the following code:

string doc1 = “c:\1.docx”;
string doc2 = “c:\2.docx”;
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(doc1, true))
{
string altChunkId = “AltChunkId2″;
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (FileStream fileStream = File.Open(doc2, FileMode.Open))
chunk.FeedData(fileStream);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
mainPart.Document.Save();
}

The end result is all the content of “doc2.docx” neatly inserted at the end of “doc1.docx”.

2 thoughts on “Combine .docx Word 2007 documents with the Microsoft Office Open XML SDK 2.0

  1. Hi,

    I am trying to merge word documents in sharepoint document library. Some pages in the docs are in portrait and some in landscape. after merging documents all the pages in the documents r displayed in portrait mode. how can i retain page orientation programmatically ?

    fyi…i think we can do it by inserting section properties after each page or each document.

    here is my code

    Appreciate your help..

    foreach (SPFile item in listitem.Folder.Files)

    {

    // SPFile inputFile = item.File;

    SPFile inputFile = item;

    string altChunkId = “AltChunkId” + id;

    id++;

    byte[] byteArray = inputFile.OpenBinary();

    AlternativeFormatImportPart chunk = outputDoc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML,

    altChunkId);

    using (MemoryStream mem = new MemoryStream())

    {

    mem.Write(byteArray, 0, (int)byteArray.Length);

    mem.Seek(0, SeekOrigin.Begin);

    chunk.FeedData(mem);

    }

    AltChunk altChunk = new AltChunk();

    altChunk.Id = altChunkId;

    outputDoc.MainDocumentPart.Document.Body.InsertAfter(altChunk,

    outputDoc.MainDocumentPart.Document.Body.Elements().Last());

    outputDoc.MainDocumentPart.Document.Save();

    }

    outputDoc.Close();

    memOut.Seek(0, SeekOrigin.Begin);

    ClientContext clientContext = new ClientContext(SPContext.Current.Site.Url);

    ClientOM.File.SaveBinaryDirect(clientContext, outputPath, memOut, true);

    // Conversion

    • Could you please send me the working code ASAP. My requirement is similar what you have mentioned. Even I have to combine two sharepoint document with no loss to formatting and layout. Your help is highly appreciable

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>