1
 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();
        } 
```
 
Rams
  • 11
  • 2

1 Answers1

1

Here is a solution using OpenXml sdk, Hope you will get all formatting without word file theme's styling.

// Set the file paths for the two documents that you want to merge
string file1 = @"D:\wordFiles\File 1.docx";
string file2 = @"D:\wordFiles\File 2.docx";

// Create a new Word document
using (var destinationDocument = WordprocessingDocument.Create(@"D:\wordFiles\mergedDocument.docx", WordprocessingDocumentType.Document))
{
    // Add the main document part for the destination document
    var mainPart = destinationDocument.AddMainDocumentPart();

    // Create a new document and add it to the main document part
    mainPart.Document = new Document();

    var destinationBody = mainPart.Document.AppendChild(new Body());
    var pageBreak = new Paragraph(new Run(new Break { Type = BreakValues.Page }));

    AppendDocument(file1, destinationBody);
    destinationBody.AppendChild(pageBreak); //comment this line, if you don't need page break
    AppendDocument(file2, destinationBody);

    mainPart.Document.Save();
}

This method will append old file contents to marged document

private static void AppendDocument(string filePath, Body destinationBody)
{
    using (var sourceDocument = WordprocessingDocument.Open(filePath, false))
    {
        // Get the main document part for the source document
        var mainPart = sourceDocument.MainDocumentPart;

        // Get all the paragraphs in the source document
        var paragraphs = mainPart.Document.Body.Elements<Paragraph>();

        // Add each paragraph to the destination document
        foreach (Paragraph paragraph in paragraphs)
        {
            destinationBody.AppendChild(paragraph.CloneNode(true));
        }
    }
}
Maruf
  • 41
  • 5
  • No I can not open merge document throwing error stating "Found a problem with contents". – Rams Dec 28 '22 at 04:37
  • Can you share your documents with me? which you are trying to marge – Maruf Dec 28 '22 at 04:38
  • Sorry Can't share those documents as they are client provided but the documents have headers and footers – Rams Dec 28 '22 at 05:01