In that case, you don't need iTextSharp. Create a generic handler (.ashx file), retrieve the binary data from the database in the ProcessRequest Method and send it to the client.
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim pdfID As Integer = Convert.ToInt32(context.Request.QueryString("pdfID"))
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Const SQL As String = "SELECT [pdfData] FROM [pdfTable] WHERE [pdfID] = @pdfID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@pdfID", pdfID)
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader
If myReader.Read Then
context.Response.ContentType = "application/pdf"
context.Response.BinaryWrite(myReader("pdfData"))
context.Response.AddHeader("Content-Disposition", "attachment; filename=download.pdf"
End If
myReader.Close()
myConnection.Close()
End Using
End Sub
hans_v
All-Star
35986 Points
6550 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 11:36 AM|LINK
In that case, you don't need iTextSharp. Create a generic handler (.ashx file), retrieve the binary data from the database in the ProcessRequest Method and send it to the client.
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim pdfID As Integer = Convert.ToInt32(context.Request.QueryString("pdfID")) Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Const SQL As String = "SELECT [pdfData] FROM [pdfTable] WHERE [pdfID] = @pdfID" Dim myCommand As New SqlCommand(SQL, myConnection) myCommand.Parameters.AddWithValue("@pdfID", pdfID) myConnection.Open() Dim myReader As SqlDataReader = myCommand.ExecuteReader If myReader.Read Then context.Response.ContentType = "application/pdf" context.Response.BinaryWrite(myReader("pdfData")) context.Response.AddHeader("Content-Disposition", "attachment; filename=download.pdf" End If myReader.Close() myConnection.Close() End Using End SubTo retrieve the pdf, call the ashx file like:
<asp:HyperLink ID="HyperLink1" runat="server" navigetUrl="~/pdf.ashx?pdfID=1" Text="Download" />