⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displaypdf.aspx.cs

📁 可以把HTML转化成PDF
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Threading;

namespace HtmlToPDF.PDF
{
	/// <summary>
	/// Summary description for DisplayPDF.
	/// </summary>
	public class DisplayPDF : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label LabelMsg;
		protected System.Web.UI.WebControls.HyperLink HL_Adobe;
		protected System.Web.UI.WebControls.Button BT_PDFReady;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if ( Request.Params["Page"] != null )
			{
				PDF.PDFGenerator pGen = new PDF.PDFGenerator(Server.MapPath("/"));
				string sFile = pGen.RunWeb(Request.Params["Url"].ToString());

				Response.Redirect("DisplayPDF.aspx?File=" + sFile);
			}

			if ( Request.Params["File"] != null )
			{
				bool bRet = false;
				int iTimeout = 0;
				while ( bRet == false )
				{
					bRet = CheckIfFileExist(Request.Params["File"].ToString());
					Thread.Sleep(1000);
					iTimeout++;
					if ( iTimeout == 10 )
						break;
				}

				if ( bRet == true )
				{
					Response.ClearContent();
					Response.ClearHeaders();
					Response.ContentType = "Application/pdf";
					try 
					{ 
						Response.WriteFile( MapPath( "" + Request.Params["File"].ToString() ) ); 
						Response.Flush();
						Response.Close();
					}
					catch 
					{ 
						Response.ClearContent();												
					}
					
				}
				else
				{
					if ( Request.Params["Msg"] != null )
					{
						LabelMsg.Text = Request.Params["Msg"].ToString();
					}
				}
			}
		}

		private bool CheckIfFileExist(string sFile)
		{
			string sRoot = Server.MapPath("/");
			string sDrive = "" + sRoot[0];

			if ( File.Exists(MapPath( "" + Request.Params["File"].ToString() )) == false )
			{			
				
				return(false);
			}

			return(true);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.BT_PDFReady.Click += new System.EventHandler(this.BT_PDFReady_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void BT_PDFReady_Click(object sender, System.EventArgs e)
		{
			string sMsg = "Please wait a few seconds and press the button again";

			if( Request.Params["Msg"] != null )
				sMsg = "This report takes longer than normal, wait 10 seconds and press the button again";

			if ( Request.Params["File"] != null )
			{
				Response.Redirect("DisplayPDF.aspx?File=" + Request.Params["File"].ToString() + "&Msg=" + sMsg);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -