How to Open a PDF File in ASP.net

104 27
    • 1). Save the PDF file on the web server. For this example, a PDF file named "myFile.pdf" is saved to a directory named "mydomain.com/pdf_files."

    • 2). Set a path that points to the saved PDF file. The following syntax sets up the string variable:
      string myFile = @"/pdf_files/myFile.pdf";

    • 3). Instantiate the web client class and create a buffer to store the opened file information. Notice the file path from Step 1 is used to open the data path for the buffer.
      WebClient myWeb = new WebClient();
      Byte[] myBuff = myWeb.DownloadData(myFile);

    • 4). Set the response headers to tell the browser that the file is a PDF format.
      Response.ContentType = "application/pdf";
      Response.AddHeader("content-length",myBuff.Length.ToString());

    • 5). Write the buffer to the web browser screen. This opens the PDF file in the user's browser.
      Response.BinaryWrite(myBuff);

Source...

Leave A Reply

Your email address will not be published.