Table Rows Overflow Does NOT use previous page empty area when populated. by loop.

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v12)  /  Re: Table Rows Overflow Does NOT use previous page empty area when populated. by loop.

DynamicPDF CoreSuite for .NET (v12) Forum

Hi,
I have a static table with 4 rows and 3 columns with static information on the page I have created.
Now i want to add another table that is populated dynamically using loop. When i use overflow method like in documentation. it prints the rows starting from the new page, while i have almost half page which is empty on previous created/added page in document. How to use the remaining space of the page and then create the new page and so on. ???? My code is given below:


                 Document document = new Document();
                // Create a page
                ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(PageSize.A4);
                document.Pages.Add(page);

                Table2 table = new Table2(0, 0, 700, 100);
                //table.CellDefault.Border.Color = RgbColor.White;
                table.Border.LineStyle = LineStyle.None;
                table.CellSpacing = 1.0f;
                // Add columns to the table
                table.Columns.Add(100);
                table.Columns.Add(200);
                table.Columns.Add(100);
                table.Columns.Add(100);

                // Add rows to the table and add cells to the rows
                Row2 row1 = table.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White);
                row1.CellDefault.Align = ceTe.DynamicPDF.TextAlign.Center;
                row1.CellDefault.VAlign = VAlign.Center;
                row1.CellDefault.Border.LineStyle = LineStyle.None;
                row1.Cells.Add("Client Name :").Align = ceTe.DynamicPDF.TextAlign.Left;
                row1.Cells.Add("Mike Trevor").Align = ceTe.DynamicPDF.TextAlign.Left;
                row1.Cells.Add("Ref :").Align = ceTe.DynamicPDF.TextAlign.Right;
                row1.Cells.Add("MH24050039-1").Align = ceTe.DynamicPDF.TextAlign.Right;


                // Add rows to the table and add cells to the rows
                Row2 row2 = table.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White);
                row2.CellDefault.Align = ceTe.DynamicPDF.TextAlign.Center;
                row2.CellDefault.Border.LineStyle = LineStyle.None;
                row2.Cells.Add("Postal Address :").Align = ceTe.DynamicPDF.TextAlign.Left;
                row2.Cells.Add("1234567890,ABC,ABC,54000 ").Align = ceTe.DynamicPDF.TextAlign.Left;
                row2.Cells.Add("Date :").Align = ceTe.DynamicPDF.TextAlign.Right;
                row2.Cells.Add("12 May 2024").Align = ceTe.DynamicPDF.TextAlign.Right;

                // Add rows to the table and add cells to the rows
                Row2 row3 = table.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White);
                row3.CellDefault.Border.LineStyle = LineStyle.None;
                row3.CellDefault.Align = ceTe.DynamicPDF.TextAlign.Center;
                row3.Cells.Add("Mobile No :").Align = ceTe.DynamicPDF.TextAlign.Left;
                row3.Cells.Add("03214787567").Align = ceTe.DynamicPDF.TextAlign.Left;



                // Add rows to the table and add cells to the rows
                Row2 row4 = table.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White);
                row4.CellDefault.Border.LineStyle = LineStyle.None;
                row4.CellDefault.Align = ceTe.DynamicPDF.TextAlign.Center;
                row4.Cells.Add("Email :").Align = ceTe.DynamicPDF.TextAlign.Left;
                row4.Cells.Add("awaisahmad042@gmail.com").Align = ceTe.DynamicPDF.TextAlign.Left;

                // Add rows to the table and add cells to the rows
                Row2 row5 = table.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White);
                row5.CellDefault.Border.LineStyle = LineStyle.None;
                row5.CellDefault.Align = ceTe.DynamicPDF.TextAlign.Center;
                row5.Cells.Add("Site Address :").Align = ceTe.DynamicPDF.TextAlign.Left;
                row5.Cells.Add("N/A").Align = ceTe.DynamicPDF.TextAlign.Left;

                page.Elements.Add(table);


-- Adding another table for dynamic size data by loop.

  // Add a new table for dynamic rows
                Table2 dynamicTable = new Table2(0, table.Height + 20, 500, 1000);
                dynamicTable.Border.LineStyle = LineStyle.Solid;

                // Add columns to the dynamic table
                dynamicTable.Columns.Add(100);
                dynamicTable.Columns.Add(100);
                dynamicTable.Columns.Add(200);




                // Populate dynamic table with rows
                for (int i = 1; i <= 50; i++)
                {

                    Row2 dynamicRow = dynamicTable.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White);
                    dynamicRow.Cells.Add($"Data {i}");
                    dynamicRow.Cells.Add($"Data {i}");
                    dynamicRow.Cells.Add($"Data {i}");
                }

                // Add the dynamic table to the page
                page.Elements.Add(dynamicTable);


The above code is overflowing the data , if i use the overflow method like below with the above code:

do
{
    Page page = new Page();
    document.Pages.Add(page);
    page.Elements.Add(table);
    table = table.GetOverflowRows();
} while (table != null);

It is not working fine.

(The thing is that i will set the logic for the visibility of controls like i want to hide and show some tables or elements on the page from database control ,,,,, ), SO thi dynamic page is either not working or when working it is not using the remaining previous page........

Can u please help me .
Thanks.
Hey there,
It sounds like you're having trouble with table rows overflowing to a new page without using the remaining space on the current page.To fix this, you need to calculate the remaining space on the current page and add rows to the dynamic table until the page is full. Here’s a simplified version of your code to help with that:
Document document = new Document();
// Create the first page and add the static table
ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(PageSize.A4);
document.Pages.Add(page);

Table2 staticTable = new Table2(0, 0, 700, 100);
staticTable.Columns.Add(100);
staticTable.Columns.Add(200);
staticTable.Columns.Add(100);
staticTable.Columns.Add(100);

// Add rows to the static table
staticTable.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White).Cells.Add("Client Name :").Align = ceTe.DynamicPDF.TextAlign.Left;
staticTable.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White).Cells.Add("Postal Address :").Align = ceTe.DynamicPDF.TextAlign.Left;
staticTable.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White).Cells.Add("Mobile No :").Align = ceTe.DynamicPDF.TextAlign.Left;
staticTable.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White).Cells.Add("Email :").Align = ceTe.DynamicPDF.TextAlign.Left;
staticTable.Rows.Add(20, Font.Helvetica, 9, RgbColor.Black, RgbColor.White).Cells.Add("Site Address :").Align = ceTe.DynamicPDF.TextAlign.Left;

page.Elements.Add(staticTable);

// Calculate remaining height on the page
float remainingHeight = PageSize.A4.Height - staticTable.Y - staticTable.Height;

// Add dynamic table
Table2 dynamicTable = new Table2(0, staticTable.Y + staticTable.Height + 20, 500, remainingHeight);
dynamicTable.Columns.Add(100);
dynamicTable.Columns.Add(100);
dynamicTable.Columns.Add(200);

int rowHeight = 20;
int rowsPerPage = (int)(remainingHeight / rowHeight);

// Populate dynamic table
for (int i = 1; i <= 50; i++) {
    if ((i - 1) % rowsPerPage == 0 && i != 1) {
        // Add dynamic table to page and create a new page if full
        page.Elements.Add(dynamicTable);
        page = new Page(PageSize.A4);
        document.Pages.Add(page);
        dynamicTable = new Table2(0, 0, 500, 1000);
        dynamicTable.Columns.Add(100);
        dynamicTable.Columns.Add(100);
        dynamicTable.Columns.Add(200);
    }
    dynamicTable.Rows.Add(rowHeight, Font.Helvetica, 9, RgbColor.Black, RgbColor.White).Cells.Add($"Data {i}");
}

// Add the final dynamic table to the page
page.Elements.Add(dynamicTable);

// Save the document
document.Draw("[path to save document]");

This code ensures that the remaining space on the first page is used before moving to a new page.

Hope this helps!

All times are US Eastern Standard time. The time now is 8:02 PM.