protected void Page_Load(object sender, EventArgs e)
        {
            string procinstid = "";
            string host = "";
            string url = "";
            procinstid = Request.QueryString["procinstid"].ToString();
            host = Request.QueryString["host"].ToString();
            url = "http://" + host + "/PluginsFor/PrintPage/EngineeringChangeApplication/EngineeringChangeApplication.aspx?step=view&procinstid=" + procinstid;
            //Get the Url
            if (url.Trim() == string.Empty)
            {
                Logger.Log.Info("------URL地址为空");
            }
            //获取保存路径
            string outputFileName = "";
            string path = ConfigurationManager.AppSettings["UpLoadFilePath"];
            DateTime now = DateTime.Now;
            string time = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + now.Millisecond.ToString();
            string fileName = "工程更改" + time + ".pdf";
            string newfileName = path + fileName; //文件保存路径

            
            ////文件名
            outputFileName = System.IO.Path.Combine(path, fileName);
            EO.Pdf.HtmlToPdf.Options.HeaderHtmlFormat = "工程更改流程";
            EO.Pdf.HtmlToPdf.Options.FooterHtmlFormat = "华晨汽车集团企业流程管理系统";
            EO.Pdf.HtmlToPdf.Options.PageSize = EO.Pdf.PdfPageSizes.A5;
            EO.Pdf.HtmlToPdf.Options.NoLink = false;
            HtmlToPdf.ConvertUrl(url, outputFileName);


            PdfDocument doc = new PdfDocument(outputFileName);
            doc.Security.Disallow(PdfDocumentPermissions.Printing);
            AcmRender render = new AcmRender(doc);
            render.BeforeRenderPage += new AcmPageEventHandler(BeforeRenderPage);
            AcmContent content = new AcmContent();
            render.Render(content);
            doc.Save(outputFileName);


            Response.Redirect("/PluginsFor/PrintPage/DownLoadPDF/DownLoadPDF.aspx?filename=" + System.Web.HttpUtility.UrlEncode(newfileName.ToString()));//將檔案名稱傳送到另一個頁面
            Context.Response.Write("{success:'true',fileName:'" + newfileName.ToString() + "'}");
        }
        private void BeforeRenderPage(object sender, AcmPageEventArgs e)
        {
            EO.Pdf.Contents.PdfTextLayer textLayer = new EO.Pdf.Contents.PdfTextLayer();
            textLayer.Font = new EO.Pdf.Drawing.PdfFont("Arial", 30);
            textLayer.NonStrokingColor = Color.FromArgb(1, 150, 150, 150);
            textLayer.GfxMatrix.Rotate(45);
            EO.Pdf.Contents.PdfTextContent textContent = new EO.Pdf.Contents.PdfTextContent("华晨汽车集团".ToUpper());
            textContent.PositionMode = EO.Pdf.Contents.PdfTextPositionMode.Offset;
            textContent.Offset = new EO.Pdf.Drawing.PdfPoint(400, 150);
            textLayer.Contents.Add(textContent);
            e.Page.Contents.Add(textLayer);
        }

  

public partial class DownLoadPDF : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EmployeeEntity empEnt = null;
            Object user = HttpContext.Current.Session["LoginUser"];
            //if (user == null)
            //    return;
            //empEnt = (EmployeeEntity)user;
            try
            {
                string filePath = Request.QueryString["fileName"].ToString();
                //string filePath = "C:\\Users\\Administrator\\Desktop\\word\\研究院项目付款审批表20141227233018.docx";
                string fileName = string.Empty;
                //string filePath = string.Empty;
                //filePath = ConfigurationManager.AppSettings[id];
                fileName = filePath.Substring(filePath.LastIndexOf(@"/") + 1);
                //filePath = Server.MapPath(filePath);
                FileStream steam = File.Open(filePath, FileMode.Open, FileAccess.Read);
                byte[] data = new byte[steam.Length];
                int result = steam.Read(data, 0, data.Length);
                if (!File.Exists(filePath))
                    Response.Redirect("");
                if (data.Length <= 0)
                    data = new Byte[] { 13, 10 };
                int i = filePath.LastIndexOf('/');
                steam.Close();
                string UserAgent = Request.ServerVariables["http_user_agent"].ToLower();
                if (UserAgent.IndexOf("firefox") == -1)
                {//非火狐浏览器               
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName));
                }
                else
                {
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                }

                HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString());
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.BinaryWrite(data);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex.Message);
                Response.Redirect("/Web/Page/Download/FileNotFound.htm");
            }
        }
    }

  

posted on 2017-02-24 16:14  刘祥伟  阅读(3134)  评论(0编辑  收藏  举报