Table Page Break (PDF)

Hello!

I am trying to stop Aspose PDF from page breaking in the middle of a table in C# .NET. I have not found any answers that have solved this problem through Google, Stackoverflow, or this forum.

We have a table with two rows that must always be together as they are utilized as a header. These rows are currently being split sometimes when they happen to fall on a page break. I wish to stop this from happening.

I have found the table.isBroken attribute always returns true and row.isRowBroken always returns false (from Aspose). This does not help me to stop the page from breaking in the middle of this table.

Here is a link to the post on stackoverflow: http://stackoverflow.com/questions/42079506/do-not-page-break-on-table-in-aspose-pdf-c-sharp-asp-net

Thank you for your time!

Hi Andrew,

Thanks for your inquriy. To repeat your header rows on page break and keep them together you can use RepeatingRowCount property as following. However if there is any difference in your requirement and my understanding then please share your sample code and sample output here, so we will look into the issue and will guide you accordingly.

Aspose.Pdf.Table
table = new Aspose.Pdf.Table();

table.RepeatingRowsCount = 2;

Best Regards,

Hello Tilal!


Thanks for the help!

<span style=“color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);”>That kind of works, except it throws an ArgumentOutOfRangeException. I fixed it by setting table.RepeatingRowsCount = 1; This mostly fixed the problem, however I am still left with a top border for an invisible form header on the previous page. So now there are kind of two form headers, one on the previous page that only shows the top border, and one on the next page that behaves as expected.

Hi Andrew,


Thanks for your feedback. It is good to know that suggested code helped you. However in reference to your mentioned issues, we will appreciate it if you please share your sample code along with expected PDF document. So we will look into the scenario and will guide you accordingly.

We are sorry for the inconvenience.

Best Regards,

Tilal,


The “aspose2” attachment shows what was happening before I set table.RepeatingRowsCount = 1;

The “aspose1” attachment shows what is happening now.


Here is the code for the table. I have excluded all the code that adds the content to the table rows/cells to make it easier. Hopefully this is enough information.

Table table = new Table();
table.Border = new BorderInfo { Top = new GraphInfo { Color = Color.Black, LineWidth = 1 } };

//Add two rows/cells with text in them

table.RepeatingRowsCount = 1;
page.Paragraphs.Add(table);


The expectation is that the Border for the Top of the table does not stick around before the page break.

Thanks!

Hi Andrew,

Thanks for sharing more details. I tried to create a PDF document with a table inside it. I added enough rows into table so that it can cause page to break. In the generated output I was unable to see any Top Border before the page break.

I tried following code snippet and I have also attached generated output for your reference.

Document doc = new Document();
var page = doc.Pages.Add();
Table table = new Table();
table.Border = new BorderInfo { Top = new GraphInfo { Color = Color.Black, LineWidth = 1 } };

Row r = new Row();
r.Cells.Add("Cell");
r.Cells.Add("Cell");

int i = 0;

while (i < 80)
{
    table.Rows.Add(r.Clone());
    i++;
}

table.RepeatingRowsCount = 1;
page.Paragraphs.Add(table);
doc.Save("PageBreakTable.pdf");

We will really appreciate if you please share complete sample code snippet along with sample PDF Document so that we can try to reproduce the scenario in our environment and get back to you with helpful information accordingly. We are sorry for the inconvenience.

Best Regards,

Hey Tilal,


Here is a much better example for you. I have taken the time to create a full example specifically for this problem. The code is a little bit messy, but it shows what I am talking about. The left border does the same thing as the top border if I add it in as well. Let me know if you cannot duplicate this bug or need other information. Thank you!

Here is the code:


normalTextState = new TextState(defaultFont, defaultFontSize);
level1SectionTextState = new TextState(defaultFont, 14) { FontStyle = FontStyles.Bold };
level2SectionTextState = new TextState(defaultFont, 12) { FontStyle = FontStyles.Bold };

private void TemporaryPDF(Page page)
{
Table tableSpacer = new Table();
Row rowSpacers = tableSpacer.Rows.Add();
rowSpacers.BackgroundColor = Color.Parse("#00FF00");
AddCell(rowSpacers, null, “-1”, normalTextState);
for (int i = 0; i < 47; i++)
{
rowSpacers = tableSpacer.Rows.Add();
rowSpacers.BackgroundColor = (i % 2 == 1) ? Color.Parse("#00FF00") : Color.White;
AddCell(rowSpacers, null, i.ToString(), normalTextState);
}
page.Paragraphs.Add(tableSpacer);


Table table = new Table();
table.Margin = new MarginInfo(0, 0, 0, 9);
table.Border = new BorderInfo { Top = new GraphInfo { Color = Color.Black, LineWidth = 1 } };
table.ColumnWidths = “145 135 120”;

//Form Row
Row row = table.Rows.Add();
row.BackgroundColor = Color.Parse("#CFD4D7");

AddCell(row, null, “Form:”, level2SectionTextState);
AddCell(row, null, “Hello”, normalTextState);
AddCell(row, null, “No Information”, normalTextState);
AddCell(row, null, “No Information”, normalTextState);

//Status Row
row = table.Rows.Add();
row.BackgroundColor = Color.Parse("#CFD4D7");

//Status and Score
AddCell(row, null, "Status: ", level2SectionTextState);
Cell cell = new Cell();
AddCell(row, null, “No Information”, normalTextState);
AddCell(row, null, “No Information”, normalTextState);
AddCell(row, null, “No Information”, normalTextState);

table.RepeatingRowsCount = 1;
page.Paragraphs.Add(table);
}

private Cell AddCell(Row row, Color backgroundColor, string text = “”, TextState textState = null)
{
var cell = text != null && text.Length > 0 ? row.Cells.Add(text, textState) : row.Cells.Add();

cell.VerticalAlignment = VerticalAlignment.Top;
if (backgroundColor != null)
{
cell.BackgroundColor = backgroundColor;
}
cell.Margin = new MarginInfo(cellPadding, cellPadding, cellPadding, cellPadding);

return cell;
}

Hi Andrew,


Thanks for sharing your sample code. I have tested your code with different fonts and margin settings using Apsose.Pdf for 17.2.0, but still unable to replicate the issue.

Furthermore, you are using an old version of Aspose.Pdf, please download latest version of Aspose.Pdf for .NET and test the scenario. Hopefully it will resolve the issue, however if the issue persist then please share a sample console project with us to replicate the issue at our end.

We are sorry for the inconvenience.

Best Regards,

Tilal,


Updating our code to reference the newest version of Aspose has solved our page break border problem. Thank you for your help with everything!

Thanks!

Hi Andrew,


Thanks for the acknowledgement.

We are glad to hear that your problem is resolved. Please continue using our API and in the event of any further query, please feel free to contact.