Advertisement
Guest User

Untitled

a guest
May 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /// <summary>
  2. /// Print to pdf format
  3. /// </summary>
  4. private void btn_Print_Click(object sender, EventArgs e)
  5. {
  6. try
  7. {
  8. Random rnd = new Random();
  9. int rndNum = rnd.Next(1, 100);
  10. string fileName = Utils.tableName + "" + rndNum + ".pdf";
  11. string fileHeader = "DANH SÁCH " + Utils.tableName + "";
  12.  
  13. // export pdf to print
  14. adapter.Fill(db);
  15. dgvShowInf.DataSource = db;
  16. dgvShowInf.Columns["rowguid"].Visible = false;
  17. Utils.ExportDataTableToPdf(db, @".\document\" + fileName+"", fileHeader);
  18. System.Diagnostics.Process.Start(@".\document\" + fileName);
  19. WindowState = FormWindowState.Minimized;
  20. }
  21. catch (Exception ex)
  22. {
  23. MessageBox.Show(ex.Message, "In danh sách thất bại");
  24. }
  25. }
  26.  
  27. // Export dtb to pdf for print
  28. public static void ExportDataTableToPdf(DataTable dtblTable, String strPdfPath, string strHeader)
  29. {
  30. System.IO.FileStream fs = new FileStream(strPdfPath, FileMode.Create, FileAccess.Write, FileShare.None);
  31. Document document = new Document();
  32. document.SetPageSize(PageSize.A4);
  33. PdfWriter writer = PdfWriter.GetInstance(document, fs);
  34. document.Open();
  35.  
  36. //Report Header
  37. BaseFont bfntHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
  38. Font fntHead = new Font(bfntHead, 16, 1, BaseColor.BLACK);
  39. Paragraph prgHeading = new Paragraph();
  40. prgHeading.Alignment = Element.ALIGN_CENTER;
  41. prgHeading.Add(new Chunk(strHeader.ToUpper(), fntHead));
  42. document.Add(prgHeading);
  43.  
  44. //Author
  45. Paragraph prgAuthor = new Paragraph();
  46. BaseFont btnAuthor = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
  47. Font fntAuthor = new Font(btnAuthor, 8, 2, BaseColor.GRAY);
  48. prgAuthor.Alignment = Element.ALIGN_RIGHT;
  49.  
  50. prgAuthor.Add(new Chunk("Khoa : " + Utils.khoa.ToString() + "", fntAuthor));
  51. prgAuthor.Add(new Chunk("\nNgày : " + DateTime.Now.ToShortDateString(), fntAuthor));
  52. document.Add(prgAuthor);
  53.  
  54. ////Add a line seperation
  55. Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
  56. document.Add(p);
  57.  
  58. //Add line break
  59. document.Add(new Chunk("\n", fntHead));
  60.  
  61. //Write the table
  62. PdfPTable table = new PdfPTable(dtblTable.Columns.Count);
  63.  
  64. //Table header
  65. BaseFont btnColumnHeader = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
  66. Font fntColumnHeader = new Font(btnColumnHeader, 10, 1, BaseColor.WHITE);
  67. for (int i = 0; i < dtblTable.Columns.Count; i++)
  68. {
  69. PdfPCell cell = new PdfPCell();
  70. cell.BackgroundColor = BaseColor.GRAY;
  71. cell.AddElement(new Chunk(dtblTable.Columns[i].ColumnName.ToUpper(), fntColumnHeader));
  72. table.AddCell(cell);
  73. }
  74. //table Data
  75. for (int i = 0; i < dtblTable.Rows.Count; i++)
  76. {
  77. for (int j = 0; j < dtblTable.Columns.Count; j++)
  78. {
  79. table.AddCell(dtblTable.Rows[i][j].ToString());
  80. }
  81. }
  82. document.Add(table);
  83. document.Close();
  84. writer.Close();
  85. fs.Close();
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement