gpt4 book ai didi

c# - OpenXmlPowerTools DocumentBuilder 在单独的页面上合并文档

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:57 25 4
gpt4 key购买 nike

我正在尝试合并 4 个 word 文档并强制每个文档的内容从新页面开始。但是,它不是将每个文档的文本附加到单独的页面,而是将所有文档的文本添加到一个页面。像这样:enter image description here

我该如何解决?这是代码:

 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

}

}

最佳答案

这是您用于“源”的构造函数:

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)
};

关于c# - OpenXmlPowerTools DocumentBuilder 在单独的页面上合并文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34600564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com