aspose利用word模板生成word、PDF
阿新 • • 發佈:2019-02-04
專案需求:表單歸檔時生成表單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);
}
aspose.word、aspose.pdf dll 下載地址
http://download.csdn.net/download/tt871911/10229509