|
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);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.
Al is a Microsoft ASP.NET MVP
Blog