public void Merge(Stream doc1, Stream doc2) { var wordDoc = WordprocessingDocument.Open(doc1, true); var mainPart = wordDoc.MainDocumentPart; var chunk = mainPart.AddAlternativeFormatImportPart( DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML); doc2.Position = 0; chunk.FeedData(doc2); var altChunkId = mainPart.GetIdOfPart(chunk); var altChunk = new DocumentFormat.OpenXml.Wordprocessing.AltChunk(); altChunk.Id = altChunkId; Paragraph para = new Paragraph(new Run((new Break() { Type = BreakValues.Page }))); mainPart.Document.Body.InsertAfter(para, mainPart.Document.Body.LastChild); mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.LastChild); wordDoc.Close(); }
void AddAltChunk(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename) { string altChunkId = "AltChunkId" + id; id++; byte[] byteArray = filename.OpenBinary(); AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart( AlternativeFormatImportPartType.WordprocessingML, altChunkId); using (MemoryStream mem = new MemoryStream()) { mem.Write(byteArray, 0, (int)byteArray.Length); mem.Seek(0, SeekOrigin.Begin); chunk.FeedData(mem); } Word.AltChunk altChunk = new Word.AltChunk(); altChunk.Id = altChunkId; // Replace content control with altChunk information. OpenXmlElement parent = sdt.Parent; parent.InsertAfter(altChunk, sdt); sdt.Remove(); }