ASP.NET View PDF in the webpage
I am trying to put a pdf document in a a web page. But instead of viewing this in the web page, when this page is called it ask's if we want to save/download an then open this in Adobe with the printer dialog open.
Javascript which calls this page load function
window.open("http://localhost:1843/PrintDocument.aspx?DocumentId="+id+"&Year="+year+"&Location="+loc);
ASPX CODE BEHIND , Page load function:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "Application/pdf";
string yr = Request.QueryString["Year"].ToString();
string Dyr = Convert.ToDateTime(yr).Year.ToString();
string Location = Request.QueryString["Location"].ToString();
string DocumentID = Request.QueryString["DocumentId"].ToString();
string PDFFile = string.Format(@"\\abc\def\pqr\{0}\{1}\{2}.pdf", Dyr, Location.Substring(0, 4), Location.Substring(4, 4));
//Response.AddHeader("content-disposition", "attachment; filename=document.pdf");
Response.WriteFile(PDFFile);
Response.End();
}
}
There are several possible versions.
For example you can use the easiest way, like
Response.Clear();
string filePath = ur file path on the server
Response.contentType = "application/pdf";
Response.WriteFile(filePath);
It was already discussed HERE. You can find other options also. Also check HERE and HERE
Also you can use custom control : PDF Control
I was testing this code on firefox and it was missing the PDF plugin. Once I added this plugin it started working with my code. So when ever you are working with PDF on firefox , please make sure that you have PDF plugin.
精彩评论