PdfDocument document = new PdfDocument();
PdfPage currentPage = document.Pages.Add();
// Get page client size.
SizeF pageSize = currentPage.GetClientSize();
//Create a PdfGrid.
PdfGrid grid = new PdfGrid();
float columnWidth = 165;
// this is the width of the 3rd column
//Add three columns.
grid.Columns.Add(3);
grid.Columns[2].Width = columnWidth;
//Add header.
grid.Headers.Add(1);
PdfGridRow pdfGridHeader = grid.Headers[0];
pdfGridHeader.Cells[0].Value = "Employee ID";
pdfGridHeader.Cells[1].Value = "Employee Name";
pdfGridHeader.Cells[2].Value = "Salary";
PdfGridCellStyle headerStyle = new PdfGridCellStyle();
headerStyle.CellPadding = new PdfPaddings(0, 0, 0, 0);
headerStyle.BackgroundBrush = PdfBrushes.Gainsboro;
//Apply style
pdfGridHeader.Cells[0].Style = headerStyle;
pdfGridHeader.Cells[1].Style = headerStyle;
pdfGridHeader.Cells[2].Style = headerStyle;
var font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
PdfGridCellStyle style = new PdfGridCellStyle();
style.CellPadding = new PdfPaddings(0, 0, 0, 0);
//Assign the font which used to measure the text size to Grid cell
style.Font = font;
// no paddings for claritystyle.
// Font = font;
for (int i = 1; i <= 50; ++i)
{
string str = "$" + i * 100 + " this is a vely long test to check if the text in this cell wraps properly in the table here.";
//measure the string height
SizeF size = font.MeasureString(str);
//divide the text width and column width
float lineCount = (size.Width / columnWidth) + 1;
//Add rows.
PdfGridRow row = grid.Rows.Add();
row.Height = (size.Height + 1) * lineCount;
row.Cells[0].Value = "E01" + i;
row.Cells[1].Value = "John #" + i;
row.Cells[2].Value = str;
row.Cells[0].Style = style;
row.Cells[1].Style = style;
row.Cells[2].Style = style;
}
//Draw the PdfGrid.
var pos = grid.Draw(currentPage, new RectangleF(0, 10, pageSize.Width, pageSize.Height));
document.Save("Output5.pdf"); |
Thanks Kumar for your response. Actually, if you look closer to the code I sent, there is a line style.Font = font; just after style.CellPadding = new PdfPaddings(0, 0, 0, 0); The formatting got lost when I edited the question (typo) but all code is there.
Are you able to reproduce the problem? I would send a screenshot, but it seems I'm not able to upload images to the forum and copy and paste simply does not work.
Leszek
Thank you Surya for your response. It's very easy to replicate the issue. Just change the following line in your code:
float columnWidth = 165;
with this one:
float columnWidth = 205;
You will see that the first nine rows have incorrect height with an unwanted spacing at the bottom (please, find the attachment with a screenshot).
Leszek
//divide the text width and column width
float lineCount = (size.Width / columnWidth) + 1;
|
Thanks Surya for your response.
The line of code float lineCount = (size.Width / columnWidth) + 1; is a desperate effort to set the proper height of the table rows. Without that all the rows would have the same height which is incorrect for the first nine rows. The lineCount should be around 2 for the first nine rows and 3 for the rest. Unfortunately, it does not work this way and I don't see why not. That's why it looks to me like a bug in the Syncfusion PDF framework.
The idea of calculating the lineCount comes from the Syncfusion post: https://www.syncfusion.com/forums/119192/pdf-grid-row-height-auto-adjust
Leszek