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

Free Support Forum - aspose.com

GetMaxColumnWidth and NewLines

The GetMaxColumnWidth() method does not honor a NewLine character when doing its calculation. The scenario is that we have a table that we want the column headers to wrap but none of the row data, so we cannot use the GetMinColumnWidth() method. To do this we have constructed the column header text with a NewLine character where we want the wrap to occur. Then we want the GetMaxColumnWidth() method to compute the optimal column widths for the table. The report produce does have the table column headers wrapped correctly but the columns with the wrapped headers are to wide.

By the way GetMinColumnWidth() does not break on a NewLine character either.

Hello Charles,

Thanks for using our products.

Can you please share some code snippet so that we can test the scenario at our end. We apologize for your inconvenience.

Please refer to <A href="</FONT></A>. The code in that issue is the same code being used here.</SPAN> </DIV>

Hello Charles,

Thanks for your patience.

I have tried replicating the issue using following code snippet and before I have made some further understanding, I would like to have your feedback over this matter. Please take a look over two attached PDF documents that I have generated with following code snippet. When using GetMaxColumnWidth(....) method, the Header of the table is honoring New Line character but there is plenty of extra blank space in header cell. However when using GetMinColumnWidth(....) method, the header has proper width but its also wrapping the contents of table body contents. I have tested the scenarios using Aspose.Pdf for .NET 5.4.0


DataTable dt = new DataTable("Employee");
dt.Columns.Add("Employee_ID #$NL Original", typeof(Int32));
dt.Columns.Add("Employee_Name #$NL Jr.", typeof(string));

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

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

//Create a Table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
//Add the Table object in the paragraphs collection of the section
//Set column widths of the table
tab1.ColumnWidths = "40 100 100 100";
//Set default cell border of the table using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo((int) Aspose.Pdf.BorderSide.All,0.1F);
//Import data into the Table object from the DataTable created above

for (int columnIndex = 0; columnIndex < dt.Columns.Count; columnIndex++)
tab1.SetColumnWidth(columnIndex, tab1.GetMaxColumnWidth(pdf1, columnIndex));

//Get 1st row from the table
Aspose.Pdf.Row row1 = tab1.Rows[0];
//Iterate through all cells in the row and set their background color to blue
foreach(Aspose.Pdf.Cell curCell in row1.Cells)
curCell.BackgroundColor = new Aspose.Pdf.Color("Pink");

//Save the Pdf

I need the column headers to wrap but not the cells in the body of the table, so GetMinColumnWidth(…) will not work for me. I need to use GetMaxColumnWidth(…) and force the header columns to wrap, but when I do that GetMaxColumnWidth(…) does not honor the newline and computes the max width of the column as if the column header was one line. This causes the column to be to wide. In some of the reports I need to generate the wider columns is causing the report to bleed off the right side of the page (see attached file).