1. 程式人生 > >ASP. NET MVC專案 使用iTextSharp將網頁程式碼生成PDF檔案

ASP. NET MVC專案 使用iTextSharp將網頁程式碼生成PDF檔案

        /// <summary>
        /// 獲取MVC檢視Html
        /// </summary>
        /// <param name="context">控制器上下文</param>
        /// <param name="viewName">檢視名稱</param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string GetViewHtml(ControllerContext context, string viewName)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = context.RouteData.GetRequiredString("action");
            using (var sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
                var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
                try
                {
                    viewResult.View.Render(viewContext, sw);
                }
                catch (Exception ex)
                {
                    throw;
                }

                return sw.GetStringBuilder().ToString();
            }
        }

        [NoAuthorization]
        public ActionResult IndexPdf(string Year, string month)
        {
            //獲取值班內容
            string monthContent = helper.GetRotaMonth(Year, month);
            //值班標題
            string title = "";
            if (month == "January")
            {
                title = Year + "年1月份週六值班安排";
            }
            else if (month == "February")
            {
                title = Year + "年2月份週六值班安排";
            }
            else if (month == "March")
            {
                title = Year + "年3月份週六值班安排";
            }
            else if (month == "April")
            {
                title = Year + "年4月份週六值班安排";
            }
            else if (month == "May")
            {
                title = Year + "年5月份週六值班安排";
            }
            else if (month == "June")
            {
                title = Year + "年6月份週六值班安排";
            }
            else if (month == "July")
            {
                title = Year + "年7月份週六值班安排";
            }
            else if (month == "August")
            {
                title = Year + "年8月份週六值班安排";
            }
            else if (month == "September")
            {
                title = Year + "年9月份週六值班安排";
            }
            else if (month == "October")
            {
                title = Year + "年10月份週六值班安排";
            }
            else if (month == "November")
            {
                title = Year + "年11月份週六值班安排";
            }
            else if (month == "December")
            {
                title = Year + "年12月份週六值班安排";
            }

            //從網址下載Html字串(方法一)
            //string inpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/Rota/September.html");
       //string htmlText = HtmlToPdfHelper.GetWebContent(inpath)//如果讀取檔案中的HTML程式碼呼叫此方法
            string htmlText = HtmlToPdfHelper.GetWebContent(monthContent);//這是呼叫的我獲取的資料庫中的內容
            //獲取MVC檢視Html字串(方法二)
            //string htmlText = GetViewHtml(ControllerContext, "EditIndex");            //如果直接呼叫檢視中的HTML調呼叫此方法,不建議用此方法,因為檢視中有資料會是動態生成的,這時會獲取不到報錯
//水印圖片路徑 //string picPath = Server.MapPath("~/PDFTemplate/TemplateImg/authentication-iocn.png"); //string picPath = Server.MapPath("~/Upload/Rota/authentication-iocn.png"); //html轉pdf並加上水印 //byte[] pdfFile = HtmlToPdfHelper.ConvertHtmlTextToPdf(htmlText, "", 100, 200, 100, 100); byte[] pdfFile = HtmlToPdfHelper.ConvertHtmlTextToPdf(htmlText); //輸出至客戶端 HtmlToPdfHelper.PdfDownload(pdfFile, title);//此處呼叫步驟6方法 return View(); }