项目需求:表单归档时生成表单word、PDF文件
先上效果图
1、word模板(部分)书签
2、生成结果图
开始上代码
Dictionary dictSource = new Dictionary();
dictSource = FlowCommon.FlowFun.NCRDocDic(FlowModel); //获取文字替换数据
string docFIlePath = SetNCRDocImg(FlowModel, dictSource);//替换签名图片及文字内容,返回生成的word文件路径
string path = Server.MapPath("~" + docFIlePath);
FlowCommon.NPIOHelper.WordToPDF(path + "NCR.docx", path, "NCR");//根据word生成PDF
文字标签数据
public static Dictionary NCRDocDic(Model.View_NCRFlowList FlowModel)
{
Dictionary dictSource = new Dictionary();
try
{
dictSource.Add("zlgly_psjb", FlowModel.zlgly_psjb);//评审级别
dictSource.Add("zlgly_bh", FlowModel.zlgly_bh);//编号
dictSource.Add("jcy_mcth", FlowModel.jcy_mcth);//不合格品名称及图号
dictSource.Add("jcy_fsdw", FlowModel.jcy_fsdw);//发生单位
dictSource.Add("jcy_cpbh", FlowModel.jcy_cpbh);//产品编号
dictSource.Add("jcy_fslb", FlowModel.jcy_fslb);//不合格品发生类别
...
}
catch (Exception e)
{
Log.Write(tabLog, e.ToString());
dictSource = null;
}
return dictSource;
}
///
/// NCR文档图片设置
///
///
///
///
public string SetNCRDocImg(Model.View_NCRFlowList FlowModel, Dictionary dictSource, bool IsGD = true)
{
string docFIlePath = "";
string templateFile = Server.MapPath("~/Templates/NCR.docx");
Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);//读取模板
DocumentBuilder builder = new DocumentBuilder(doc);
//检查员
string jcy = FlowModel.jcy_jcy;
DataTable dt_jcy = FlowCommon.FlowFun.GetUserSignImg(jcy, "1"); //签名图片获取
ReplaceImg(dt_jcy, "jcy_jcy", dictSource, builder);
//质量管理员--判断等级
string zlgly = FlowModel.zlgly_zlgly;
DataTable dt_zlgly = FlowCommon.FlowFun.GetUserSignImg(zlgly, "2");
ReplaceImg(dt_zlgly, "zlgly_zlgly", dictSource, builder);
//使用文本方式替换
foreach (string name in dictSource.Keys)
{
doc.Range.Replace(name, dictSource[name].Replace("\r\n", ""), true, true);
}
string SaveFile = "";
if (IsGD) //默认归档操作,归档与打印文件保存路径不同
docFIlePath = "/upload/NCRFiles/" + FlowModel.zlgly_bh + "/";
else
docFIlePath = "/upload/PrintFiles/" + DateTime.Now.Ticks.ToString() + "/";
SaveFile = Server.MapPath("~" + docFIlePath);
if (!Directory.Exists(SaveFile))
{
Directory.CreateDirectory(SaveFile);
}
doc.Save(SaveFile + "NCR.docx");
return docFIlePath;
}
///
/// 签名图片替换
///
/// 数据源
/// 标签名称
///
///
/// 是否为多图片
private void ReplaceImg(DataTable dt, string name, Dictionary dictSource, DocumentBuilder builder, bool IsMul = false)
{
if (dt != null && dt.Rows.Count > 0) //查到数据了
{
if (!IsMul)
{
string url = dt.Rows[0]["signurl"].ToString();
//PHOTO_jcy
string imgurl = Server.MapPath("~" + url);
FileInfo fileInfo = new FileInfo(imgurl);
if (fileInfo.Exists)
{
builder.MoveToBookmark("PHOTO_" + name);
builder.InsertImage(imgurl, RelativeHorizontalPosition.Default, -5, RelativeVerticalPosition.Margin, 0, 45, 18, WrapType.None);
dictSource[name] = ""; //签名图片存在时清空文字标签内容
}
}
else //客户需求有多人签名,在同一个表单框中,最多现在三个签名,这里根据自己的业务需求来
{
int i = 1;
foreach (DataRow dr in dt.Rows)
{
if (i > 3) break;
string url = dr["signurl"].ToString();
//PHOTO_jcy
string imgurl = Server.MapPath("~" + url);
FileInfo fileInfo = new FileInfo(imgurl);
if (fileInfo.Exists)
{
builder.MoveToBookmark("PHOTO_" + name + i.ToString());
builder.InsertImage(imgurl, RelativeHorizontalPosition.Default, -5, RelativeVerticalPosition.Margin, 0, 45, 18, WrapType.Square);
dictSource[name] = ""; //
}
i++;
}
}
}
}
///
/// Word转成Pdf
///
/// 要转换的文档的路径
/// 转换成Pdf的保存路径
/// 转换成html的文件名字
public static void WordToPDF(string path, string savePath, string wordFileName)
{
Aspose.Words.Document d = new Aspose.Words.Document(path);
d.Save(savePath + wordFileName + ".pdf", SaveFormat.Pdf);
}