PdfGridCell ColumnSpan cell width wider than expected

When I set ColumnSpan to the number of columns in a grid, row appears few pixels wider than expected.


        public void ColspanIssue()

        {

            using (PdfDocument pdfDocument = new PdfDocument())

            {

                const float pageMargins = 15;


                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 with 2 rows

                const float padding = 3;

                cell.Style.CellPadding = new PdfPaddings(padding, padding, padding, padding);

                PdfGrid grid = new PdfGrid();

                float width = col.Width - 2 * padding;

                grid.Columns.Add();

                grid.Columns.Add();

                foreach (PdfGridColumn gridColumn in grid.Columns)

                {

                    gridColumn.Width = width / 2;

                }


                cell.Value = grid;

                grid.Rows.Add();

                grid.Rows[0].Cells[0].ColumnSpan = 2;

                grid.Rows[0].Cells[0].Style.CellPadding = cell.Style.CellPadding;

                grid.Rows[0].Cells[0].Value = "Why is this cell extending beyond table?";


                grid.Rows.Add();

                grid.Rows[1].Cells[0].Style.CellPadding = cell.Style.CellPadding;

                grid.Rows[1].Cells[0].Value = "Row 1";

                grid.Rows[1].Cells[1].Style.CellPadding = cell.Style.CellPadding;

                grid.Rows[1].Cells[1].Value = "Row 1";


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

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

            }

        }




12 Replies 1 reply marked as answer

SV Surya Venkatesan Syncfusion Team January 27, 2022 12:26 PM UTC

Hi umlprog,


We modified the provided code snippet to resolve the reported issues of colspan and the modified code snippet contains sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PDF_Sample_ColspanFix-1825703828 and its output document: https://www.syncfusion.com/downloads/support/directtrac/general/pd/File637789017249629910-10986248 for your reference.


Refer to the following link for more details,

https://help.syncfusion.com/file-formats/pdf/working-with-tables


Kindly try the attached sample and let us know the result.


Regards,

Surya V



UM umlprog January 28, 2022 10:50 AM UTC

If we remove setting column width issue persists.

If we remove setting column width, issue persists

        public static void ColspanIssueWidthNotSet()

        {

            using (PdfDocument pdfDocument = new PdfDocument())

            {

                const float pageMargins = 15;

                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 with 2 rows


                const float padding = 3;


                cell.Style.CellPadding = new PdfPaddings(padding, padding, padding, padding);


                PdfGrid grid = new PdfGrid();

// Not settings width

                grid.Columns.Add();


                grid.Columns.Add();


                cell.Value = grid;


                grid.Rows.Add();


                grid.Rows[0].Cells[0].ColumnSpan = 2;


                grid.Rows[0].Cells[0].Style.CellPadding = cell.Style.CellPadding;


                grid.Rows[0].Cells[0].Value = "Why is this cell extending beyond table?";




                grid.Rows.Add();


                grid.Rows[1].Cells[0].Style.CellPadding = cell.Style.CellPadding;


                grid.Rows[1].Cells[0].Value = "Row 1";


                grid.Rows[1].Cells[1].Style.CellPadding = cell.Style.CellPadding;


                grid.Rows[1].Cells[1].Value = "Row 1";


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


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

            }

        }




UM umlprog January 31, 2022 02:14 PM UTC

Any follow up on this?



SV Surya Venkatesan Syncfusion Team January 31, 2022 03:15 PM UTC

Hi umlprog,


On further analysis of provided code snippet, The second grid was drawn in the first cell of the first row in the rootgrid and second gird of first row was colspan with 2 due to this, the reported issue was caused if remove the colspan it works fine. So, we recommend to draw with column setting width or draw second grid separately without using rootgrid to overcome the reported issue.


Kindly let us know if you need any further assistance with this.


Regards,

Surya V



UM umlprog February 4, 2022 07:11 PM UTC

We must have second grid inside the first grid.

And it is unexpected that width of the first row is greater than width of the second row just because colspan = 2 is set on a first row. We may not necessarily want to set column width, it should assume width automatically.

This seems like a bug. Can you fix it?



SV Surya Venkatesan Syncfusion Team February 7, 2022 12:35 PM UTC

Hi umlprog,


Currently, we are analyzing on the reported issue and we will update the further details on February 9th 2022.


Regards,

Surya V



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

Hi umlprog,

Thank you for your patience. 
 
We confirmed the issue “The cell width gets greater when applying column-span in the child grid” as a defect in our product. We will include the fix for this issue in our weekly NuGet release, which will be available on February 15th, 2022. 
 
Please use the below feedback link to track the status of the reported bug. 
 
Note: If you require patch for the reported issue in any of our Essential Studio Main or SP release version, then kindly let us know the version, so that we can provide a patch in that version based on our SLA policy. 
 
Please let us know if you need any further assistance in this. 

Regards,
 
Gowthamraj K 



GK Gowthamraj Kumar Syncfusion Team February 15, 2022 10:23 AM UTC

Hi umlprog, 
 
We have included the fix for the reported issue “The cell width gets greater when applying column-span in the child grid” in our latest weekly NuGet release (v19.4.0.52).  
 
Please use the below link to download our latest weekly NuGet,      
  
Please let us know if you have any concerns about this   
 
Regards, 
Gowthamraj K 


Marked as answer

GK Gowthamraj Kumar Syncfusion Team February 16, 2022 02:15 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!



UM umlprog February 18, 2022 01:54 PM UTC

Now in some cases cells are actually shrinked (prior to this fix they were wider, now they are shorter).


public void ColspanIssue2()

{

using (PdfDocument pdfDocument = new PdfDocument())

{

const float pageMargins = 15;


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.Aquamarine;


// Create grid with 2 rows

const float padding = 3;

cell.Style.CellPadding = new PdfPaddings(padding, padding, padding, padding);

PdfGrid grid = new PdfGrid();

grid.Columns.Add();

grid.Columns.Add();

grid.Columns.Add();

cell.Value = grid;


void SetCell(int rowIndex, int cellIndex, string text, int colSpan = 1)

{

PdfGridCell _cell = grid.Rows[rowIndex].Cells[cellIndex];

_cell.Style.CellPadding = cell.Style.CellPadding;

_cell.Style.Borders.All = new PdfPen(Color.Black, 0.5f);

_cell.ColumnSpan = colSpan;

_cell.Value = text;

}

grid.Rows.Add();

SetCell(0, 0, "A");

SetCell(0, 1, "B");

SetCell(0, 2, "C");


grid.Rows.Add();

SetCell(1, 0, "Colspan 3: Why is this cell more narrow than above?", 3);


grid.Rows.Add();

SetCell(2, 0, "A");

SetCell(2, 1, "B");

SetCell(2, 2, "C");


grid.Rows.Add();

SetCell(3, 0, "A");

SetCell(3, 1, "Colspan 2: Why is this cell more narrow than above?", 2);


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

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

}

}





SV Surya Venkatesan Syncfusion Team replied to umlprog February 21, 2022 06:26 PM UTC

Hi umlprog,


We request you to follow up on this forum: https://www.syncfusion.com/forums/173004/pdfgridcell-columnspan-cell-width-more-narrow-than-expected?reply=S2uLXj for further details.


Regards,

Surya V


Loader.
Up arrow icon