We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table height needed

I am having little issue while getting the table height.

if the text in the last row gets wrapped to next line because of space issue.

table.GetHeight() is not giving the correct height of the table.

this is creating little issue for my documents formatting.

Need help to get the actual table height.


Hello Sanjay,

Thanks for using our products.

I have tried reproducing the issue over my end while using the code snippet shared over
How to - Figure out if the table will break in current page and I am unable to notice any issue. In order to replicate this issue, I have slightly modified the code i.e. changed the ColumnWidths value for Table object to “100” so that table should have one column and have added a row at the end of table with large contents inside cell.

// create another row with large contents in its cells
Row LastRow = table1.Rows.Add();
LastRow.Cells.Add(“This is sample text in cell of Table inside PDF which is forcing the table to span more than one page”);

As per my observations, the table height information is properly being returned. Can you please share the code snippet that you are using so that we can test it in our environment. We apologize for your inconvenience.

PS, I have tested the scenario using Aspose.Pdf for .NET 6.1.0

private Stream NewPage(DataTable dataTableTask, DataTable dataTableWorkFlow, float width, string fileName)
{
Pdf page = new Pdf();

Section sec1 = page.Sections.Add();
sec1.PageInfo.PageWidth = width;
Image img1 = new Image(sec1);
img1.ImageInfo.File = "logo.jpg";
img1.ImageInfo.ImageFileType = ImageFileType.Jpeg;
sec1.Paragraphs.Add(img1);
img1.PositioningType = PositioningType.PageRelative;
img1.Left = 30;
img1.Top = 20;
img1.ImageInfo.FixHeight = 30;
img1.ImageInfo.FixWidth = 110;
sec1.Paragraphs[0].ID = "img1";

Text taskHeader = new Text(fileName);
sec1.Paragraphs.Add(taskHeader);
taskHeader.PositioningType = PositioningType.PageRelative;
taskHeader.Top = 35;
taskHeader.Left = width - textWidth(taskHeader);
taskHeader.TextInfo.Color = new Aspose.Pdf.Generator.Color("gray");
float origWidth = width;
width = (float)(width * 0.9);


Table table1 = new Table();
sec1.Paragraphs.Add(table1);
sec1.Paragraphs[2].ID = "table1";
table1.PositioningType = PositioningType.PageRelative;
table1.Left = 35;
table1.Top = 80;
table1.ColumnWidths = width*0.284+" "+width*0.157+" "+width*0.196+" "+ width*0.176+" "+ width*0.186;
table1.Margin.Bottom = 50;

Row row1 = table1.Rows.Add();
row1.FixedRowHeight = 16;
Cell cell1 = row1.Cells.Add();
cell1.BackgroundColor = new Aspose.Pdf.Generator.Color("aqua");
BorderInfo binf = new BorderInfo((int)BorderSide.All, new Aspose.Pdf.Generator.Color("gray"));
cell1.Border = binf;
Text tasksHeader = new Text("Tasks");
cell1.Paragraphs.Add(tasksHeader);
cell1.Paragraphs[0].Margin.Top = 3;
cell1.Padding.Left = -7;
cell1.ColumnsSpan = 5;

Row row2 = table1.Rows.Add();
row2.Border = new BorderInfo((int)BorderSide.Bottom, 8, new Aspose.Pdf.Generator.Color("white"));
Cell cell2 = row2.Cells.Add();
Text txt = new Text("The following tasks have been assigned to the participants in this signature workflow.");
txt.TextInfo.FontSize = 11;
txt.Margin.Top = 4;
txt.Margin.Left = 7;
cell2.Paragraphs.Add(txt);
cell2.ColumnsSpan = 5;
Row row3 = table1.Rows.Add();
row3.Border = this.RowLeftBorder();
this.AddBoldCell(row3, "Assigned To");
this.AddCell(row3, "Purpose");
this.AddCell(row3, "Due Date");
this.AddCell(row3, "Status");
this.AddBoldCell(row3, "Outcome");

Row lastRow1 = null;
bool brokenText = false;

foreach (DataRow dr in dataTableTask.Rows)
{
Row r = table1.Rows.Add();
r.Border = this.RowLeftBorder();
this.AddBoldCell(r, dr.Field(0));
this.AddCell(r, dr.Field(1));
this.AddCell(r, dr.Field(2));
this.AddCell(r, dr.Field(3));
this.AddBoldCell(r, dr.Field(4));
lastRow1 = r;

brokenText=(r.Cells[0].IsWordWrapped || r.Cells[1].IsWordWrapped || r.Cells[2].IsWordWrapped || r.Cells[3].IsWordWrapped || r.Cells[4].IsWordWrapped );

}

//foreach (Cell cell in lastRow1.Cells)
//{
// cell.Border = new BorderInfo((int)BorderSide.Bottom, 1, new Aspose.Pdf.Generator.Color("gray"));
//}

Graph graph1 = new Graph(sec1);
sec1.Paragraphs.Add(graph1);
sec1.Paragraphs[3].ID = "graph1";
graph1.PositioningType = PositioningType.ParagraphRelative;
graph1.ReferenceParagraphID = "table1";
graph1.Top = table1.GetHeight(page) + 3;//+ (brokenText ? 14:0);
float[] posArr = new float[] { 0, 0, width, 0 };
Line line1 = new Line(graph1, posArr);
line1.GraphInfo.Color = new Aspose.Pdf.Generator.Color("gray");
graph1.Shapes.Add(line1);

Table table2 = new Table();
sec1.Paragraphs.Add(table2);
table2.PositioningType = PositioningType.ParagraphRelative;
table2.ReferenceParagraphID = "graph1";
table2.Top = 20;
table2.Left = 0;
table2.ColumnWidths = width * 0.215 + " " + width * 0.205 + " " + width * 0.392 + " " + width * 0.186; //"110 105 200 95";

Row row21 = table2.Rows.Add();
row1.FixedRowHeight = 16;
cell1 = row21.Cells.Add();
cell1.BackgroundColor = new Aspose.Pdf.Generator.Color("aqua");
BorderInfo binf2 = new BorderInfo((int)BorderSide.All, new Aspose.Pdf.Generator.Color("gray"));
cell1.Border = binf2;
Text workHeader = new Text("Workflow History");
cell1.Paragraphs.Add(workHeader);
cell1.Paragraphs[0].Margin.Top = 3;
cell1.Paragraphs[0].Margin.Left = 7;
cell1.ColumnsSpan = 5;

Row row22 = table2.Rows.Add();
row22.Border = new BorderInfo((int)BorderSide.Bottom, 8, new Aspose.Pdf.Generator.Color("white"));
cell2 = row22.Cells.Add();
Text txt1 = new Text("The following events have occurred in this signature workflow.");
txt1.TextInfo.FontSize = 11;
txt1.Margin.Top = 4;
txt1.Margin.Left = 7;
cell2.Paragraphs.Add(txt1);
cell2.ColumnsSpan = 4;

Row row23 = table2.Rows.Add();
row23.Border = this.RowLeftBorder();
this.AddBoldCell(row23, "Date Occurred");
this.AddCell(row23, "Event Type");
this.AddCell(row23, "Description");
this.AddBoldCell(row23, "Outcome");

Row lastRow=null;
brokenText=false;

foreach (DataRow dr in dataTableWorkFlow.Rows)
{
Row r = table2.Rows.Add();
r.Border = this.RowLeftBorder();
this.AddBoldCell(r, dr.Field(0));
this.AddCell(r, dr.Field(1));
this.AddCell(r, dr.Field(2));
this.AddBoldCell(r, dr.Field(3));
lastRow = r;
//brokenText=(r.Cells[0].IsWordWrapped || r.Cells[1].IsWordWrapped|| r.Cells[2].IsWordWrapped|| r.Cells[3].IsWordWrapped);
}

//foreach (Cell cell in lastRow.Cells)
//{
// cell.Border = new BorderInfo((int)BorderSide.Bottom, 1, new Aspose.Pdf.Generator.Color("gray"));
//}

float f= lastRow.FixedRowHeight;

sec1.Paragraphs[4].ID = "table2";

Graph graph2 = new Graph(sec1);
sec1.Paragraphs.Add(graph2);
graph2.PositioningType = PositioningType.ParagraphRelative;
graph2.ReferenceParagraphID = "table2";
graph2.Top = table2.GetHeight(page) + 3; //+(brokenText ? 14:0)
//if(brokenText) graph2.Top+=f;
posArr = new float[] { 0, 0, width, 0 };
Line line2 = new Line(graph2, posArr);
line2.GraphInfo.Color = new Aspose.Pdf.Generator.Color("gray");
graph2.Shapes.Add(line2);
MemoryStream str = new MemoryStream();
try
{
page.Save(@"D:\docs\abc.pdf");
return str;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
str.Dispose();
}
return null;
}


This is the code I am using for generating a new pdf page. Occasionally the lines line1 and line2 don't show up below the last row of the table, but they show up above or over the last row contents.

Any help ?

Hello Sanjay,

Thanks for your patience.

I have tried replicating the issue over my end but I am afraid I am unable to notice the problem. In fact the code snippet that you have shared does not include the definition for
RowLeftBorder, AddBoldCell, AddCell methods and also the contents of dataTableTask and dataTableWorkFlow DataTables is not known. After little modifications to your code I am still unable to notice the problem. Can you please take a look over the attached code snippet and see if you can help us replicating this problem at our end or, can you please share the complete project so that we can test the scenario at our end. We are really sorry for this inconvenience.