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);
}
}
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
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);
}
}
Any follow up on this?
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
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?
Hi umlprog,
Currently, we are analyzing on the reported issue and we will update the further details on February 9th 2022.
Regards,
Surya V
I confirmed that Syncfusion.Pdf.WinForms 19.4.0.52 fixed this issue.
Thanks!
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);
}
}
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