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;
}
|
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);
}
} |
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.
Hi umlprog,
Currently, we are analyzing your queries and we will update the further details on January 28th 2022.
Regards,
Surya V
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,
Regards,
Surya V
I tried 19.4.0.50 and it doesn't work.
Second grid still starts from the new page.
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.
I confirmed that Syncfusion.Pdf.WinForms 19.4.0.52 fixed this issue.
Thanks!