Adjusting Table Width Automatically


#1

Tommy,



The feature list for 2.8.0.0 includes the following:



3) Adjusting table width automatically when the child table is too large is supported.



The only way I can get a child table to fit is by using the following code:



tab1.SetColumnWidth(1, tab1.GetMinColumnWidth(1)) ’ Column 1 contains the nested table



If I don’t use that line of code, the nested table will extend outside
the borders of its parent table. How do I make tables do this
automatically?



Thanks, Natan.


#2

Dear Natan,

Thanks for considering Aspose.Pdf.

Can you post the total coding block, I will test on my computer.

Best regards.


#3

Here’s the code I used (2.8.2.0):



Dim fsPdfFile As New FileStream(sPdfFile, FileMode.Create)

pdfDocument = New Pdf(fsPdfFile)

pdfSection = New Section(pdfDocument)

pdfDocument.Sections.Add(pdfSection)



’ Debug nested table test

Dim tab1 As Table = New Table()

tab1.FixedWidth = 400

tab1.ColumnWidths = “200 200”

tab1.DefaultCellBorder = New BorderInfo(BorderSide.All)

Dim row1 As Row = tab1.Rows.Add()

row1.Cells.Add(“left cell”)

Dim cell2 As Cell = row1.Cells.Add()

cell2.Padding.Bottom = 3

cell2.Padding.Top = 3

cell2.Padding.Left = 3

cell2.Padding.Right = 3

Dim tab2 As Table = New Table(cell2)

cell2.Paragraphs.Add(tab2)

tab2.FixedWidth = 300

tab2.ColumnWidths = “150 150”

Dim row21 As Row = tab2.Rows.Add()

Dim cell21 As Cell = row21.Cells.Add(“top cell”)

cell21.ColumnsSpan = 2

Dim row22 As Row = tab2.Rows.Add()

row22.Cells.Add(“left bottom cell”)

row22.Cells.Add(“right bottom cell”)

tab1.SetColumnWidth(1, tab1.GetMinColumnWidth(1))

pdfSection.AddParagraph(tab1)



pdfDocument.Close()


#4

This bug will be fixed in the next hotfix at this weekend.


#5

Thanks. So from this version on, can I assume that nested tables should never extend beyond the boundaries of parent tables?

I had written some code to prevent this from happening and I'd like to remove it for performance reasons.

Thanks, Natan.


#6

Yes but when nested table is larger than parent table's column, we can only extend column of parent table. We can't assure the parent table has appropriate column width then.


#7

hi, i have a similar issue:

when using Table.ColumnWidths = "50% 50%" in a NESTED table, the table does not display; but when using the syntax Table.ColumnWidths = "300 300" in a nested tabel, no problem.

Of course it would be nice to do this (all dynamic sizing of tables and nested tables within) :

For example:

Table masterTable = new Table();

masterTable.ColumnWidths = "100%";

sec1.Paragraphs.Add(masterTable);

Row row_1 = masterTable.Rows.Add();

Cell cell_1 = row_1.Cells.Add();

Table table_1 = new Table();

table_1.ColumnWidths = "50% 50%";

...


#8

Dear,

I have tested the above snippet code embedded in my scenario, it was fine and the bug wasn’t reproduced.

Could you please post the whole code block or send to william.wen@aspose.com?


#9

ok, here's the code (it does not work if the ColumnWidths are set to percentages, but if you comment that line of code, of course it works!) also works when you add a pixel unit, like "300 300"

Pdf pdf = new Pdf();

Section sec1 = pdf.Sections.Add();

sec1.PageInfo.PageHeight = PageSize.LetterHeight;

sec1.PageInfo.PageWidth = PageSize.LetterWidth;

sec1.PageInfo.Margin.Left = 20;

sec1.PageInfo.Margin.Right = 20;

Table masterTable = new Table();

masterTable.ColumnWidths = "50% 50%";

sec1.Paragraphs.Add(masterTable);

masterTable.Border = new BorderInfo((int) BorderSide.All, 0.5f, new Color(0,255,0));

Row row_1 = masterTable.Rows.Add();

Row row_2 = masterTable.Rows.Add();

Cell cell2 = row_2.Cells.Add("Testing");

///

Cell cell_1 = row_1.Cells.Add();

Table t_1 = new Table();

t_1.ColumnWidths = "25% 25%";

t_1.Border = new BorderInfo((int) BorderSide.All, 0.5f, new Color(255,0,0));

Row t_1_r_1 = t_1.Rows.Add();

Cell t_1_r_1_c_1 = t_1_r_1.Cells.Add("1Test_Cell1");

cell_1.Paragraphs.Add(t_1);

///

Cell cell_2 = row_1.Cells.Add();

Table t_2 = new Table();

t_2.Border = new BorderInfo((int) BorderSide.All, 0.5f, new Color(0,0,255));

Row t_2_r_1 = t_2.Rows.Add();

Cell t_2_r_1_c_1 = t_2_r_1.Cells.Add("2Test_Cell2");

cell_2.Paragraphs.Add(t_2);

pdf.Save("test.pdf",SaveType.OpenInBrowser,Response);


#10

Hi,

Which version of Aspose.Pdf was made reference to in your project? It
is not a new bug and has been resolved, Please download the latest
version and try again.