List<datagridviewcolumn> listVisible = new List<datagridviewcolumn>(); foreach (DataGridViewColumn col in dataGridView1.Columns) { if (col.Visible) listVisible.Add(col); } PdfPTable pdfTable = new PdfPTable(listVisible.Count); pdfTable.DefaultCell.Padding = 2; pdfTable.WidthPercentage = 100; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1; //Adding Header row for (int i = 0; i < listVisible.Count; i++) { PdfPCell cell = new PdfPCell(new Phrase(listVisible[i].HeaderText)); pdfTable.AddCell(cell); } //Adding DataRow for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { for (int j = 0; j < listVisible.Count; j++) { try { pdfTable.AddCell( dataGridView1.Rows[i].Cells[listVisible[j].Name].Value.ToString()); } catch { } } } SaveFileDialog svg = new SaveFileDialog(); svg.ShowDialog(); using (FileStream stream = new FileStream(svg.FileName + ".pdf", FileMode.Create)) { Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); MessageBox.Show("PDF Created Successfully"); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)