Nested pdfgrid does not expand hosting cell

When we create nested grid inside a cell, that cell does not autoexpand.

Why there is no "Autogrow" or similar property on a cell? 

How can we force cell to accomodate for the inner grid content?



public void NestedGridClips()

        {

            using (PdfDocument pdfDocument = new PdfDocument())

            {

                //Create the page

                var section = pdfDocument.Sections.Add();

                var pdfPage = section.Pages.Add();

                PdfGrid parentGrid = new PdfGrid();

                FillGrid(parentGrid, "parentGrid", 3, 3, 40);

                PdfGrid pGrid = new PdfGrid();

                parentGrid.Rows[0].Cells[0].Value = pGrid; // Set nested grid to the first cell


                FillGrid(pGrid, "pGrid", 5, 4, rowHeight: 20);

                parentGrid.Draw(pdfPage, PointF.Empty, new PdfGridLayoutFormat {Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate});

void FillGrid(PdfGrid grid, string name, int iMax, int jMax, int? rowHeight = null)

        {

            for (int j = 0; j < jMax; j++)

            {

                grid.Columns.Add();

                grid.Columns[j].Width = 50 + 5 * j;

            }


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

            {

                var row = grid.Rows.Add();

                if (rowHeight.HasValue)

                {

                    row.Height = rowHeight.Value;

                }


                for (int j = 0; j < jMax; j++)

                {

                    var cell = grid.Rows[i].Cells[j];

                    cell.Value = $"grid: {name}. i:{i},j:{j}";

                }

            }

        }


                //Save the document

                pdfDocument.Save("NestedTable.pdf");

                //Close the document

                pdfDocument.Close(true);

                //This will open the PDF file so, the result will be seen in default PDF viewer

                System.Diagnostics.Process.Start("NestedTable.pdf");

            }

        }


5 Replies

GK Gowthamraj Kumar Syncfusion Team December 27, 2021 03:30 PM UTC

Hi umlprog, 
 
We have analyzed your code snippet, the reported issue occurs due to width and height value are set to grid cell. So that nested grid rows are clipped. We can overcome this issue by removing the grid width and height code.  Please find the below code snippet,  
static void FillGrid(PdfGrid grid, string name, int iMax, int jMax, int? rowHeight = null) 
 
        { 
 
            for (int j = 0; j < jMax; j++) 
            { 
 
                grid.Columns.Add(); 
 
                //grid.Columns[j].Width = 50 + 5 * j; 
 
            } 
 
 
 
            for (int i = 0; i < iMax; i++) 
 
            { 
 
                var row = grid.Rows.Add(); 
                for (int j = 0; j < jMax; j++) 
               { 
                    var cell = grid.Rows[i].Cells[j]; 
 
                    cell.Value = $"grid: {name}. i:{i},j:{j}"; 
                } 
 
            } 
 
        } 
 
Please find the output document from below link, 
 
Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 



UM umlprog December 28, 2021 11:03 AM UTC

Our code base is rather generic and always sets minimal row height and columns' width before outputting contents to cells.

We need a way for cells to grow to fit content unknown a priori.

We tried calculating grid height after it's laid out (CurrentHeight is a property of an object wrapping PdfGrid):

public float CurrentHeight

{

get

{

//Create a new document.

using (PdfDocument doc = new PdfDocument())

{

//Add a page

PdfPage page = doc.Pages.Add();

//Create Pdf graphics for the page

PdfGraphics graphics = page.Graphics;

//Draw the table

PdfLayoutResult result = this.grid.Draw(page, new PointF(0, 0));

float height = result.Bounds.Height;

//Close the document

doc.Close();


return height;

}

}

}


and update row.Height to it on a hosting PdfGridRow

var height = grid.CurrentHeight;

if (row.Height < height)

{

row.Height = height;

}

but somehow it breaks other parts of a document.




GK Gowthamraj Kumar Syncfusion Team December 29, 2021 02:39 PM UTC

Hi umlprog, 

As we said earlier, If we did not set the grid row height and width, then it will be automatically auto increase the grid. In last update, we are suggested you to try the nested grid rendering without mentioning the default or calculated height or width. This is our actual behavior of PDF grid rendering. Please let us know, any specific purpose which are you implementing (sets minimal row height and columns) in your sample project?   

We request you to share the simplified sample to analyze the issue with your implementation on our end, so that it will be helpful for us to provide solution. 

Regards, 
Gowthamraj K 



UM umlprog December 30, 2021 11:38 AM UTC

We set width and height because we are reading these values from a template. Some layout has empty rows with a fixed height to create empty space in an output. If we skip setting row height, they will not render at all, since it has no content from Syncfusion point of view.



GK Gowthamraj Kumar Syncfusion Team December 31, 2021 09:39 AM UTC

Hi umlprog, 
 
We have understood your requirement, we can overcome this scenario by setting the default height to that particular grid row (which contains empty values on this grid row cells). While rendering the pdf grid, we are calculating the row height values based on the content for non-specified rows. This is our actual behavior of pdf grid. 
 
Please let us know if you need any assistance on this. 
 
Regards, 
Gowthamraj K 


Loader.
Up arrow icon