varbinary(max) to pdf in vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • salonisona
    New Member
    • Oct 2009
    • 3

    #1

    varbinary(max) to pdf in vb.net

    Hello,

    I wanted code to convert the varbinry to pdf and save it on Clients machine.
    Can anyone help me ??
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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.

    Comment

    • ssnaik84
      New Member
      • Aug 2009
      • 149

      #3
      check iTextSharp Library tutorial.
      you just have to send binary data to PdfWriter object and set some properties.

      Comment

      • ssnaik84
        New Member
        • Aug 2009
        • 149

        #4
        wait.. If you are developing web application, you dont need even that.
        you just have to
        Code:
        Response.ContentType = "application/pdf"

        Comment

        • salonisona
          New Member
          • Oct 2009
          • 3

          #5
          First of all Thanks both of you.
          I got the solution.Actual ly 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 ByteArrayToPDFC onverter_Click( )

          Dim cn As ADODB.Connectio n
          Dim rs As ADODB.Recordset
          Dim mstream As ADODB.Stream

          Set cn = New ADODB.Connectio n
          cn.Open "Provider=SQLOL EDB;data Source=test;Ini tial Catalog=test;Us er Id=_test;Passwo rd=test"

          Set rs = New ADODB.Recordset
          rs.Open "Select document from tbltest where id = 13", cn, adOpenKeyset, adLockOptimisti c
          Set mstream = New ADODB.Stream
          mstream.Type = adTypeBinary
          mstream.Open
          mstream.Write rs.Fields("docu ment").Value
          mstream.SaveToF ile "c:\ByteTopdf.p df", adSaveCreateOve rWrite


          rs.Close
          cn.Close
          --------------------------------------------------------------------------------
          in c# for web application
          --------
          while (reader.Read())
          {
          bFileData = (byte[])reader["doc"];
          Response.Buffer Output = true;
          Response.Conten tType = "applicatio n/pdf";
          Response.AddHea der("Content-Disposition", "inline; filename=" + file.Name);
          //Response.AddHea der("content-disposition", "attachment;fil ename=Test.pdf" );
          Response.Binary Write(bFileData );
          Response.Flush( );
          Response.End();
          }

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

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

          Dim cn As New SqlConnection(M y.Settings.DocS toreConnectionS tring.ToString( ))
          Dim cmd As New SqlCommand("Sel ect DocData From Documents WHERE DocName = @DocName", cn)

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

          cn.Open()

          Using dr As SqlDataReader = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
          If dr.Read() Then
          Dim fs As IO.FileStream = New IO.FileStream(I O.Path.Combine( Me.FolderBrowse rDialog1.Select edPath, DocName), IO.FileMode.Cre ate)
          Dim b() As Byte = dr.Item("DocDat a")
          fs.Write(b, 0, b.Length)
          fs.Close()
          End If
          End Using 'dr

          cn.Close()

          Best Regards
          Sona

          Comment

          Working...