I am new to iTextSharp, and want to convert my DataGridView to a PdfPtable.
When i write the table to PDF, it shows up nicely, except when the amount of columns is relatively large. Then it accomodates by making the column width smaller.
Instead, I would like to start on a new page, with more of the table shown, with headers.
Below is my code for the table:
PdfPTable table = new PdfPTable( view.Columns.Count );
float[] widths = new float[view.Columns.Count];
for ( int i = 0; i < view.Columns.Count; i++ ) {
widths[i] = view.Columns[i].Width;
}
table.SetWidths( widths );
table.HorizontalAlignment = 1; //Center
PdfPCell cell = null;
//Headers
foreach ( DataGridViewColumn c in view.Columns ) {
cell = new PdfPCell( new Phrase( new Chunk( c.HeaderText, _standardFont ) ) );
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell( cell );
}
//Rest of table
for ( int i = 1; i < view.Rows.Count; i++ ) {
for ( int j = 0; j < view.Columns.Count; j++ ) {
bool hasValue = view.Rows[i].Cells[j].Value == null ? false : true;
if ( hasValue ) {
cell = new PdfPCell( new Phrase( view.Rows[i].Cells[j].Value.ToString(), _standardFont ) );
} else {
cell = new PdfPCell( new Phrase( "", _standardFont ) );
}
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell( cell );
}
}
One solution would be to somehow know how many columns could be printed fullsize on a page, and then add a new page, with more of the grid displayed there, but i am unsure how to detect a new page. The problem here is that there seems to be no way of changing the number of columns of a PdfPtable, so I have to be able to calculate the effect in width when adding columns beforehand.
EDIT: I want to split the pages vertically