Click here to Skip to main content
16,015,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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 phDatImages present in datagridview not exporting to file only text contents are generating into PDF file..e = 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);
          }
      }
Posted
Updated 3-Apr-15 1:32am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900