使用 Aspose.Words合并word
using Aspose.Words;
string CombineWord(string id, params string[] pages)//合并模板doc
{
string path1 = "", path2 = "", path3 = "", path4 = "";
string[] tpls = { "商品混凝土出厂合格证", "开盘检定", "生产任务通知单", "生产用混凝土配合比调整通知单" };
path1 = GetTplPath(tpls[0],out path1);
path2 = GetTplPath(tpls[1], out path2);
path3 = GetTplPath(tpls[2], out path2);
path4 = GetTplPath(tpls[3], out path4);
if (string.IsNullOrEmpty(path1))
{
CommanFunction.WriteLog2("printHeBing.log", "合并打印," + tpls[0] + "的模板不存在");
return null;
}
else if (string.IsNullOrEmpty(path2))
{
CommanFunction.WriteLog2("printHeBing.log", "合并打印," + tpls[1] + "的模板不存在");
return null;
}
else if (string.IsNullOrEmpty(path3))
{
CommanFunction.WriteLog2("printHeBing.log", "合并打印," + tpls[3] + "的模板不存在");
return null;
}
else if (string.IsNullOrEmpty(path4))
{
CommanFunction.WriteLog2("printHeBing.log", "合并打印," + tpls[3] + "的模板不存在");
return null;
}
else {
path1 = Server.MapPath(path1);
path2 = Server.MapPath(path2);
path3 = Server.MapPath(path3);
path4 = Server.MapPath(path4);
var dstDoc = AppendDocument(new Document(path1), new Document(path2));
dstDoc = AppendDocument(dstDoc, new Document(path3));
dstDoc = AppendDocument(dstDoc, new Document(path4));
var dir = Server.MapPath("/tempData");
var savePath = Path.Combine(dir, id + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc");
if (File.Exists(savePath))
File.Delete(savePath);
dstDoc.Save(savePath);
return savePath;
}
}
Document AppendDocument(Document dstDoc, Document srcDoc, SectionStart sectionStart = SectionStart.NewPage)
{
srcDoc.FirstSection.PageSetup.SectionStart = sectionStart;
dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
ParagraphFormat pf = dstDoc.Styles.DefaultParagraphFormat;
pf.ClearFormatting();
return dstDoc;
}
使用 Aspose.Pdf合并PDF
using Aspose.Pdf;
string CombinePDF(string projectNum, params string[] pages)//合并模板
{
string path1 = "", path2 = "";
if (pages.Length > 0)
{
path1 = Server.MapPath(pages[0]);
CommanFunction.WriteLog2("printHeBing.log", "合并打印,CombinePDF,path1" + path1);
Document pdfdoc1 = new Document(path1);
for (int i = 1; i < pages.Length; i++)
{
path2 = Server.MapPath(pages[i]);
Document pdfdoc2 = new Document(path2);
pdfdoc1.Pages.Add(pdfdoc2.Pages);
}
var dir = Server.MapPath("/tempData");
var savePath = Path.Combine(dir, projectNum + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
CommanFunction.WriteLog2("printHeBing.log", "合并打印,CombinePDF,savePath:" + savePath);
if (File.Exists(savePath))
File.Delete(savePath);
pdfdoc1.Save(savePath);
return savePath;
}
else
{
msg = "没有PDF可合并";
return null;
}
}