Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenXmlPowerTools DocumentBuilder merging documents on a seperate page

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

    }

}
like image 845
Timur Avatar asked Oct 16 '25 07:10

Timur


1 Answers

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)
    };
like image 189
Gianmaria Dalla Torre Avatar answered Oct 18 '25 22:10

Gianmaria Dalla Torre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!