Hello,
I need merge cells in the table. I found a problem in this feature.
I have created a test app:
VisioTest.zip (18.6 KB)
Please run it and press the button. In the destination folder you should find a word.docx file with the table.
The table has 3 columns and 2 rows. In the first row all free cells are merged correctly. But in the second row, I have merged second and third cell but as you can see, the last cell is missing.
image.png (14.4 KB)
Thanks
@ManMasterDevelopment
Please change the width of cell to 50% as below to get the desired output.
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50);
We suggest you please read the following article.
Working with Columns and Rows
This doesn’t solve the problem. If I set width to 50% then both cells in the second row will be equal. But my idea is to have first column 33% width and second column (merged) with 66% width.
Another problem is that in the first row merging works good (we have one big cell with width 100% without any modification) but in the second row, third cell disappeared
@ManMasterDevelopment
Please use the following code example to achieve your requirement. Hope this helps you.
var word = new Aspose.Words.Document();
var builder = new DocumentBuilder(word);
builder.StartTable();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100);
var cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.First;
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
cell = AddCell(builder);
cell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(33);
cell = AddCell(builder);
cell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(69);
builder.EndRow();
builder.EndTable();
word.Save(MyDir + "20.8.docx", SaveFormat.Docx);
Your example of course works in this test application but our problem is more complicated. Basically we are creating some kind of converter to Word format. So in this case the problem is that our Source document has three columns and some merged cells. So we cannot easly replace them to 2 columns table (as we have in your example). We need to have a working merging with 3 columns.
From my point of view there is a bug in aspose merging mechanism. If you change CellMerge.Previous to None in the third cell in the first row, everyhing looks correct and the third cell in second row is visible now.
Here is the code:
var word = new Aspose.Words.Document();
var builder = new DocumentBuilder(word);
builder.StartTable();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(33);
var cell=AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.First;
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.None;//this has impact on the second row!
builder.EndRow();
cell=AddCell(builder);
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.First;
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
builder.EndTable();
So visibility of the cell from second row is depends on the first row, which from my point of view is incorrect.
Basically please try to fix the problem with merging. Keep in mind that we are creating a converter so we are not able to easily apply a fix for a specific situation. We need a stable API
If you need more info, please let me know.
Thanks!
@ManMasterDevelopment
We have executed your code using the latest version of Aspose.Words for .NET 20.8 and output looks good to us. Please check the attached output document. output.zip (6.6 KB)
If you still face problem, please ZIP and attach your expected output Word document. We will then provide you more information on it.
Your output document was generated by my second code where I said that this looks good.
If you run the test app I send in the first post you will get this result:
wrong.zip (16.4 KB)
The third column in second row is missing.
I expect this output:
good.zip (21.1 KB)
@ManMasterDevelopment
By Microsoft Word design, rows in a table in a Microsoft Word document are completely independent. It means each row can have any number of cells of any width. So, if you imagine first row with one wide cell and second row with two narrow cells, then looking at this document the cell in the first row will appear horizontally merged. But it is not a merged cell; it is just a single wide cell.
Another perfectly valid scenario is when the first row has two cells. First cell has CellMerge.First and second cell has CellMerge.Previous, in this case it is a merged cell. In both cases, the visual appearance in MS Word is exactly the same. Both cases are valid.
In your case, we suggest you please create the wide cells as shown below. Hope this helps you.
DocumentBuilder documentBuilder = new DocumentBuilder();
Aspose.Words.Tables.Table table = documentBuilder.StartTable();
documentBuilder.InsertCell();
documentBuilder.CellFormat.Width = 100;
documentBuilder.Write("100");
documentBuilder.InsertCell();
documentBuilder.CellFormat.Width = 100;
documentBuilder.Write("100");
documentBuilder.InsertCell();
documentBuilder.CellFormat.Width = 100;
documentBuilder.Write("100");
documentBuilder.EndRow();
documentBuilder.InsertCell();
documentBuilder.CellFormat.Width = 200;
documentBuilder.Write("200");
documentBuilder.InsertCell();
documentBuilder.CellFormat.Width = 100;
documentBuilder.Write("100");
documentBuilder.EndRow();
table.PreferredWidth = PreferredWidth.FromPoints(300);
documentBuilder.EndTable();
documentBuilder.Document.Save(MyDir + "20.8.docx");
If you still want to use your old code, please set CellFormat.HorizontalMerge as CellMerge.None before starting new row.
var word = new Aspose.Words.Document();
var builder = new DocumentBuilder(word);
builder.StartTable();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(33.3);
var cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.First;
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
cell.CellFormat.HorizontalMerge = CellMerge.None;
cell = AddCell(builder);
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.First;
cell = AddCell(builder);
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
builder.EndTable();
word.Save(MyDir + "output.docx");