We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Adjustable height of PdfGridRow

Hi,I have a PdfGrid with a column that may have lots of text. The amount of text is difficult to predict, that's why I'd like the rows of my PdfGrid to adjust their height accordingly. I have found that I need to calculate the row's height and set it programmatically. I have tried different approaches but nothing works: the rows' height is not correct.This is the code that I'm using: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 stylepdfGridHeader.Cells[0].Style = headerStyle;pdfGridHeader.Cells[1].Style = headerStyle;pdfGridHeader.Cells[2].Style = headerStyle;var font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);PdfGridCellStyle style = new PdfGridCellStyle();style.CellPadding = new PdfPaddings(0, 0, 0, 0); // 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(currentPos.Page, new RectangleF(0, currentPos.Bounds.Bottom + 10, pageSize.Width, pageSize.Height));This is the output. As you can see the height of the 3rd column is not set correctly:Thanks,Leszek

8 Replies

SK Surya Kumar Syncfusion Team July 21, 2017 09:42 AM UTC

Hi Leszek, 


Thank you for using Syncfusion products. 


We have analysed the code snippet which you have given, it is found that you have not assigned the font which you used to measure the string height to font of the PdfGridCellStyle, we have modified the code snippet which you have given in your last update and highlighted the change. 

Please find the code snippet below: 

           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"); 


Please let us know if you need any further information in this. 

Regards,
Surya Kumar  
 



LT Leszek Taratuta July 21, 2017 01:21 PM UTC

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




SK Surya Kumar Syncfusion Team July 24, 2017 09:16 AM UTC

Hi Leszek, 

We have tried to reproduce the issue which you have mentioned, with the below attached sample, but we are unable to reproduce the same. If you can still reproduce the issue please provide us the output document or modify the code snippet if there is any change in it, to analyse further and rectify this issue. 

Sample link: 



Please let us know if you need any further information. 

Regards, 
Surya Kumar 



LT Leszek Taratuta July 24, 2017 01:30 PM UTC

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




LT Leszek Taratuta July 24, 2017 01:33 PM UTC

ScreenshotIt seems the forum also has a bug: I'm not able to attach a screenshot (or I don't know how).Thanks,Leszek


SK Surya Kumar Syncfusion Team July 25, 2017 04:15 PM UTC

Hi Leszek, 
 
We are able to reproduce the behavior which you have mentioned. This behavior is caused due to the line count calculation, which will not return exact line count in all the cases.  
Please find the code snippet which you have used to calculate line count: 
//divide the text width and column width   
float lineCount = (size.Width / columnWidth) + 1;  
 
 
The contents inside every cell is word wrapped by default. So, if a word can’t accommodate within the line in a cell then it automatically moves to next line as shown in screenshot below: 
 
 
The extra space which is created due to word wrapping adds to the width of the string which we give inside the cell and the width of this extra space cannot be found. 
 
Due to this process, we cannot calculate accurate number of lines which can be accommodated within given column width and so we cannot find the exact height of the row. 
 
 
Can you please let us know the reason for this height calculation, with your exact requirement so that we can help you better. 
 
Please let us know if you need any further information. 
 
Regards, 
Surya Kumar 



LT Leszek Taratuta July 25, 2017 07:44 PM UTC

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



SK Surya Kumar Syncfusion Team July 26, 2017 12:01 PM UTC

Hi Leszek, 
 
A support incident to track the status of reported issue has been created under your account. Please log on to our support website to check for further updates.     
    
 
 
Regards, 
Surya Kumar 


Loader.
Up arrow icon