title: How to merge word documents with different headers using openxml?
body: <p>I am trying to merge multiple documents into a single one by following examples as posted in this other post.
  I am using <code>AltChunk altChunk = new AltChunk()</code>. 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.</p>
  
  <p>My question is, how can I preserve different headers of the documents being merged?</p>
  
  <p><a href="https://stackoverflow.com/questions/18351829/merge-multiple-word-documents-into-one-open-xml">Merge multiple word documents into one Open Xml</a></p>
  
  <pre><code>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);
                  altPart.FeedData(memoryStreamSrc);
                  var altChunk = new AltChunk();
                  altChunk.Id = altChunkId;
                                OpenXmlElement lastElem = mainPart.Document.Body.Elements&lt;AltChunk&gt;().LastOrDefault();
              if(lastElem == null)
              {
                  lastElem = mainPart.Document.Body.Elements&lt;Paragraph&gt;().Last();
              }
  
  
              //Page Brake einfügen
              Paragraph pageBreakP = new Paragraph();
              Run pageBreakR = new Run();
              Break pageBreakBr = new Break() { Type = BreakValues.Page };
  
              pageBreakP.Append(pageBreakR);
              pageBreakR.Append(pageBreakBr);                
  
              return memoryStreamDest.ToArray();
          }
      }
  }
  </code></pre>