简体   繁体   中英

how to merge multiple word document form MemoryStream using openxml

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?

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, eg how styles and sections get merged, how page numbering restarts...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM