How to merge word documents with different headers using openxml? RRS feed

  • Question

  • I am trying to merge multiple documents into a single. I am using AltChunk altChunk = new AltChunk(). When documents are merged, it does not seem to retain seperate hearders of each document. The merged document will contain the headers of the first document during the merging. If the first document being merged contains no hearders, then all the rest of the newly merged document will contain no headers, and vise versa.

    My question is, how can I preserve different headers of the documents being merged?

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

    namespace WordMergeProject
        public class Program
            private static void Main(string[] args)
                byte[] word1 = File.ReadAllBytes(@"..\..\word1.docx");
                byte[] word2 = File.ReadAllBytes(@"..\..\word2.docx");

                byte[] result = Merge(word1, word2);

                File.WriteAllBytes(@"..\..\word3.docx", result);

            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);
                var memoryStreamSrc = new MemoryStream(src);

                using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStreamDest, true))
                    MainDocumentPart mainPart = doc.MainDocumentPart;
                    AlternativeFormatImportPart altPart =
                        mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                    var altChunk = new AltChunk();
                    altChunk.Id = altChunkId;
                                  OpenXmlElement lastElem = mainPart.Document.Body.Elements<AltChunk>().LastOrDefault();
                if(lastElem == null)
                    lastElem = mainPart.Document.Body.Elements<Paragraph>().Last();

                //Page Brake einfügen
                Paragraph pageBreakP = new Paragraph();
                Run pageBreakR = new Run();
                Break pageBreakBr = new Break() { Type = BreakValues.Page };


                return memoryStreamDest.ToArray();

    Thursday, January 9, 2020 8:22 AM