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

Free Support Forum - aspose.com

Adding to table one cell at a time messes up the table

Hi,

My application makes a document with two tables in each section, one in the header and one in the body. First it creates the entire body table and creates the header table with a single cell in some rows. It then adds cells of different sizes to the one-cell rows. This last step jumbles the cell sizes in the whole table. See the attached console app made in VS2015 for details. This worked through Words 15.7.0.0 but some subsequent release broke it. The current output is \bin\debug\Example.docx. The desired output, made with Words 15.7.0.0 is \bin\debug\Produced with 15.7.0.0.docx.



Sometimes my app puts both tables into the body and then the tables come out merged. Is that intended behavior? With Words 16 the jumbling extends into the second part of the merged table.

Thanks,

Bill Below

Hi Bill,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-13305. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.
bbelow:
Sometimes my app puts both tables into the body and then the tables come out merged.
If you insert two consecutive tables in the header/body of section, the tables will be merged. This is expected behavior of Aspose.Words. MS Word does the same.

Thanks Tahir,

As I was working on the demo console app it occurred to me that since the table width is specified by percent, the column widths should be also. I reworked the demo app to try it out and found that it failed in a way similar (or maybe the same as the. I have attached Program.cs which you can drop into the console app you already have. I think the comments in the code will make it clear what to do. This one fails with version 15.7 also.

Thanks,

Bill Below

Hi Bill,

Thanks for sharing the detail. We have logged this detail in our issue tracking system. We will update you via this forum thread once this issue is resolved. We apologize for your inconvenience.
Hi Bill,

Thanks for your patience. The issue (WORDSNET-13305) has been resolved in latest version of Aspose.Words for .NET 16.3.0. Please use following modified code example to get the required output.

int maxtier = 3;

Document doc = new Document();

var builder = new DocumentBuilder(doc);

var ps = builder.PageSetup;

ps.PaperSize = Aspose.Words.PaperSize.Letter;

ps.Orientation = Aspose.Words.Orientation.Landscape;

ps.TopMargin = 36.0;

ps.BottomMargin = 36.0;

ps.LeftMargin = 36.0;

ps.RightMargin = 36.0;

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

Table mtab = new Table(doc);

Row fullrow = MakeTableRow(doc);

for (int k = 0; k < maxtier; k++)

{

if (k < maxtier - 1)

{

var row = new Row(doc);

var cell = new Cell(doc);

cell.CellFormat.PreferredWidth = PreferredWidth.FromPoints(180);

row.Cells.Add(cell);

mtab.Rows.Add(row);

}

else

mtab.Rows.Add(fullrow.Clone(true));

}

// the following 4 lines cause the problem

AddCell(doc, mtab, 0, 540);

AddCell(doc, mtab, 1, 180);

AddCell(doc, mtab, 1, 180);

AddCell(doc, mtab, 1, 180);

// add the table into the document

builder.CurrentStory.AppendChild(mtab);

NodeCollection alltabs = builder.CurrentStory.Tables;

int inx = alltabs.IndexOf(mtab);

mtab.RightPadding = 0.0;

mtab.LeftPadding = 0.0;

mtab.Alignment = TableAlignment.Left;

mtab.AllowAutoFit = false;

// Aspose.Words Call AutoFit(FixedColumnWidths) to update table structure from cell widths after the table construction is complete (no more cells are added).

mtab.AutoFit(AutoFitBehavior.FixedColumnWidths);

// Aspose.Words Set table preferred width after AutoFit() because AutoFit() resets table width to Auto.

mtab.PreferredWidth = PreferredWidth.FromPercent(100);

builder.MoveTo(builder.CurrentSection.Body.FirstParagraph);

Table btab = new Table(doc);

Row brow = MakeTableRow(doc);

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

btab.Rows.Add(brow.Clone(true));

btab.AutoFit(AutoFitBehavior.FixedColumnWidths);

btab.Alignment = TableAlignment.Left;

btab.AllowAutoFit = false;

builder.CurrentSection.Body.AppendChild(btab);

doc.Save(MyDir + "Out v16.3.0.docx");

bbelow:
As I was working on the demo console app it occurred to me that since the table width is specified by percent, the column widths should be also. I reworked the demo app to try it out and found that it failed in a way similar (or maybe the same as the. I have attached Program.cs which you can drop into the console app you already have. I think the comments in the code will make it clear what to do. This one fails with version 15.7 also
We have logged this problem in our issue tracking system as WORDSNET-13425. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.
Hi Bill,

Thanks for your patience. Regarding WORDSNET-13425, there are two ways to change your code to make the expected table structure.

1) If the resulting table should have absolute cell preferred widths, then it makes sense to specify absolute preferred widths from the beginning. This will be the same scenario shared for WORDSNET-13305.

2) If the resulting table should have the originally specified cell preferred widths in percent units, then cell merges should be set explicitly during table construction. In this case there is no need to call Table.AutoFit method after table's construction.