0
votes

i create word document form MemoryStream using openxml

Here is the code I am using:

 System.IO.MemoryStream ms1=new MemoryStream();
 using (var mainDoc = WordprocessingDocument.Create(ms1, WordprocessingDocumentType.Document))
 {
    mainDoc.AddMainDocumentPart();
    mainDoc.MainDocumentPart.Document =
                new Document(new Body(new Paragraph(new Run(new Text("Hello World 1!")))));
                mainDoc.MainDocumentPart.Document.Save();
 }

 System.IO.MemoryStream ms2 = new MemoryStream();
 using (var mainDoc = WordprocessingDocument.Create(ms2,WordprocessingDocumentType.Document))
 {
                mainDoc.AddMainDocumentPart();
                mainDoc.MainDocumentPart.Document =
                new Document(new Body(new Paragraph(new Run(new Text("Hello World 2!")))));
                mainDoc.MainDocumentPart.Document.Save();
  }

i can save one MemoryStream to FileStream ,and save to .docx file than open it, but how could i merge two MemoryStream word document as one word document in MemoryStream or FileStream than save it?

1
Have you looked into OpenXmlPowerTools? More info on Eric White's site - erdomke

1 Answers

0
votes

Merging two documents using Open XML is quite a demanding task. I suggest you to use either

  1. Eric White's OpenXmlPowerTools or
  2. One of the .NET libraries such as Docentric Toolkit OpenXmlPowerTools is free and is capable of merging two documents, but you have no control over merging process. On the other side Docentric Toolkit is not free but allows you to control the merging proces to a great detail, e.g. how styles and sections get merged, how page numbering restarts...