Guest User

Untitled

a guest
Apr 22nd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. private void button4_Click(object sender, EventArgs e)
  2. {
  3. //Creating iTextSharp Table from the DataTable data
  4. PdfPTable pdfTable = new PdfPTable(datagridview1.ColumnCount);
  5. pdfTable.DefaultCell.Padding = 3;
  6. pdfTable.WidthPercentage = 30;
  7. pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
  8. pdfTable.DefaultCell.BorderWidth = 1;
  9.  
  10. //Adding Header row
  11. foreach (DataGridViewColumn column in datagridview1.Columns)
  12. {
  13. PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
  14. cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
  15. pdfTable.AddCell(cell);
  16. }
  17.  
  18. if (pdfTable == null)
  19. {
  20. //Adding DataRow
  21. foreach (DataGridViewRow row in datagridview1.Rows)
  22. {
  23. foreach (DataGridViewCell cell in row.Cells)
  24. {
  25. pdfTable.AddCell(cell.Value.ToString());
  26. }
  27. }
  28. }
  29.  
  30. //Exporting to PDF
  31. string folderPath = "C:\Users\maury\Desktop";
  32. if (!Directory.Exists(folderPath))
  33. {
  34. Directory.CreateDirectory(folderPath);
  35. }
  36. using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
  37. {
  38. Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
  39. PdfWriter.GetInstance(pdfDoc, stream);
  40. pdfDoc.Open();
  41. pdfDoc.Add(pdfTable);
  42. pdfDoc.Close();
  43. stream.Close();
  44. }
  45. }
  46.  
  47. if (pdfTable == null)
  48. {
  49. //Adding DataRow
  50. foreach (DataGridViewRow row in datagridview1.Rows)
  51. {
  52. foreach (DataGridViewCell cell in row.Cells)
  53. {
  54. pdfTable.AddCell(cell.Value.ToString());
  55. }
  56. }
  57. }
Add Comment
Please, Sign In to add comment