Free Support Forum - aspose.com

Splitting a dynamic table into multiple tables based on page/cell width

Hi,

I am trying a proof of concept to convince my client to purchase this tool for document generation. I am stuck up with the below issue.

I have a requirement where i have to generate a table dynamically. No of Rows and Columns in the table may vary in the run time. Based on the cell width and page width i've to split the tables also. Below are the two possible scenarios. I've the entire content of the table in a datatable. I am not able to decide upon the approach. Let me know the best approach for this?

Scenario 1:

My table has only 3 columns and hence it fits into the page width.

Performance Option - Col 1

Col 2

Col 3

Base unit

Nameplate unit kW (kW)

411.00

411.00

Nameplate RLA (A)

708.00

708.00

Scenario 2:

My table has 6 columns but only 4 columns fit into the page width and hence my table should be split into two tables

1) Table 1 - with cols 1,2,3,4

2) Table 2 - with cols 1,5,6

Note: Column 1 should be repeated in both the tables

Performance Option - Col 1

Col 2

Col 3

Col 4

Base unit

Nameplate unit kW (kW)

4o1.00

411.00

422.0

Nameplate RLA (A)

708.00

718.00

789.0

Performance Option - Col 1

Col 5

Col 6

Base unit

Nameplate unit kW (kW)

431.00

441.00

Nameplate RLA (A)

728.00

738.00

Thanks and Regards,

SakthiVenkatesh.

Hi

Thanks for your interesting inquiry. I think that you can achieve this using Aspose.Words. I create code example for you.

public void TestInsertTable_100821()

{

//Create table with data

DataTable table = new DataTable();

table.Columns.Add("Col1");

table.Columns.Add("Col2");

table.Columns.Add("Col3");

table.Columns.Add("Col4");

table.Columns.Add("Col5");

table.Columns.Add("Col6");

for (int i = 0; i < 10; i++)

{

DataRow row = table.NewRow();

row[0] = "Nameplate unit kW (kW)";

row[1] = i.ToString() + "0";

row[2] = i.ToString() + "1";

row[3] = i.ToString() + "2";

row[4] = i.ToString() + "3";

row[5] = i.ToString() + "4";

table.Rows.Add(row);

}

//create new document

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//insert table

InsertTable_100746(builder, table);

//save document

doc.Save(@"322_100821_sakthivenkatesh\out.doc");

}

void InsertTable_100746(DocumentBuilder builder, DataTable table)

{

//get page width

PageSetup pageSetup = builder.CurrentSection.PageSetup;

double width = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;

//get count of clumns for table

int counter = 4;

double firstColunm = 0.5;

if (counter > table.Columns.Count)

{

counter = table.Columns.Count;

}

//start table

builder.StartTable();

//insert header

builder.PushFont();

builder.Font.Bold = true;

for (int j = 0; j < counter; j++)

{

builder.InsertCell();

if (j == 0)

{

builder.CellFormat.Width = width * firstColunm;

}

else

{

builder.CellFormat.Width = width * (1 - firstColunm) / (counter - 1);

}

builder.Write(table.Columns[j].ColumnName);

}

builder.PopFont();

builder.EndRow();

//insert rows

foreach (DataRow row in table.Rows)

{

for (int i = 0; i < counter; i++ )

{

builder.InsertCell();

if (i == 0)

{

builder.CellFormat.Width = width * firstColunm;

}

else

{

builder.CellFormat.Width = width * (1 - firstColunm) / (counter - 1);

}

builder.Write(row[i].ToString());

}

builder.EndRow();

}

builder.EndTable();

builder.InsertBreak(BreakType.ParagraphBreak);

//remove inserted columns from DataTable

for (int j = 1; j < counter; j++)

{

table.Columns.Remove(table.Columns[1].ColumnName);

}

//Call this method again if table has columns.

if (table.Columns.Count != 1)

{

InsertTable_100746(builder, table);

}

}

I hope that this code will help you to solve this task.

Best regards.

Hi,

Thanks for the quick response. I appreciate your help.

I've one other issue. Currently you have assumed that maximum of 4 columns can be put in a table. [int counter = 4;]. But we cannot hardcode the max no of columns in a table. Width of a cell will be based on the content of the cell.

For example, look at the below two scenarios where one table has just 2 columns [one of the cell value is lengthier] and the other has 6 columns [all the cell values are smaller]. So we should dynamically set the cell width based on its content.

Is there a way to achieve this?

Table with only two columns [less than 4 columns]:

Label2

E1

General

Unit size (Number)

250

Unit type Pressure Equi (No)

Pressure code CE Directive (Pressure Equipment)

Refrigerant (Number)

R407C

Table with six columns [greater than 4 columns]:

Label2

E1

E2

E3

E4

E5

General

Unit size

250

300

301

500

100

Unit type

STANDARD

SUPER_QUIET

SUPER_QUIET

SUPER_QUIET

SUPER_QUIET

Refrigerant

R407C

R407C

R408C

R408C

R417C

Cooling

63.8

77.5

67.2

77.5

87.2

Thanks,

SakthiVenkatesh.

Hi

Thanks for your inquiry. I think that you should loop through the all rows in your DataTable and calculate max length of the content in the every column. Then you can calculate amount and width of columns in the word table.

Best regards.

Hi,

Sorry for the late response. Got stuck up with work.

Thanks for the suggestion. I will check out and let you know.

Regards,

SakthiVenkatesh.