Table layout inconsistent when using fixed cell width and merged cells


#1

Hello,

I want to generate a table in a Word document with some merged cells in the first row, and the other rows containing data. I need to specify the column width explicitly (using the DocumentBuilder.CellFormat.Width property), but I get strange results.

Here is the code to isolate the problem:

Document Doc=new Document();

Doc.EnsureMinimum();

DocumentBuilder Db=new DocumentBuilder(Doc);

Db.InsertCell();

Db.CellFormat.HorizontalMerge = CellMerge.First;

//Uncomment this line to reproduce the bug: cells width in the whole table are ‘messed up’

//Db.CellFormat.Width=350;

Db.Write(“Text in the merged cells.”);

Db.InsertCell();

//This cell is merged to the previous and should be empty.

Db.CellFormat.HorizontalMerge = CellMerge.Previous;

Db.InsertCell();

Db.CellFormat.HorizontalMerge = CellMerge.Previous;

Db.InsertCell();

Db.CellFormat.HorizontalMerge = CellMerge.Previous;

Db.InsertCell();

Db.CellFormat.HorizontalMerge = CellMerge.Previous;

Db.EndRow();

Db.InsertCell();

Db.CellFormat.HorizontalMerge = CellMerge.None;

Db.Write(“Text in one cell.”);

Db.InsertCell();

Db.Write(“Text in another cell.”);

//Uncomment this line to reproduce the bug: cells width in the whole table are ‘messed up’

//Db.CellFormat.Width=200;

Db.InsertCell();

Db.Write(“Text in another cell.”);

Db.InsertCell();

Db.Write(“Text in another cell.”);

Db.InsertCell();

Db.Write(“Text in another cell.”);

Db.EndRow();

Doc.Save(@“c:\test.doc”);


If I execute this code as is (without uncommenting the cell width initializing lines), the ouput Word document is OK.

When I uncomment the line ‘Db.CellFormat.Width=350;’, the output document contains the first cell of the first row with a tiny width, and the cells in the row below are far too wide. (I attached the output doc)

When I uncomment the line ‘//Db.CellFormat.Width=200;’, the output document contains a table with strange layout, too.
I use Aspose.word 3.1.1.0 (but it was the same with 3.1.0.0), and Word 2003 as viewer.


#2

Hi,

Thank you for your request, we have logged the issue and will try to fix it asap, please check back in several days.


#3

The easiest thing for you to do here is to create just one cell in the top row, don't need to use merged cells.

DocumentBuilder db = new DocumentBuilder();

db.InsertCell();

db.CellFormat.Width =350;

db.Write("Text in the merged cells.");

db.EndRow();

db.InsertCell();

db.CellFormat.Width = 50;

db.Write("Text in one cell.");

db.InsertCell();

db.CellFormat.Width = 60;

db.Write("Text in another cell.");

db.InsertCell();

db.CellFormat.Width = 70;

db.Write("Text in another cell.");

db.InsertCell();

db.CellFormat.Width = 80;

db.Write("Text in another cell.");

db.InsertCell();

db.CellFormat.Width = 90;

db.Write("Text in another cell.");

db.EndRow();

TestUtil.Save(db.Document, @"Other\TestDefect545 Out.doc");


You still can create merged cells if you want to, but you need to take care of width of each cell. It's just the way merged cells work in MS Word.


#4

Yes, it works, thank you