Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Print to pdf format
- /// </summary>
- private void btn_Print_Click(object sender, EventArgs e)
- {
- try
- {
- Random rnd = new Random();
- int rndNum = rnd.Next(1, 100);
- string fileName = Utils.tableName + "" + rndNum + ".pdf";
- string fileHeader = "DANH SÁCH " + Utils.tableName + "";
- // export pdf to print
- adapter.Fill(db);
- dgvShowInf.DataSource = db;
- dgvShowInf.Columns["rowguid"].Visible = false;
- Utils.ExportDataTableToPdf(db, @".\document\" + fileName+"", fileHeader);
- System.Diagnostics.Process.Start(@".\document\" + fileName);
- WindowState = FormWindowState.Minimized;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "In danh sách thất bại");
- }
- }
- // Export dtb to pdf for print
- public static void ExportDataTableToPdf(DataTable dtblTable, String strPdfPath, string strHeader)
- {
- System.IO.FileStream fs = new FileStream(strPdfPath, FileMode.Create, FileAccess.Write, FileShare.None);
- Document document = new Document();
- document.SetPageSize(PageSize.A4);
- PdfWriter writer = PdfWriter.GetInstance(document, fs);
- document.Open();
- //Report Header
- BaseFont bfntHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
- Font fntHead = new Font(bfntHead, 16, 1, BaseColor.BLACK);
- Paragraph prgHeading = new Paragraph();
- prgHeading.Alignment = Element.ALIGN_CENTER;
- prgHeading.Add(new Chunk(strHeader.ToUpper(), fntHead));
- document.Add(prgHeading);
- //Author
- Paragraph prgAuthor = new Paragraph();
- BaseFont btnAuthor = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
- Font fntAuthor = new Font(btnAuthor, 8, 2, BaseColor.GRAY);
- prgAuthor.Alignment = Element.ALIGN_RIGHT;
- prgAuthor.Add(new Chunk("Khoa : " + Utils.khoa.ToString() + "", fntAuthor));
- prgAuthor.Add(new Chunk("\nNgày : " + DateTime.Now.ToShortDateString(), fntAuthor));
- document.Add(prgAuthor);
- ////Add a line seperation
- Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
- document.Add(p);
- //Add line break
- document.Add(new Chunk("\n", fntHead));
- //Write the table
- PdfPTable table = new PdfPTable(dtblTable.Columns.Count);
- //Table header
- BaseFont btnColumnHeader = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
- Font fntColumnHeader = new Font(btnColumnHeader, 10, 1, BaseColor.WHITE);
- for (int i = 0; i < dtblTable.Columns.Count; i++)
- {
- PdfPCell cell = new PdfPCell();
- cell.BackgroundColor = BaseColor.GRAY;
- cell.AddElement(new Chunk(dtblTable.Columns[i].ColumnName.ToUpper(), fntColumnHeader));
- table.AddCell(cell);
- }
- //table Data
- for (int i = 0; i < dtblTable.Rows.Count; i++)
- {
- for (int j = 0; j < dtblTable.Columns.Count; j++)
- {
- table.AddCell(dtblTable.Rows[i][j].ToString());
- }
- }
- document.Add(table);
- document.Close();
- writer.Close();
- fs.Close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement