开发者

ASP.NET View PDF in the webpage

开发者 https://www.devze.com 2023-01-01 13:11 出处:网络
开发者_JAVA技巧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
开发者_JAVA技巧

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消