PdfGrid row moves to another page

In some scenarios second PdfGrid is moving to a next page leaving lots of empty space on a current page.

In code below I found it happens when number of rows for secondGrid is between 33-37 (inclusive).

First we create a root grid that holds all the complex content with 1 row and 1 column.

Than we create hosting grid where rows are added as needed. In our case we add 2 rows to hold 2 grids one after the other. When second row has to expand to fit second grid that has row count between 33-37, it seems to decide to start from a new page. 

Similar issue reported was supposed to be fixed https://www.syncfusion.com/forums/165868/split-row-across-page

Could you help in forcing both grids to go one after the other?

I think UpdadeRowHeight has no effect on this, but left it to resemble our code base.

Start with SecondGridMovesToSecondPage().


        public void SecondGridMovesToSecondPage()

        {

            using (PdfDocument pdfDocument = new PdfDocument())

            {

                const float pageMargins = 17;


                var section = pdfDocument.Sections.Add();

                var pdfPage = section.Pages.Add();

                section.PageSettings.SetMargins(pageMargins);

                pdfDocument.PageSettings.SetMargins(pageMargins);


                // Create root grid

                PdfGrid rootGrid = new PdfGrid();

                var col = rootGrid.Columns.Add();

                col.Width = PdfPageSize.A4.Width - 2 * pageMargins;

                var row = rootGrid.Rows.Add();

                var cell = row.Cells[0];

                cell.Style.Borders.All = PdfPens.Transparent;


                // Create grid hosting 2 grids with data

                PdfGrid hostGrid = new PdfGrid();

                hostGrid.Columns.Add();

                cell.Value = hostGrid;


                // Create first grid

                PdfGrid firstGrid = new PdfGrid();

                hostGrid.Rows.Add();

                hostGrid.Rows[0].Cells[0].Value = firstGrid;


                string text = "";

                var colWidth = col.Width / 2;

                firstGrid.Columns.Add().Width = colWidth;

                firstGrid.Columns.Add().Width = colWidth;

                for (int i = 0; i < 5; i++)

                {

                    var r = firstGrid.Rows.Add();

                    r.Height = 20;

                    text = $"Field name {i}";

                    firstGrid.Rows[i].Cells[0].Value = text;

                    UpdateRowHeight(text, r, colWidth);


                    text = $"Value with some text {i}";

                    firstGrid.Rows[i].Cells[1].Value = text;

                    UpdateRowHeight(text, r, colWidth);

                }


                // Create second grid

                PdfGrid secondGrid = new PdfGrid();

                hostGrid.Rows.Add();

                hostGrid.Rows[1].Cells[0].Value = secondGrid;

                var col1Width = col.Width / 4;

                var col2Width = (col.Width - col1Width) / 3;

                var col3Width = col2Width;

                var col4Width = col2Width;

                secondGrid.Columns.Add().Width = col1Width;

                secondGrid.Columns.Add().Width = col2Width;

                secondGrid.Columns.Add().Width = col3Width;

                secondGrid.Columns.Add().Width = col4Width;


                // Add header row

                var hr = secondGrid.Rows.Add();

                var headerFont = new PdfTrueTypeFont(new Font("Arial", 10), FontStyle.Bold, 10, true, true);

                text = $"Header field name";

                secondGrid.Rows[0].Cells[0].Style.Font = headerFont;

                secondGrid.Rows[0].Cells[0].Value = text;

                UpdateRowHeight(text, hr, col1Width, headerFont);


                text = $"Value with some text";

                secondGrid.Rows[0].Cells[1].Style.Font = headerFont;

                secondGrid.Rows[0].Cells[1].Value = text;

                UpdateRowHeight(text, hr, col2Width, headerFont);


                text = $"Value with some text ";

                secondGrid.Rows[0].Cells[2].Style.Font = headerFont;

                secondGrid.Rows[0].Cells[2].Value = text;

                UpdateRowHeight(text, hr, col3Width, headerFont);


                text = $"Value with some text longer than the column width";

                //text = $"Value with some text ";

                secondGrid.Rows[0].Cells[3].Style.Font = headerFont;

                secondGrid.Rows[0].Cells[3].Value = text;

                UpdateRowHeight(text, hr, col4Width, headerFont);


                // NOTE: with number of rows between 33-37 secondGrid starts from a new page. In other cases it sticks to the firstGrid correctly.

                for (int i = 1; i < 33; i++)

                {

                    var r = secondGrid.Rows.Add();

                    r.Height = 20;

                    text = $"Field name {i}";

                    secondGrid.Rows[i].Cells[0].Value = text;

                    UpdateRowHeight(text, r, colWidth);


                    text = $"Value with some text {i}";

                    secondGrid.Rows[i].Cells[1].Value = text;

                    UpdateRowHeight(text, r, colWidth);


                    text = $"Value with some text {i}";

                    secondGrid.Rows[i].Cells[2].Value = text;

                    UpdateRowHeight(text, r, colWidth);


                    text = $"Value with some text {i}";

                    secondGrid.Rows[i].Cells[3].Value = text;

                    UpdateRowHeight(text, r, colWidth);

                }


                // Paginate and draw

                var layoutFormat = new PdfLayoutFormat {Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate};

                DrawPageNumbers(pdfPage, pdfDocument, pageMargins, pageMargins);

                rootGrid.Draw(pdfPage, new PointF(0, 0), layoutFormat);


                //Save the document

                string fileName = $"File{DateTime.Now.Ticks}.pdf";

                pdfDocument.Save(fileName);

                //Close the document

                pdfDocument.Close(true);

                System.Diagnostics.Process.Start(fileName);

            }

        }


        private void UpdateRowHeight(string text, PdfGridRow row, float columnWidth, PdfTrueTypeFont font = null)

        {

            font = font ?? new PdfTrueTypeFont(new Font("Arial", 11));

            //measure the string height

            SizeF size = font.MeasureString(text);


            //divide the text width and column width

            float lineCount = size.Width / columnWidth + 1;


            float heightNeeded = (size.Height + 1) * lineCount;


            if (row.Height < heightNeeded)

            {

                row.Height = heightNeeded;

            }

        }


        private void DrawPageNumbers(PdfPage page, PdfDocument document, float leftPageMargin, float rightPageMargin)

        {

            PdfFont font = new PdfTrueTypeFont(new Font("Arial", 10));

            PdfBrush brush = PdfBrushes.Black;

            float top = 10;

            float height = 40;

            RectangleF bounds = new RectangleF(0, top, page.GetClientSize().Width - rightPageMargin - leftPageMargin, height);

            PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds)

            {

                Y = bounds.Y // Base ctor of PdfPageTemplateElement doesn't honor Y from bounds. Is this a bug?

            };

            // Create page number field.

            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);


            // Create page count field.

            PdfPageCountField count = new PdfPageCountField(font, brush);


            // Add the fields in composite fields.

            PdfCompositeField pageNumberField = new PdfCompositeField(font, brush, "{0} of {1}", pageNumber, count)

            {

                Bounds = footer.Bounds,

                StringFormat = new PdfStringFormat(PdfTextAlignment.Right)

            };


            pageNumberField.Draw(footer.Graphics, new PointF(0, top));


            //Add the footer template at the bottom.

            document.Template.Bottom = footer;

        }





11 Replies 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team January 21, 2022 03:57 PM UTC

Hi umlprog, 
 
We were able to reproduce the reported issue with provided code sample on our end. Currently, we are analysing on this and we will update the further details on January 25th 2022. 
 
Regards, 
Gowthamraj K 



GK Gowthamraj Kumar Syncfusion Team January 25, 2022 02:30 PM UTC

Hi umlprog, 

Sorry for the inconvenience caused. 
 
Still, we are validating this reported issue with high priority on our end, we will provide further analysis details on January 28, 2022. 

Also, we were able to achieve your requirement in single layer nested grid, we suspect that the reported behavior may be based on creating multilayer nested grid. 

Kindly use the following code for reference, 
public void SecondGridMovesToSecondPage() 
 
        { 
 
            using (PdfDocument pdfDocument = new PdfDocument()) 
 
            { 
 
                const float pageMargins = 17; 
 
 
 
                var section = pdfDocument.Sections.Add(); 
 
                var pdfPage = section.Pages.Add(); 
 
                section.PageSettings.SetMargins(pageMargins); 
 
                pdfDocument.PageSettings.SetMargins(pageMargins); 
 
 
 
                //// Create root grid 
 
                //PdfGrid rootGrid = new PdfGrid(); 
 
                //var col = rootGrid.Columns.Add(); 
 
                //col.Width = PdfPageSize.A4.Width - 2 * pageMargins; 
 
                //var row = rootGrid.Rows.Add(); 
 
                //var cell = row.Cells[0]; 
 
                //cell.Style.Borders.All = PdfPens.Transparent; 
 
 
 
                // Create grid hosting 2 grids with data 
 
                PdfGrid hostGrid = new PdfGrid(); 
 
                var col = hostGrid.Columns.Add(); 
                col.Width = PdfPageSize.A4.Width - 2 * pageMargins; 
 
                //cell.Value = hostGrid; 
 
 
 
                // Create first grid 
 
                PdfGrid firstGrid = new PdfGrid(); 
 
                hostGrid.Rows.Add(); 
 
                hostGrid.Rows[0].Cells[0].Value = firstGrid; 
 
 
 
                string text = ""; 
 
                var colWidth = col.Width / 2; 
 
                firstGrid.Columns.Add().Width = colWidth; 
 
               firstGrid.Columns.Add().Width = colWidth; 
 
                for (int i = 0; i < 5; i++) 
 
                { 
 
                    var r = firstGrid.Rows.Add(); 
 
                    r.Height = 20; 
 
                    text = $"Field name {i}"; 
 
                    firstGrid.Rows[i].Cells[0].Value = text; 
 
                    UpdateRowHeight(text, r, colWidth); 
 
 
 
                    text = $"Value with some text {i}"; 
 
                    firstGrid.Rows[i].Cells[1].Value = text; 
 
                    UpdateRowHeight(text, r, colWidth); 
 
                } 
 
 
 
                // Create second grid 
 
                PdfGrid secondGrid = new PdfGrid(); 
 
                hostGrid.Rows.Add(); 
 
                hostGrid.Rows[1].Cells[0].Value = secondGrid; 
 
                var col1Width = col.Width / 4; 
 
                var col2Width = (col.Width - col1Width) / 3; 
 
                var col3Width = col2Width; 
 
                var col4Width = col2Width; 
 
                secondGrid.Columns.Add().Width = col1Width; 
 
                secondGrid.Columns.Add().Width = col2Width; 
 
                secondGrid.Columns.Add().Width = col3Width; 
 
                secondGrid.Columns.Add().Width = col4Width; 
 
 
 
                // Add header row 
 
                var hr = secondGrid.Rows.Add(); 
 
                var headerFont = new PdfTrueTypeFont(new Font("Arial", 10), FontStyle.Bold, 10, true, true); 
 
                text = $"Header field name"; 
 
                secondGrid.Rows[0].Cells[0].Style.Font = headerFont; 
 
                secondGrid.Rows[0].Cells[0].Value = text; 
 
                UpdateRowHeight(text, hr, col1Width, headerFont); 
 
 
 
                text = $"Value with some text"; 
 
                secondGrid.Rows[0].Cells[1].Style.Font = headerFont; 
 
                secondGrid.Rows[0].Cells[1].Value = text; 
 
                UpdateRowHeight(text, hr, col2Width, headerFont); 
 
 
 
                text = $"Value with some text "; 
 
                secondGrid.Rows[0].Cells[2].Style.Font = headerFont; 
 
                secondGrid.Rows[0].Cells[2].Value = text; 
 
                UpdateRowHeight(text, hr, col3Width, headerFont); 
 
 
 
                text = $"Value with some text longer than the column width"; 
 
                //text = $"Value with some text "; 
 
                secondGrid.Rows[0].Cells[3].Style.Font = headerFont; 
 
                secondGrid.Rows[0].Cells[3].Value = text; 
 
                UpdateRowHeight(text, hr, col4Width, headerFont); 
 
 
 
                // NOTE: with number of rows between 33-37 secondGrid starts from a new page. In other cases it sticks to the firstGrid correctly. 
 
                for (int i = 1; i < 33; i++) 
 
                { 
 
                    var r = secondGrid.Rows.Add(); 
 
                    r.Height = 20; 
 
                    text = $"Field name {i}"; 
 
                    secondGrid.Rows[i].Cells[0].Value = text; 
 
                    UpdateRowHeight(text, r, colWidth); 
 
 
 
                    text = $"Value with some text {i}"; 
 
                    secondGrid.Rows[i].Cells[1].Value = text; 
 
                    UpdateRowHeight(text, r, colWidth); 
 
 
 
                    text = $"Value with some text {i}"; 
 
                    secondGrid.Rows[i].Cells[2].Value = text; 
 
                    UpdateRowHeight(text, r, colWidth); 
 
 
 
                    text = $"Value with some text {i}"; 
 
                    secondGrid.Rows[i].Cells[3].Value = text; 
 
                    UpdateRowHeight(text, r, colWidth); 
 
                } 
 
 
 
                // Paginate and draw 
 
                var layoutFormat = new PdfLayoutFormat { Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate }; 
 
                DrawPageNumbers(pdfPage, pdfDocument, pageMargins, pageMargins); 
 
                hostGrid.Draw(pdfPage, new PointF(0, 0), layoutFormat); 
 
 
 
                //Save the document 
 
                string fileName = $"File{DateTime.Now.Ticks}.pdf"; 
 
                pdfDocument.Save(fileName); 
 
                //Close the document 
 
                pdfDocument.Close(true); 
 
                System.Diagnostics.Process.Start(fileName); 
 
            } 
 
        } 

Regards, 
Gowthamraj K 



UM umlprog January 25, 2022 10:10 PM UTC

Thanks for the update. Unfortunately we have no control over root/host grid. It's a general layout algorithm in our code base that reads whatever user has created in the designer. 



SV Surya Venkatesan Syncfusion Team replied to umlprog January 27, 2022 11:48 AM UTC

Hi umlprog,


Currently, we are analyzing your queries and we will update the further details on January 28th 2022.


Regards,

Surya V



SV Surya Venkatesan Syncfusion Team January 28, 2022 01:34 PM UTC

Hi umlprog,


We have confirmed that the issue with “Page overlapping issue occurs when drawing a nested PDF grid with added multi number of rows” is a defect. The fix for this issue will be available in our weekly NuGet release on February 8, 2022.


Kindly use the below feedback link to track the status of the reported bug,

https://www.syncfusion.com/feedback/32261/page-overlapping-issue-occurs-when-drawing-a-nested-pdf-grid


Regards,

Surya V



UM umlprog February 8, 2022 10:08 AM UTC

I tried 19.4.0.50 and it doesn't work.

Second grid still starts from the new page.





GK Gowthamraj Kumar Syncfusion Team February 8, 2022 10:28 AM UTC

Hi umlprog,  

Sorry for the inconvenienced caused. 
  
Due to technical glitch, we were unable to included the fix in our today weekly NuGet release. We will include the fix for this issue in our upcoming weekly NuGet release on February 15, 2022 without any further delay. 

However, we have fixed the reported issue with “Page overlapping issue occurs when drawing a nested PDF grid with added multi number of rows” and created patch for this fix in this version v19.4.0.48. Please find the download link below,  
  
Recommended approach - exe will perform automatic configuration    
Please find the patch setup from below location:    

Advanced approach – use only if you have specific needs and can directly replace existing assemblies for your build environment    
Please find the patch assemblies alone from below location:    


You can track the status of the bug using the below feedback link,    

Assembly Version: 19.4.0.48 
Installation Directions :     
This patch should replace the files “Syncfusion.Pdf.Base.dll” under the following folder.    
$system drive:\ Files\Syncfusion\Essential Studio\19.4.0.48\precompiledassemblies\19.4.0.48\4.6     
Eg : $system drive:\Program Files\Syncfusion\Essential Studio\19.4.0.48\precompiledassemblies\19.4.0.48\4.0    
   
To automatically run the Assembly Manager, please check the Run assembly manager checkbox option while installing the patch. If this option is unchecked, the patch will replace the assemblies in precompiled assemblies’ folder only. Then, you will have to manually copy and paste them to the preferred location or you will have to run the Syncfusion Assembly Manager application (available from the Syncfusion Dashboard, installed as a shortcut in the Application menu) to re-install assemblies.   
 
Note :    
To change how you receive bug fixes, ask your license management portal admin to change your project’s patch delivery mode.   
  
Disclaimer :    
Please note that we have created this patch for version 19.4.0.48 specifically to resolve the following issue(s) reported in this/the ticket(s). 172143 
  
If you have received other patches for the same version for other products, please apply all patches in the order received.   
 
Regards, 
Gowthamraj K 



UM umlprog February 8, 2022 03:31 PM UTC

Could you also include fix for https://www.syncfusion.com/forums/172317/pdfgridcell-columnspan-cell-width-wider-than-expected?reply=S2CNqf

I think it's a bug and we need it working right. Thanks.



GK Gowthamraj Kumar Syncfusion Team February 9, 2022 11:34 AM UTC

Hi umlprog,

Thank you for your update. We have updated the details in this forum. https://www.syncfusion.com/forums/172317/pdfgridcell-columnspan-cell-width-wider-than-expected 
 
Please let us know if you need any further assistance in this. 

Regards,
 
Gowthamraj K 



GK Gowthamraj Kumar Syncfusion Team February 16, 2022 02:16 PM UTC

Hi umlprog, 
 
Thank you for your confirmation and we are glad to know that your problem has been solved. Please let us know if you need any further assistance in this. 
 
Regards, 
Gowthamraj K 



UM umlprog February 16, 2022 04:27 PM UTC

I confirmed that Syncfusion.Pdf.WinForms 19.4.0.52 fixed this issue.

Thanks!


Marked as answer
Loader.
Up arrow icon