asposeword.dll通过word模板生成word、PDF
效果圖
1、word模板(部分)書簽?
?
2、生成結果圖
開始上代碼
?
?Dictionary<string, string> dictSource = new Dictionary<string, string>();
?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<string, string> NCRDocDic(Model.View_NCRFlowList FlowModel)
? ? ? ? {
? ? ? ? ? ? Dictionary<string, string> dictSource = new Dictionary<string, string>();
? ? ? ? ? ? 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;
? ? ? ? }
/// <summary>
? ? ? ? /// NCR文檔圖片設置
? ? ? ? /// </summary>
? ? ? ? /// <param name="FlowModel"></param>
? ? ? ? /// <param name="dictSource"></param>
? ? ? ? /// <param name="IsGD"></param>
? ? ? ? public string SetNCRDocImg(Model.View_NCRFlowList FlowModel, Dictionary<string, string> 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;
? ? ? ? }
?/// <summary>
? ? ? ? /// 簽名圖片替換
? ? ? ? /// </summary>
? ? ? ? /// <param name="dt">數據源</param>
? ? ? ? /// <param name="name">標簽名稱</param>
? ? ? ? /// <param name="dictSource"></param>
? ? ? ? /// <param name="builder"></param>
? ? ? ? /// <param name="IsMul">是否為多圖片</param>
? ? ? ? private void ReplaceImg(DataTable dt, string name, Dictionary<string, string> 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++;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
/// <summary>
? ? ? ? /// Word轉成Pdf
? ? ? ? /// </summary>
? ? ? ? /// <param name="path">要轉換的文檔的路徑</param>
? ? ? ? /// <param name="savePath">轉換成Pdf的保存路徑</param>
?/// <param name="wordFileName">轉換成html的文件名字</param>?
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);
? ? ? ? }
總結
以上是生活随笔為你收集整理的asposeword.dll通过word模板生成word、PDF的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 源码阅读 AtomicInteger
- 下一篇: springCloud - 第6篇 -