locked
merging multiple docx files using open xml. RRS feed

  • Question

  • 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

    Friday, July 6, 2012 1:06 PM

Answers

  • 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 6:47 AM

All replies

  • 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 6:47 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 10:31 AM
  • thank u TOM,

    YOUR SNIPPET WORKING FINE.

    Tuesday, July 10, 2012 12:21 PM
  • When I am using the same code under a loop, it overwrites the content with the last file.

    Means instead of getting 10 different different content on page, I end up getting the same content 10 times, which is basically the content of the 10th file.

    Wednesday, August 21, 2013 2:06 PM
  • Are you able to work with multiple files?

    In my case code works for two files perfectly, but it is overwriting the last file content, in case I have more than two files.

    Wednesday, August 21, 2013 2:15 PM
  • Hello

    Thanks for the code.

    I have implemented the code and I can see the code is working fine, that's good.
    But there are some issues I am facing,
    1. While opening a file into the MS Word desktop app, it is giving us a message like "Word found unreadable content in "*.docx". Do you want to recover the contents of this document? If you trust the source of this document, click yes.". After clicking YES, the files gets opened properly but while saving it is not allow us to save into the same. rather it is asking us to save it to another location as another file.
    2. After uploading the same file on SharePoint, it also not allow to edit this file. It is giving us the message "Sorry, this document can't be opened because it contains objects that Word doesn't support in a browser. To edit this document please open it in the desktop version of Microsoft Word.". But once I correct the file from MS Word desktop app, the corrected file is working fine.
    Monday, November 9, 2020 7:48 AM