Shalini rajendran

Shalini rajendran

  • NA
  • 21
  • 10.5k

Images present in datagridview not exporting to file only te

Apr 3 2015 7:11 AM

Hi Everyone,

I have created simple Desktop app in that I trying to generate PDF file from Datagridview...when I click on ExportPDf button Pdf file is generation successfully but the issue is in that pdf whatever the images has present in datagridview that images are not generation into PDF only the text contents are Present in PDF file.

Does any one can tell me how to generate the PDF file along with images.

Here is my code:

private void btnexportPDF_Click(object sender, EventArgs e)
{
int ApplicationNameSize = 15;
int datesize = 12;
Document document = null;

try
{
SaveFileDialog savefiledg = new SaveFileDialog();
savefiledg.Filter = "All Files | *.* ";

if (savefiledg.ShowDialog() == DialogResult.OK)
{
string path = savefiledg.FileName;

document = new Document(PageSize.A4, 3, 3, 10, 5);

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path + ".pdf", FileMode.Create));
document.Open();

// Creates a phrase to hold the application name at the left hand side of the header.
Phrase phApplicationName = new Phrase("Sri Lakshmi Finance,Hosur-560068", FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL));


// Creates a phrase to show the current date at the right hand side of the header.
Phrase phDate = new Phrase(DateTime.Now.ToLongDateString(), FontFactory.GetFont("Arial", datesize, iTextSharp.text.Font.NORMAL));


document.Add(phApplicationName);
document.Add(phDate);

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("D:\\logo.JPG");
document.Add(img);

iTextSharp.text.Font font5= iTextSharp.text.FontFactory.GetFont(FontFactory.TIMES_ROMAN, 5);
iTextSharp.text.Font font6 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 6);
//float[] columnDefinitionSize = { 2.5f, 7.0f,6.6f, 8.6f, 6.6f, 5.0f, 4.5f, 7.0f, 6.3f, 7.0f, 3.5f, 6.0f, };

PdfPTable table = null;
table = new PdfPTable(dataGridView1.Columns.Count);
table.WidthPercentage = 100;

PdfPCell cell = null;
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText,font6)));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
table.AddCell(cell);
}

if (dataGridView1.Rows.Count > 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
PdfPCell[] objcell = new PdfPCell[dataGridView1.Columns.Count];

for (int j = 0; j < dataGridView1.Columns.Count - 0; j++)
{
cell = new PdfPCell(new Phrase(dataGridView1.Rows[i].Cells[j].Value.ToString(), font5));
cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
cell.VerticalAlignment = PdfPCell.ALIGN_LEFT;
cell.Padding = PdfPCell.ALIGN_LEFT;
objcell[j] = cell;
}

PdfPRow newrow = new PdfPRow(objcell);
table.Rows.Add(newrow);
}
}

document.Add(table);
MessageBox.Show("PDF Generated Successfully");
document.Close();
}
else
{
//Error
}
}
catch (FileLoadException fle)
{
MessageBox.Show(fle.Message);
MessageBox.Show("Error in PDF Generation", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}