4

I am trying to merge 4 word documents and force content of each document starts at new page. But instead of appending text from each document to a separate page, it adds text from all docs to one page. Like this:enter image description here

How can i fix it? This is the code:

 public class HomeController : Controller
{ 
    public void DocMerger()
    {
        var source1 = Server.MapPath(Url.Content("~/App_Data/1.docx")); //source 1
        var source2 = Server.MapPath(Url.Content("~/App_Data/2.docx")); //source 2
        var source3 = Server.MapPath(Url.Content("~/App_Data/3.docx")); //source 3
        var source4 = Server.MapPath(Url.Content("~/App_Data/4.docx")); //source 4
        var merged =  Server.MapPath(Url.Content("~/App_Data/merged.docx")); //merged

        var f1 = new FileInfo(source1);
        var f2 = new FileInfo(source2);
        var f3 = new FileInfo(source3);
        var f4 = new FileInfo(source4);

        //Use DocumentBuilder and merge the files
        var sources = new List<OpenXmlPowerTools.Source>()
        {
            new Source(new WmlDocument(f1.FullName),false),
              new Source(new WmlDocument(f2.FullName),false),
                new Source(new WmlDocument(f3.FullName),false),
                  new Source(new WmlDocument(f4.FullName),false)
        };
        var mergedDocument = DocumentBuilder.BuildDocument(sources);
        mergedDocument.SaveAs(merged); //save merged data as merged.docx

    }

}
Xan-Kun Clark-Davis
  • 2,664
  • 2
  • 27
  • 38
Timur
  • 131
  • 1
  • 3
  • 13
  • You're not really using `openxml-sdk` though, `OpenXmlPowerTools` is another library, you should edit your tags – Alexander Derck Jan 05 '16 at 07:34
  • I was able to fix it by using DocX library. This is example for one of four documents: `using (DocX document = DocX.Load(source1)) {Novacode.Paragraph p2 = document.InsertParagraph(); p2.InsertPageBreakAfterSelf(); document.Save(); }` – Timur Jan 05 '16 at 14:16

2 Answers2

3

This is the constructor that you use for "Source":

public Source(WordprocessingDocument source, bool keepSections)

Then you just have to change false with true on keepSections value:

var sources = new List<OpenXmlPowerTools.Source>()
    {
        new Source(new WmlDocument(f1.FullName),true),
        new Source(new WmlDocument(f2.FullName),true),
        new Source(new WmlDocument(f3.FullName),true),
        new Source(new WmlDocument(f4.FullName),true)
    };
  • This works only if the documents being merged have sections. "Simple" documents just merge as is. – aleyush Sep 25 '20 at 19:32
0

Using OpenXmlPowerTools.NetStandard version 4.6.0, it works so far with this code, which is based on the answers above, but a little simplyfied.

using OpenXmlPowerTools;

public class Example {
    public static void MergeWithBreaks() {
        string doc_A       = "doc_A.docx";
        string doc_B       = "doc_B.docx";
        string destination = "Merged_Result.docx";

        var sources = new List<Source> {
            new(new WmlDocument(doc_A), true),
            new(new WmlDocument(doc_B), true)
        };

        var outputPath = destination;

        DocumentBuilder.BuildDocument(sources, outputPath);
    }
}

I am still trying to do a memory based version.

Xan-Kun Clark-Davis
  • 2,664
  • 2
  • 27
  • 38