Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void button4_Click(object sender, EventArgs e)
- {
- //Creating iTextSharp Table from the DataTable data
- PdfPTable pdfTable = new PdfPTable(datagridview1.ColumnCount);
- pdfTable.DefaultCell.Padding = 3;
- pdfTable.WidthPercentage = 30;
- pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
- pdfTable.DefaultCell.BorderWidth = 1;
- //Adding Header row
- foreach (DataGridViewColumn column in datagridview1.Columns)
- {
- PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
- cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
- pdfTable.AddCell(cell);
- }
- if (pdfTable == null)
- {
- //Adding DataRow
- foreach (DataGridViewRow row in datagridview1.Rows)
- {
- foreach (DataGridViewCell cell in row.Cells)
- {
- pdfTable.AddCell(cell.Value.ToString());
- }
- }
- }
- //Exporting to PDF
- string folderPath = "C:\Users\maury\Desktop";
- if (!Directory.Exists(folderPath))
- {
- Directory.CreateDirectory(folderPath);
- }
- using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
- {
- Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
- PdfWriter.GetInstance(pdfDoc, stream);
- pdfDoc.Open();
- pdfDoc.Add(pdfTable);
- pdfDoc.Close();
- stream.Close();
- }
- }
- if (pdfTable == null)
- {
- //Adding DataRow
- foreach (DataGridViewRow row in datagridview1.Rows)
- {
- foreach (DataGridViewCell cell in row.Cells)
- {
- pdfTable.AddCell(cell.Value.ToString());
- }
- }
- }
Add Comment
Please, Sign In to add comment