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

Free Support Forum - aspose.com

Text in Cell not centered- the first row is detached from remaining table

Hello,

I recently upgraded from Aspose.PDF version 4 to the latest version. However,

The formatting of data in the Table object is messed up.

1. The data inside the first row's cells no longer centered(this is the headerRow in my code below).

2. The headerRow is detached from the rest of the table.

Could you please let me know how to fix this problem? Below is my code used for formatting the table. The same code used to work fine in Version 4.0

//create a table to import data
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
tab1.DefaultCellTextInfo.FontSize = 8;
tab1.DefaultCellPadding.Left = 2;
tab1.DefaultCellPadding.Right = 2;
tab1.DefaultCellPadding.Bottom = 2;
tab1.DefaultCellPadding.Top = 2;
sec1.Paragraphs.Add(tab1);
tab1.ColumnWidths = "70 150 150 50 50 50 50 50 50 50 50 50";
tab1.ColumnAdjustment = ColumnAdjustmentType.AutoFitToContent;
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);

//create header row

Row headerRow = tab1.Rows.Add();

headerRow.FixedRowHeight = 20;

headerRow.DefaultCellTextInfo.Alignment = AlignmentType.Center;

headerRow.VerticalAlignment = VerticalAlignmentType.Center;

//Import data into the Table object from the DataTable created above

tab1.ImportDataTable(dtData, false,2, 0);

AsposeNewBee:

1. The data inside the first row's cells no longer centered(this is the headerRow in my code below).

Hello Susan,

Thanks for using our products.

I have tested the scenario using following code snippet and I am able to notice the problems that text inside first row is not center aligned and also the Table.ColumnAdjustment property is not working. For the sake of correction, I have logged this problem as PDFNET-24908 in our issue tracking system. I am able to notice the problem with v5.0.0 and also with v4.0.0. We will further look into the details of these problems and will keep you updated on the status of correction. We apologize for your inconvenience.

AsposeNewBee:

2. The headerRow is detached from the rest of the table.

I am afraid I am unable to understand this problem. Can you please take a look over the following code snippet and the resultant PDF documents that I have generated.

[C#]

DataTable dt = new DataTable("Employee");
dt.Columns.Add("Employee_ID", typeof(Int32));
dt.Columns.Add("Employee_Name", typeof(string));
dt.Columns.Add("Gender", typeof(string));

//Add 2 rows into the DataTable object programmatically
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] = "John Smith";
dr[2] = "Male";

dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 2;
dr[1] = "Mary Miller";
dr[2] = "Female";
dt.Rows.Add(dr);

//Instantiate a Pdf instance
Pdf pdf1 = new Pdf();
//Create a section in the Pdf instance
Section sec1 = pdf1.Sections.Add();

//create a table to import data
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
tab1.DefaultCellTextInfo.FontSize = 8;
tab1.DefaultCellPadding.Left = 2;
tab1.DefaultCellPadding.Right = 2;
tab1.DefaultCellPadding.Bottom = 2;
tab1.DefaultCellPadding.Top = 2;
sec1.Paragraphs.Add(tab1);
tab1.ColumnWidths = "70 150 150 50";
tab1.ColumnAdjustment = ColumnAdjustmentType.AutoFitToContent;
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);

//create header row
Row headerRow = tab1.Rows.Add();
headerRow.FixedRowHeight = 20;
headerRow.DefaultCellTextInfo.Alignment = AlignmentType.Center;
headerRow.VerticalAlignment = VerticalAlignmentType.Center;
//Import data into the Table object from the DataTable created above
tab1.ImportDataTable(dt, true,0, 1,3,3);
pdf1.Save(@"D:\pdftest\TableAlignment_test.pdf");

Thanks for responding to my request.

My client wants us to centered align the text within the headers. Is there anyway to work around this issue and force it to center the text?

Also, regarding the table detaching from the first row, if you make 12 columns and set the width=150 then you should be able to reproduce the problem. The first row does not detach in your code because there is only 4 columns.

In your sample code, how do I rename the text in the first row using Aspose.PDF without going into the database? I am not allowed to modifiy the data in the DB. but I would like to name the header row different from the DB.

AsposeNewBee:
My client wants us to centered align the text within the headers. Is there anyway to work around this issue and force it to center the text?

Hello Susan,

In order to center align the text, you can specify the Alignment information for each cell individually. You may try using the following code line

each_cell.Alignment = AlignmentType.Center;

AsposeNewBee:
Also, regarding the table detaching from the first row, if you make 12 columns and set the width=150 then you should be able to reproduce the problem. The first row does not detach in your code because there is only 4 columns.

I have tried reproducing the problem with 12 Columns where each column width is 150, and I am unable to notice the problem. Please take a look over the attached PDF document that I have generated. I have used the following code line to specify the column width information for the table cells.

tab1.ColumnWidths ="70 150 150 150 150 150 150 150 150 150 150 150";

However, from detach if you mean that column is not displaying, you may set the page orientation to Landscape so that more columns can be accommodated easily. Or, you can set the value of IsVerticalBroken broken property of Table class to true. This value indicates whether the table is broken vertically as the table is printed out of the right page margin.

AsposeNewBee:
In your sample code, how do I rename the text in the first row using Aspose.PDF without going into the database? I am not allowed to modifiy the data in the DB. but I would like to name the header row different from the DB.

In order to update the contents inside table cells of header row, first you need to clear the contents of table cell and then you can add new contents to the paragraphs collection of cell object. Please take a look over the following code snippet in which I have specified the individual alignment information for each table cell and have update the contents inside header row.

[C#]

// iterate through each table cell
foreach (Aspose.Pdf.Cell each_cell in headerRow.Cells)
{
// set the alignment information for each table cell as center aligned
each_cell.Alignment = AlignmentType.Center;
// clear the existing contents of table cell
each_cell.Paragraphs.Clear();
// add new contents to paragraphs collection of table cell
each_cell.Paragraphs.Add(new Text("Updated Text"));
// set the font size information for table cell
each_cell.DefaultCellTextInfo.FontSize = 8;
}

In case it does not satisfy your requirements or you have any further query, please feel free to contact. We apologize for your inconvenience.

Nayyer,

Thank you very much for your help!! I used your code and it is now working.

I think the reason why my code wasn't working(added extra space between the header row and the 2nd row in the PDF thus making it look detached) was because the Text object I put into the Cell oject. I used to create new cells like below:

this.CreateHeaderColumn(headerRow,"Username",3);

private void CreateHeaderColumn(Row headerRow, string headerText, int headerColSpan)

{

Cell headerRowCell = headerRow.Cells.Add();

Text txt = new Text("" + headerText + "");

headerRowCell.Paragraphs.Add(txt);

txt.IsHtmlTagSupported = true;

txt.TextInfo.FontSize = 8;

txt.TextInfo.Alignment = AlignmentType.Center;

headerRowCell.ColumnsSpan = headerColSpan;

}

Hello Susan,

Thanks for using our products.

In order to set extra space, you may try setting the Header row height information using Row.FixedRowHeight property.

In case the problem still persists, or you have any further query, please feel free to contact.

The issues you have found earlier (filed as 24908 ) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.