public void DocMerger()
{
var source1 = Server.MapPath(Url.Content("~/1.docx")); //source 1
var source2 = Server.MapPath(Url.Content("~/2.docx")); //source 2
var merged = Server.MapPath(Url.Content("~/App_Data/merged.docx")); //merged
var f1 = new FileInfo(source1);
var f2 = new FileInfo(source2);
//Use DocumentBuilder and merge the files
var sources = new List<OpenXmlPowerTools.Source>()
{
new Source(new WmlDocument(f1.FullName),true),
new Source(new WmlDocument(f2.FullName),true),
};
var mergedDocument = DocumentBuilder.BuildDocument(sources);
mergedDocument.SaveAs(merged); //save merged data as merged.docx
}
Able to merge the documents but 2nd documents is appended immediately after 1st page last para. But I need to get the 2nd document to come in a new page. Any one can please suggest. I have tried with open xml also but the formatting of the second document is not coming up correctly. Any suggestions please
using (WordprocessingDocument myDoc = WordprocessingDocument.Open("D:\\Test.docx", true))
{
string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2) ;
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (FileStream fileStream = File.Open("D:\\Test1.docx", FileMode.Open))
chunk.FeedData(fileStream);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document
.Body
.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
mainPart.Document.Save();
}
```