merging multiple docx files using open xml.

Answered merging multiple docx files using open xml.

  • Friday, July 06, 2012 1:06 PM
     
     

    hi,

    i want to merge  multible word docx files by using C# in openxml , plz can any one help me.

    actually i tried with Altchunk but it throwing error like "document containing altchunk". so is there any other approach to fisx this problem.

    thanks ,

    in advance

All Replies

  • Tuesday, July 10, 2012 6:47 AM
    Moderator
     
     Answered Has Code

    Hi Bollineni,

    Thanks for posting in the MSDN forum.

    I would recommend you try following snippet:

    using (WordprocessingDocument myDoc =
        WordprocessingDocument.Open("Test1.docx", true))
    {
        string altChunkId = "AltChunkId1";
        MainDocumentPart mainPart = myDoc.MainDocumentPart;
        AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
            AlternativeFormatImportPartType.WordprocessingML, altChunkId);
        using (FileStream fileStream = File.Open("TestInsertedContent.docx", 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();
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, July 10, 2012 10:31 AM
     
     
    You can try using this toolkit. It is basically a document generation toolkit/library for .NET but it is also very capable at merging documents. It doesn't use the altChunk technique but instead "really merges" sub documents into a mester document. This is a much better approach for two reasons: 1) a generated document can be also open in a non MS Word apps (like Open Office) which do not support altChunks and 2) you are not constrained to insert sub-documents at the root of the master document but can also insert it into e.g. a table cell or a middle of a paragraph. The minus is that the toolkit is a commercial product and it will cost you some money.
  • Tuesday, July 10, 2012 12:21 PM
     
     

    thank u TOM,

    YOUR SNIPPET WORKING FINE.