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

Free Support Forum - aspose.com

Calculate unknown number of columns in 100% width table

More information on my task: I am using Aspose.Pdf (v 5.3.0.0) to generate a A4 Page PDF version of a dynamic report.xml structure retrieved from database. This report should contain on each page a header title and a simple footer. The content will consist of a unknown number of elements (in this case extensive information on a jobseeker CV and such). Each element should start on a new page. For each element there could be a list of subcollections that should be put in a horizontal tables (100%) with descriptive table headers.


So my problem is I havent found a simple solution to let the column width of data tables be automatically calculated. while still keeping the table at a 100%.

I have looked into the following:
table1.ColumnWidths = “70 2cm”;


But I dont know how many columns my table will have. I have also tried using this one:

//Call GetMinColumnWidth and pass the column number whose minimum width is needed

float width = tab1.GetMinColumnWidth(pdf, 0);

//Call SetColumnWidth and pass the column number with minimum width

tab1.SetColumnWidth(0,width);


I try this method on my table, column 0 and I get “Invalid index in Cells indexer: 0”. Here I also do not know the number of columns.

What I am looking for is a HTML table behaviour where the columns are adjusted to the content. Do you have any advise on achieving this behaviour?

Hi,

Thanks for using our products.

In order to accomplish your requirement, please try using the following code snippet in which you can specify the number of columns against Column_Counter variable and their width will automatically be adjusted to get equal width in table keeping the aspect ratio of table as 100%. I have tested it with v5.3.0 and the output seems to be correct.

[C#]

// create Pdf Instance
Pdf pdf = new Pdf();
// create section object and add it to pdf object
Section sec1 = pdf.Sections.Add();

// create a table instance
Table mytable = new Table();
// specify the default border for table object and set its color as Navy
mytable.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 1f, new Aspose.Pdf.Color("Navy"));
// specify the border information for table object
mytable.Border= new BorderInfo((int)BorderSide.All, 1f, new Aspose.Pdf.Color("Navy"));
// define initial width information for table cells|
mytable.ColumnWidths = "100 100";

// create a row object and add it to table
Row row1 = mytable.Rows.Add();
// create loop to add specify the number of columns to be added to table row
for (int Column_Counter = 1; Column_Counter <=7; Column_Counter++)
{
// create a cell object and add it to table row
Cell Cell = row1.Cells.Add("Cell (1,"+ Column_Counter.ToString() +")");
}

// define the variable to keep record of number of columns in table
int TableColumn = 0;
// traverse through each table cell in row object
foreach (Cell InnerCell in row1.Cells)
{
// specify the width information for each column based on section objects margin and width values
// set the width value as, get the total width of section and subtract left and right margin and divide
// it with total number of cells in a particular table row
mytable.SetColumnWidth(TableColumn, (sec1.PageInfo.PageWidth - sec1.PageInfo.Margin.Left - sec1.PageInfo.Margin.Right)/ row1.Cells.Count);
// increase the value of variable holding the column count information
TableColumn += 1;
}

// add table to paragraphs collection of section
sec1.Paragraphs.Add(mytable);
// save the resultant PDF document
pdf.Save("d:/pdftest/DynamicTableWidth.pdf");

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

Hi again, the problem is that the cells cannot have equal width. They need to be proportional to the width of the content. For example some columns will only have text like “Yes” “No”, while some will contain larger sentences. To set these larger columns to the equal width of a small cell will look unprofessional. Is there no other way to achieve this?

Hi,

Sorry for delay in response.

Please try using the following code snippet to get the column width information at runtime and the width information will depend upon the contents present inside table cell.

[C#]

Pdf pdf = new Pdf();
Aspose.Pdf.Section sec = pdf.Sections.Add();
//Create a table object and add it to the paragraphs collection of the section
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
sec.Paragraphs.Add(tab1);
//Set the column widths and default cell border of the table
tab1.ColumnWidths = "60 100 100 80 50 80";
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo((int)Aspose.Pdf.BorderSide.All, 1F);
//Prepare an array of string values to be added to table
string[] darr = new string[] { "Sample Text", "Hi", "Hello", "YES", "NO", "© Aspose 2002-2011. All Rights Reserved" };
//Import the contents of the array created in above step
tab1.ImportArray(darr, 0, 0, true);

// define the variable to keep record of number of columns in table
int TableColumn = 0;
foreach (Cell InnerCell in tab1.Rows[0].Cells)
{
//Call GetMaxColumnWidth and pass the column number whose minimum width is needed
float width = tab1.GetMaxColumnWidth(pdf, TableColumn);
//Call SetColumnWidth and pass the column number with minimum width
tab1.SetColumnWidth(TableColumn, width);
// increase the value of variable holding the column count information
TableColumn += 1;
}
//save the resultant PDF
pdf.Save(@"d:/pdftest/ColumnWidth_Test.pdf");

You may also try visiting the following link for information on Achieve Minimum Column Width of a Table

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

Hi, do you really need to use the importarray function for this functionality to work? We are looping through an an xml collection and creating rows+cells on the fly.


If I have some sentences inside one cell would this content span over multiple lines or will it be all in one line?

Also how do I achieve 100% width of the table?