How to open PDF file in ASP.Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shekhaliya
    New Member
    • Oct 2006
    • 1

    How to open PDF file in ASP.Net

    I want to open particular PDF file in my Asp.net application. For that have store the path of PDF file in database. Now I want to open that file on one of the page in my application.
  • mohq82
    New Member
    • Nov 2006
    • 2

    #2
    Originally posted by shekhaliya
    I want to open particular PDF file in my Asp.net application. For that have store the path of PDF file in database. Now I want to open that file on one of the page in my application.

    try the following :

    System.Net.WebC lient client = new System.Net.WebC lient();
    Byte[] buffer = client.Download Data(path);

    if (buffer != null)
    {
    Response.Conten tType = "applicatio n/pdf";
    Response.AddHea der("content-length", buffer.Length.T oString());
    Response.Binary Write(buffer);
    }

    hope it helps.

    Comment

    • abbaky
      New Member
      • Nov 2006
      • 2

      #3
      Originally posted by shekhaliya
      I want to open particular PDF file in my Asp.net application. For that have store the path of PDF file in database. Now I want to open that file on one of the page in my application.

      Hi,

      If it is ASP .Net just add response.redire ct("The PDF file path") in the code

      Comment

      • jammoral
        New Member
        • Jul 2007
        • 1

        #4
        what if I am storing the pdf in a blob column in oracle? how do I assign the binary value to the response function?

        Comment

        • firstmalone
          New Member
          • Mar 2008
          • 1

          #5
          I did not see anyone actually answering this question.

          I thought if I provide my current solution, I might get some constructive feedback and perhaps some assistance.

          While the following ASP.NET 2.0 (2005 SP1) w/ VB.NET 2005 SP1 solution does provide a PDF rendering of the oracle stored blob PDF file, I'd like to display the PDF reader within my current page template - In Classic ASP I would do this by utilizing a report reader object.

          How do I do this in ASP.NET 2.0?

          If you have a sample from that language would work for me too as I can and do read, understand and develop in C#. (I do not currently have any paying customers who develop in C#, but I do use it.)

          [HTML]
          <%@ Page Language="vb" AutoEventWireup ="false" CodeBehind="PFD Viewer.aspx.vb" Inherits="Proje ct.PFDViewer" %>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head id="HTMLHead" runat="server"> <title></title></head><body></body>
          </html>
          [/HTML]
          Code:
          Imports System.Data.OracleClient
          
          Partial Public Class PFDViewer
              Inherits System.Web.UI.Page
          
              Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                  If IsNothing(Session("seUSERGUID")) Then
                      Me.Response.Redirect("~\Login.aspx", True)
                  Else
                      If Not IsPostBack Then
                          Dim Tracker As New clsWebUserTracker(New OracleClient.OracleConnection(ConfigurationManager.ConnectionStrings("AppSecASPNETConnectionString").ConnectionString))
                          Tracker.LogEvent(Request.ServerVariables("Remote_addr").ToString, Me.Page.Request.Url.AbsoluteUri.ToString, Session("seUserName"), Session("seUSERGUID"), Session("seEntityGUID"))
                      End If
                  End If
                  Dim orConn As New OracleConnection(Me.Session("seEntityConnectionString"))
                  Dim DM As New clsDocuments(orConn, Nothing)
                  If Me.Request("ByType") Then
                      DM.getDocumentFileByType(Me.Request("DocCallVal"))
                  Else
                      DM.getDocumentFileByGUID(Me.Request("DocCallVal"))
                  End If
                  Me.Title = DM.DocumentTitle & " - " & DM.DocumentType
                  If String.IsNullOrEmpty(DM.Filetype) Then
                      Response.Write("File Not Available")
                  ElseIf Not DM.Filetype.ToUpper = "PDF" Then
                      Response.Write("File Not Available")
                  Else
                      'Write the file directly to the HTTP output stream.
                      Dim SR As IO.Stream
                      SR = DM.FileBlob
                      Dim bw As IO.BinaryReader
                      Response.Clear()
                      bw = New IO.BinaryReader(SR)
                      Dim SendByteArray As Byte()
                      SendByteArray = bw.ReadBytes(bw.BaseStream.Length)
                      Response.ContentType = "application/pdf"
                      Response.AddHeader("Content-Type", "application/pdf")
                      Response.AddHeader("Content-Disposition", "inline")
                      Response.BinaryWrite(SendByteArray)
                      Response.End()
                  End If
              End Sub
          End Class

          Comment

          • amity2001
            New Member
            • Jan 2012
            • 1

            #6
            Very very useful...
            Thank you very much...

            Just translating & refining the code for C# users,

            all credit is yours...!


            Code:
                        StreamReader streamReader = new StreamReader(PDFDirectoryPath);
            
                        Stream stream = streamReader.BaseStream;
            
                        BinaryReader binaryReader = new BinaryReader(stream);
            
                        byte[] sendbyteArray = binaryReader.ReadBytes(Convert.ToInt32( binaryReader.BaseStream.Length));
            
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Type", "application/pdf");
                        Response.AddHeader("Content-Disposition", "inline");
                        Response.BinaryWrite(sendbyteArray);
                        Response.End();

            Comment

            Working...