我正在尝试合并4个单词文档,并且每个文档的强制内容都从新页面开始。但是,它不是将每个文档中的文本附加到单独的页面,而是将所有文档中的文本添加到一个页面。 像这样:
我该如何解决? 这是代码:
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
}
}
答案 0 :(得分:1)
这是用于“源”的构造函数:
public Source(WordprocessingDocument source, bool keepSections)
然后,您只需要在keepSections值上将false更改为true:
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)
};