I found lots of examples of how to export DataGridView and DataTable to PDF using iText Sharp, which is an older version of iText7, but I would like to how we can do it using iText7 including Headers, Thanks in advance
Asked
Active
Viewed 1,597 times
0
-
There appears to be a fairly robust tutorial on the iText7 website… [iText 7: Jump-Start Tutorial for .NET](https://itextpdf.com/en/resources/books/itext-7-jump-start-tutorial-net) … Are you stuck at some point that the tutorial is missing? Your question is to broad and without more details and specifics will likely be closed. I recommend a visit to the [SO Help Center.](https://stackoverflow.com/help) – JohnG May 26 '20 at 01:14
1 Answers
0
Good Day, Hi :), you can use for & foreach and getting the data from selected DGV to the table... Please, see the code below:
private Table PDFTableFromDGV(DataGridView dgv, float [] cloumnwidth)
{
// Getting Rows & Columns Counts
int dgvrowcount = dgv.Rows.Count;
int dgvcolumncount = dgv.Columns.Count;
// Set The Table like new float [] {15f, 15f, 15f, 15f, 15f }
Table table = new Table(cloumnwidth);
table.SetWidth(iText.Layout.Properties.UnitValue.CreatePercentValue(100));
// Print The DGV Header To Table Header
for (int i = 0; i < dgvcolumncount; i++)
{
Cell headerCells = new Cell()
.SetBackgroundColor(iText.Kernel.Colors.ColorConstants.LIGHT_GRAY)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
//headerCells.SetNextRenderer(new RoundedCornersCellRenderer(headerCells));
var gteCell = headerCells.Add(new Paragraph(dgv.Columns[i].HeaderText));
table.AddHeaderCell(gteCell);
}
// Print The DGV Cells To Table Cells
for (int i = 0; i < dgvrowcount; i++)
{
for (int c = 0; c < dgvcolumncount; c++)
{
Cell cells = new Cell()
.SetBackgroundColor(iText.Kernel.Colors.ColorConstants.WHITE)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
var gteCell = cells.Add(new Paragraph(dgv.Rows[i].Cells[dgv.Columns[c].HeaderText].Value.ToString()));
table.AddCell(gteCell);
}
}
return table;
}

ALAALI
- 1
- 2