Connecting Tech Pros Worldwide Help | Site Map

varbinary(max) to pdf in vb.net

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: Oct 7 '09
Hello,

I wanted code to convert the varbinry to pdf and save it on Clients machine.
Can anyone help me ??
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: Oct 7 '09

re: varbinary(max) to pdf in vb.net


Well varbinary should return a byte array. Assuming it was populated correctly, you can save the byte array as a file and give it a .pdf extension.
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 119
#3: Oct 8 '09

re: varbinary(max) to pdf in vb.net


check iTextSharp Library tutorial.
you just have to send binary data to PdfWriter object and set some properties.
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 119
#4: Oct 8 '09

re: varbinary(max) to pdf in vb.net


wait.. If you are developing web application, you dont need even that.
you just have to
Expand|Select|Wrap|Line Numbers
  1. Response.ContentType = "application/pdf"
Newbie
 
Join Date: Oct 2009
Posts: 2
#5: Oct 8 '09

re: varbinary(max) to pdf in vb.net


First of all Thanks both of you.
I got the solution.Actually my senario was ,I had stored the pdf in SQL SEVER 2005 as varbinary(max) type and I wanted to retriew the pdf back.
Solution for this is as below :
In web we can use Response Content type as said by ssnaik .I have found the following solution.

in vb6
----------
Private Sub ByteArrayToPDFConverter_Click()

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream

Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB;data Source=test;Initial Catalog=test;User Id=_test;Password=test"

Set rs = New ADODB.Recordset
rs.Open "Select document from tbltest where id = 13", cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields("document").Value
mstream.SaveToFile "c:\ByteTopdf.pdf", adSaveCreateOverWrite


rs.Close
cn.Close
--------------------------------------------------------------------------------
in c# for web application
--------
while (reader.Read())
{
bFileData = (byte[])reader["doc"];
Response.BufferOutput = true;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name);
//Response.AddHeader("content-disposition", "attachment;filename=Test.pdf");
Response.BinaryWrite(bFileData);
Response.Flush();
Response.End();
}

and using Filestream
--------------------------------
bFileData = (byte[])reader["doc"];

using (FileStream stream = File.Open("C:\\file.pdf", FileMode.Create))
{
stream.Write(bFileData, 0, bFileData.Length);
}
-----------------
in vb.net
------------------
Private Sub SqlBlob2File(ByVal DocName As String)

Dim cn As New SqlConnection(My.Settings.DocStoreConnectionString .ToString())
Dim cmd As New SqlCommand("Select DocData From Documents WHERE DocName = @DocName", cn)

cmd.Parameters.AddWithValue("@DocName", DocName)

cn.Open()

Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read() Then
Dim fs As IO.FileStream = New IO.FileStream(IO.Path.Combine(Me.FolderBrowserDial og1.SelectedPath, DocName), IO.FileMode.Create)
Dim b() As Byte = dr.Item("DocData")
fs.Write(b, 0, b.Length)
fs.Close()
End If
End Using 'dr

cn.Close()

Best Regards
Sona
Reply