Free Support Forum - aspose.com

Is it possible to know when a table is broken?

Hi,
I want to know if there is a way to know when a table isBroken. I create a table which is at the end of my page and sometimes it is just divided between my two pages. I change the property isBroken to false but I wanted to know when my table is passing to the next page because I have to change my top margin according to that.
Is there a way?
Thanks for your help

Hello Anouck,

Thanks for using our products.

You can programmatically get the required information. For that reason, first you need to get the document size information, then you need to get the page Top and page Bottom margin information, Table top margin information and table height. If you add page Top Margin + page Bottom Margin + table Top Margin + table Height and deduct it from the document height, you can get the amount of space present over the document. Depending upon the particular height of row that you have specified, you can calculate that if the row can be accommodated within the remaining space over page or not.

Please take a look over the following code snippet. In following code, the average row height is 23.002 Points.

[C#]

// Instantiate an object PDF class
Pdf pdf = new Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Section section = pdf.Sections.Add();

//Instantiate a table object
Table table1 = new Table();
table1.Margin.Top = 300;
//Add the table in paragraphs collection of the desired section
section.Paragraphs.Add(table1);
//Set with column widths of the table
table1.ColumnWidths = "100 100 100";
//Set default cell border using BorderInfo object
table1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
//Set table border using another customized BorderInfo object
table1.Border = new BorderInfo((int)BorderSide.All, 1F);

//Create MarginInfo object and set its left, bottom, right and top margins
MarginInfo margin = new MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;
//Set the default cell padding to the MarginInfo object
table1.DefaultCellPadding = margin;

for (int RowCounter = 0; RowCounter <= 16; RowCounter++) // if you increase the counter to 17, table will break because it can not be accomodated any more over this page
{
//Create rows in the table and then cells in the rows
Row row1 = table1.Rows.Add();
row1.Cells.Add("col " + RowCounter.ToString() + ", 1");
row1.Cells.Add("col " + RowCounter.ToString() + ", 2");
row1.Cells.Add("col " + RowCounter.ToString() + ", 3");
}

// Display Page Height, Table Height, table Top margin and Page Top and Bottom margin information
MessageBox.Show("PDF document Height = " + pdf.PageSetup.PageHeight.ToString() + "\nTop Margin Info = " + section.PageInfo.Margin.Top.ToString() + "\nBottom Margin Info = " + section.PageInfo.Margin.Bottom.ToString() + "\nTable-Top Margin Info = " + table1.Margin.Top.ToString() + " \nTable height " + table1.GetHeight(pdf).ToString() + "\nTable Row Height = " + table1.Rows[0].GetHeight(pdf).ToString());

// get the Page Height information
float PageHeight = pdf.PageSetup.PageHeight;

// get the total height information of Page Top & Bottom margin, table Top margin and table height.
float TotalObjectsHeight = section.PageInfo.Margin.Top + section.PageInfo.Margin.Bottom + table1.Margin.Top + table1.GetHeight(pdf);

// Display information retrieved in previous steps
MessageBox.Show("" + PageHeight.ToString() + " " +TotalObjectsHeight.ToString());

// check if we deduct the sume of Page top margin + Page Bottom margin + Table Top margin and table height from Page height and its less than 10 (an average row can be greater than 10)

if ((PageHeight - TotalObjectsHeight) <= 10)
// if the value is less than 10, then display the message. Which shows that another row can not be placed and if we add new row, table will break. It depends upon the row height that you have specified.
MessageBox.Show("Page Bottom Margin is less than 10");

//Save the pdf document
pdf.Save(@"D:/pdftest/TableIsBrokenTest.pdf");

In case it does not resolve your problem or you have any further query, please feel free to contact.

I didn’t say that I work in VB.NET. So here’s the code, if that can help somebody :
’ Instantiate an object PDF class
Dim Pdf = New Pdf()
’ add the section to PDF document sections collection
Dim section As Aspose.Pdf.Section = Pdf.Sections.Add()

'Instantiate a table object
Dim table1 As Table = New Table()
table1.Margin.Top = 300
'Add the table in paragraphs collection of the desired section
section.Paragraphs.Add(table1)
'Set with column widths of the table
table1.ColumnWidths = “100 100 100”
'Set default cell border using BorderInfo object
table1.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.1F)
'Set table border using another customized BorderInfo object
table1.Border = New BorderInfo(BorderSide.All, 1.0F)

'Create MarginInfo object and set its left, bottom, right and top margins
Dim margin As MarginInfo = New MarginInfo()
margin.Top = 5.0F
margin.Left = 5.0F
margin.Right = 5.0F
margin.Bottom = 5.0F
'Set the default cell padding to the MarginInfo object
table1.DefaultCellPadding = margin

For RowCounter As Integer = 0 To 16 ’ if you increase the counter to 17, table will break because it can not be accomodated any more over this page

'Create rows in the table and then cells in the rows
Dim row1 As Row = table1.Rows.Add()
row1.Cells.Add("col " + RowCounter.ToString() + “, 1”)
row1.Cells.Add("col " + RowCounter.ToString() + “, 2”)
row1.Cells.Add("col " + RowCounter.ToString() + “, 3”)
Next

’ Display Page Height, Table Height, table Top margin and Page Top and Bottom margin information
Console.Write("PDF document Height = " & Pdf.PageSetup.PageHeight.ToString() & "\nTop Margin Info = " & section.PageInfo.Margin.Top.ToString() & "\nBottom Margin Info = " & section.PageInfo.Margin.Bottom.ToString() & “\nTable-Top Margin Info = " & table1.Margin.Top.ToString() & " \nTable height " & table1.GetHeight(Pdf).ToString() & “\nTable Row Height = " & table1.Rows(0).GetHeight(Pdf).ToString())

’ get the Page Height information
Dim PageHeight As Double = Pdf.PageSetup.PageHeight

’ get the total height information of Page Top & Bottom margin, table Top margin and table height.
Dim TotalObjectsHeight As Double = section.PageInfo.Margin.Top + section.PageInfo.Margin.Bottom + table1.Margin.Top + table1.GetHeight(Pdf)

’ Display information retrieved in previous steps
Console.Write(”” & PageHeight.ToString() & " " & TotalObjectsHeight.ToString())

’ check if we deduct the sume of Page top margin + Page Bottom margin + Table Top margin and table height from Page height and its less than 10 (an average row can be greater than 10)

If PageHeight - TotalObjectsHeight <= 10 Then
’ if the value is less than 10, then display the message. Which shows that another row can not be placed and if we add new row, table will break. It depends upon the row height that you have specified.
Console.Write(“Page Bottom Margin is less than 10”)
End If
'Save the pdf document
Pdf.Save(“D:/TableIsBrokenTest.pdf”)


Thanks for teh help, I’ll try this way !

Hello Anouck,

Thanks for sharing the code snippet.

If you have already mentioned that you are working in VB.NET, then I would have been more than happy to share the code snippet in VB.NET. However, please try using this approach and in case you have any further query, please feel free to contact.

Yeah I totally forgot to point out that.
In fact I still have a problem that I don’t understand
Each time I write the following code, I have an error when I save my document :
Tableau.GetHeight(Doc.Doc)
doc.doc is an Aspose.Pdf.Pdf

Here is the error, I thought it was because I remove some lines of my Table but I comment my remove code and it still doesn’t work. Any idea of what is doing that? (the code is a little complex for the table filling but I couldn’t think of anything which don’t allow to calculate the height) It is strange because the height is returned but just the moment I save my pdf the error occurred.
System.NullReferenceException: La référence d’objet n’est pas définie à une instance d’un objet.
à x3f91538e8c0de749.x1e934fbd7f82b78c.xd09ff365fb39fccc(Table x1ec770899c98a268, Int32 x78a7603cacf2ae22, Int32 x7b28e8a789372508, Int32 xfce896c6a8f97f29)
à x3f91538e8c0de749.x1e934fbd7f82b78c.x54c647965e5f6928(Pdf x6beba47238e0ade6, Section x4c742376f4cbaf27, Table x1ec770899c98a268, Single xb6c9438c24bd11c8)
à x3f91538e8c0de749.x1e934fbd7f82b78c.x655c25ff3d87c202(Pdf x6beba47238e0ade6, Section x4c742376f4cbaf27, HeaderFooter x105be60320c854bc, Table x1ec770899c98a268, x33eef31791ddfc27 xb4012f98512a2f3d, x33eef31791ddfc27 x1af02198e7b35f4e, xbfecfdaff4b493fe x96b3b0aca69853de, Boolean x70e701cee22724e3)
à x3f91538e8c0de749.x2288a8cd3edf6dd5.x5b7cc3ed4565d299(Pdf x6beba47238e0ade6, Section x4c742376f4cbaf27, HeaderFooter x105be60320c854bc, Table x1ec770899c98a268, x33eef31791ddfc27 xb4012f98512a2f3d, xbfecfdaff4b493fe x96b3b0aca69853de, Boolean x70e701cee22724e3)
à x3f91538e8c0de749.x7576dadd7c449efa.x8810991188d773db(Pdf x6beba47238e0ade6, Section xb32f8dd719a105db, x33eef31791ddfc27 xb4012f98512a2f3d)
à x3f91538e8c0de749.x6a7cd842e95518d7.xc5e548c04b18e6e2(Pdf x6beba47238e0ade6)
à x3f91538e8c0de749.xaa36f84dba9d652f.x5ec1518f85fc1bbf(xb912e9429339cb81 x80010f902a1f736e, Pdf x6beba47238e0ade6)
à Aspose.Pdf.Pdf.Save(String pdfFile)

Hello Anouck,

In my earlier test, I was able to get the table height and was able to save the resultant PDF. Can you please share the sample code/project that we can use to reproduce the problem at our end. I am afraid until or unless we are able to replicate this problem here, we might not be able to figure out the actual reasons of this problem.

We apologize for your inconvenience.