0

I am trying to merge 2 word documents in binary format, however, it only gets the first document. The resulting document should have all elements from each source document included with formatting.

Here is the code so far. Using OpenXml by the way.

private static byte[] Merge(byte[] dest, byte[] src)
{
    string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString();

    var memoryStreamDest = new MemoryStream();
    memoryStreamDest.Write(dest, 0, dest.Length);
    memoryStreamDest.Seek(0, SeekOrigin.Begin);

    using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStreamDest, true))
    {
        MainDocumentPart mainPart = doc.MainDocumentPart;

        Paragraph para = new Paragraph(new Run((new Break() { Type = BreakValues.Page })));
        mainPart.Document.Body.InsertAfter(para, mainPart.Document.Body.LastChild);

        //Insert the source file into the target file using AltChunk
        AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
        AlternativeFormatImportPartType.WordprocessingML, altChunkId);

        using (MemoryStream mem = new MemoryStream())
        {
            mem.Write(src, 0, (int)src.Length);
            mem.Seek(0, SeekOrigin.Begin);
            chunk.FeedData(mem);
        }

        AltChunk altChunk = new AltChunk();
        altChunk.Id = altChunkId;
        mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Descendants<Paragraph>().Last());
        mainPart.Document.Save();

        return memoryStreamDest.ToArray();
    }
}
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Yuno
  • 1
  • 1
  • 2
    What exactly do you mean by "merge"? – canton7 Nov 25 '21 at 11:43
  • You also never use `altChunk` -- presumably that's unintentional? You also don't save the document properly. – canton7 Nov 25 '21 at 11:44
  • Combining 2 word documents to make it as one file. – Yuno Nov 25 '21 at 11:44
  • 2
    Right, but combining how? Should the result contain all the elements in document 1, followed by all the elements in document 2? If so, why are you messing around with AlternativeFormatImportTypes? What happens if the documents have different styles, etc? This is going to be hard if the documents have lists as well, since you need to manage the numbering part of the document. – canton7 Nov 25 '21 at 11:45
  • That's what I am planning to do, should have both elements included with formatting. Do you have something to recommend? Thanks! – Yuno Nov 25 '21 at 11:49
  • Related: https://social.msdn.microsoft.com/Forums/en-US/a1449c87-3ae4-404f-abdf-f8bc098e9048/join-multiple-word-documents-into-one-open-xml?forum=aspgettingstarted and https://stackoverflow.com/questions/34600564/openxmlpowertools-documentbuilder-merging-documents-on-a-seperate-page. Using the AlternativeFormatImportTypes approach could work looking at the first link, but you'll need to fix the obvious errors in your code first – canton7 Nov 25 '21 at 11:52
  • Manage to make this one worked. – Yuno Nov 26 '21 at 03:42
  • @Yuno - *Manage to make this one worked* - in that case might you add a [self answer](https://stackoverflow.com/help/self-answer) letting us know how you resolved your problem? Thanks! – dbc Nov 29 '21 at 03:34

0 Answers0