Advertisement
estevaorada

iTextSharp | DGV to FDP

Mar 30th, 2021
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. if (dgvFuncionarios.Rows.Count > 0)
  2.             {
  3.                 SaveFileDialog sfd = new SaveFileDialog();
  4.                 sfd.Filter = "PDF (*.pdf)|*.pdf";
  5.                 sfd.FileName = "Output.pdf";
  6.                 bool fileError = false;
  7.                 if (sfd.ShowDialog() == DialogResult.OK)
  8.                 {
  9.                     if (File.Exists(sfd.FileName))
  10.                     {
  11.                         try
  12.                         {
  13.                             File.Delete(sfd.FileName);
  14.                         }
  15.                         catch (IOException ex)
  16.                         {
  17.                             fileError = true;
  18.                             MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message);
  19.                         }
  20.                     }
  21.                     if (!fileError)
  22.                     {
  23.                         try
  24.                         {
  25.                             PdfPTable pdfTable = new PdfPTable(dgvFuncionarios.Columns.Count);
  26.                             pdfTable.DefaultCell.Padding = 3;
  27.                             pdfTable.WidthPercentage = 100;
  28.                             pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
  29.  
  30.                             foreach (DataGridViewColumn column in dgvFuncionarios.Columns)
  31.                             {
  32.                                 PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
  33.                                 pdfTable.AddCell(cell);
  34.                             }
  35.  
  36.                             foreach (DataGridViewRow row in dgvFuncionarios.Rows)
  37.                             {
  38.                                 foreach (DataGridViewCell cell in row.Cells)
  39.                                 {
  40.                                     pdfTable.AddCell(cell.Value.ToString());
  41.                                 }
  42.                             }
  43.  
  44.                             using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create))
  45.                             {
  46.                                 Document pdfDoc = new Document(PageSize.A4, 10f, 20f, 20f, 10f);
  47.                                 PdfWriter.GetInstance(pdfDoc, stream);
  48.                                 pdfDoc.Open();
  49.                                 pdfDoc.Add(pdfTable);
  50.                                 pdfDoc.Close();
  51.                                 stream.Close();
  52.                             }
  53.  
  54.                             MessageBox.Show("Data Exported Successfully !!!", "Info");
  55.                         }
  56.                         catch (Exception ex)
  57.                         {
  58.                             MessageBox.Show("Error :" + ex.Message);
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 MessageBox.Show("No Record To Export !!!", "Info");
  66.             }
  67.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement