I am having total 9 columns, all are having bytes data. Out of these 9 columns, 7 columns have IMAGE datatype & 1 column have VARBINARY(MAX). These 7 columns have bytes for JPG and 1 column have bytes for PDF. Finally I need to merge all in single pdf.
I have function where I am sending the bytes in using Dictionary(Of Byte(), String) = New Dictionary(Of Byte(), String)().
On the line Dim image = iTextSharp.text.Image.GetInstance(data) I am getting following error as "The byte array is not a recognized imageformat."
I am already displaying that bytes to image on page and it's displaying successfully, but iTextsharp is not reading JPG Bytes.
below is my code:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports iTextSharp.text
Imports System.IO
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.Drawing
Partial Class Default_
Inherits System.Web.UI.Page
Dim mergedpdf As Byte() = Nothing, newbytes As Byte() = Nothing, schoolname As String = "", _
constr As String = ConfigurationManager.ConnectionStrings("dConstr").ConnectionString
Dim _items As List(Of KeyValuePair(Of Byte(), String)) = New List(Of KeyValuePair(Of Byte(), String))()
Dim My_dict1 As Dictionary(Of Byte(), String) = New Dictionary(Of Byte(), String)()
Protected Sub Button1_Click(sender As Object, e As EventArgs)
MergeandDownload(123)
End Sub
Sub MergeandDownload(ByVal regid As Integer)
Using conn As New SqlConnection(constr)
Try
Using cmd As New SqlDataAdapter("SELECT b.schoolname,(case when a.[singlepdf] is null then '' else a.[singlepdf] end) AS singlepdf2,(case when a.[building] is null then '' else a.[building] end) AS building,a.[fire],a.[pollution],a.[chemical],a.[municipality],a.[traffic],a.[bylaws],a.[building_sketch],(case when a.[singlepdf] is null then '' else a.[singlepdf] end) AS singlepdf FROM [reg_documents] a inner join registration_master b on b.reg_schoolid = a.reg_schoolid where a.reg_schoolid = @regid", conn)
cmd.SelectCommand.Parameters.AddWithValue("@regid", regid)
conn.Open()
Using dsset As New DataSet()
cmd.Fill(dsset, "tabs")
If (dsset.Tables(0).Rows.Count > 0) Then
schoolname = dsset.Tables(0).Rows(0)("schoolname").ToString()
If (dsset.Tables(0).Rows(0)("building").ToString() <> "") Then
My_dict1.Add(System.Text.Encoding.ASCII.GetBytes(dsset.Tables(0).Rows(0)("building").ToString()), "jpg")
End If
If (dsset.Tables(0).Rows(0)("singlepdf").ToString() <> "") Then
My_dict1.Add(System.Text.Encoding.ASCII.GetBytes(dsset.Tables(0).Rows(0)("singlepdf").ToString()), "pdf")
'listoffilebytes.Add(System.Text.Encoding.ASCII.GetBytes(dsset.Tables(0).Rows(0)("singlepdf").ToString()))
'_items.Add(New KeyValuePair(Of Byte(), String)(System.Text.Encoding.ASCII.GetBytes(dsset.Tables(0).Rows(0)("singlepdf").ToString()), "pdf"))
End If
mergedpdf = concatAndAddContent()
End If
End Using
End Using
Catch ex As Exception
Response.Write(ex.ToString())
Finally
If ConnectionState.Open Then
conn.Close()
End If
End Try
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Public Function concatAndAddContent() As Byte()
Dim mergedpdf2 As Byte() = Nothing
Dim doc As Document = New Document()
doc.SetPageSize(PageSize.A4)
Dim ms = New System.IO.MemoryStream()
If True Then
Dim pdf As PdfCopy = New PdfCopy(doc, ms)
doc.Open()
If (My_dict1.Count > 0) Then
Dim img_ As System.Drawing.Image = Nothing
Dim data As Byte() = Nothing
For Each ele1 As KeyValuePair(Of Byte(), String) In My_dict1
doc.NewPage()
Dim imageDocument As Document = Nothing
Dim imageDocumentWriter As PdfWriter = Nothing
Select Case ele1.Value.ToString()
Case "bmp", "gif", "jpg", "png"
'img_ = BytestoImage() 'File.ReadAllBytes()
data = CType(ele1.Key.ToArray(), Byte()) 'ImagetoBytes(img_)
imageDocument = New Document()
Using imageMS = New MemoryStream()
imageDocumentWriter = PdfWriter.GetInstance(imageDocument, imageMS)
imageDocument.Open()
If imageDocument.NewPage() Then
Dim image = iTextSharp.text.Image.GetInstance(data)
image.Alignment = Element.ALIGN_CENTER
image.ScaleToFit(doc.PageSize.Width - 10, doc.PageSize.Height - 10)
If Not imageDocument.Add(image) Then
Throw New Exception("Unable to add image to page!")
End If
imageDocument.Close()
imageDocumentWriter.Close()
Dim imageDocumentReader As PdfReader = New PdfReader(ele1.Key.ToArray())
Dim page = pdf.GetImportedPage(imageDocumentReader, 1)
pdf.AddPage(page)
imageDocumentReader.Close()
End If
End Using
Case "pdf"
Dim reader = New PdfReader(data)
For i As Integer = 0 To reader.NumberOfPages - 1
pdf.AddPage(pdf.GetImportedPage(reader, i + 1))
Next
pdf.FreeReader(reader)
reader.Close()
Case Else
End Select
Next
End If
If doc.IsOpen() Then doc.Close()
Return ms.ToArray()
End If
End Function
End Class