Hi Bello,
Check this example. Now please take its reference and correct your code.
For adding images in database i have used below article.
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Windows.Forms
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
BindDataGridView();
}
private void BindDataGridView()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Id,Name,Data FROM tblFiles WHERE ContentType = 'image/jpeg'";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
}
}
}
private void btnExport_Click(object sender, EventArgs e)
{
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 30;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;
//Adding Header row
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
pdfTable.AddCell(cell);
}
//Adding DataRow
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string id = row.Cells[0].Value.ToString();
pdfTable.AddCell(id);
string name = row.Cells[1].Value.ToString();
pdfTable.AddCell(name);
byte[] imageByte = (byte[])row.Cells[2].Value;
iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(imageByte);
pdfTable.AddCell(myImage);
}
//Exporting to PDF.
string folderPath = @"D:\\";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(folderPath + "DataGridViewImageExport.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
BindDataGridView()
End Sub
Private Sub BindDataGridView()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT Id,Name,Data FROM tblFiles WHERE ContentType = 'image/jpeg'"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
End Using
End Using
End Sub
Private Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
Dim pdfTable As PdfPTable = New PdfPTable(dataGridView1.ColumnCount)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 30
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
For Each column As DataGridViewColumn In dataGridView1.Columns
Dim cell As PdfPCell = New PdfPCell(New Phrase(column.HeaderText))
cell.BackgroundColor = New iTextSharp.text.BaseColor(240, 240, 240)
pdfTable.AddCell(cell)
Next
For Each row As DataGridViewRow In dataGridView1.Rows
Dim id As String = row.Cells(0).Value.ToString()
pdfTable.AddCell(id)
Dim name As String = row.Cells(1).Value.ToString()
pdfTable.AddCell(name)
Dim imageByte As Byte() = CType(row.Cells(2).Value, Byte())
Dim myImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageByte)
pdfTable.AddCell(myImage)
Next
Dim folderPath As String = "D:\\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As FileStream = New FileStream(folderPath & "DataGridViewImageExport.pdf", FileMode.Create)
Dim pdfDoc As Document = New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
pdfDoc.Add(pdfTable)
pdfDoc.Close()
stream.Close()
End Using
End Sub
Screenshots
Form
PDF