Tabel horizontal merge not working

Dear team,

I am using Aspose latest jar for document conversion . I have created a table containing horizontal merge, through aspose api ( cellformat.getHorizontalMerge()) am trying to get horizontal merge, but it is giving 0 for attached document. Am i doing anything wrong ? Is there any other method to get details about horizontal merge?

Thanks.
Anbu.S

Hi
Thank you for additional information. I would like to clarify a bit regarding horizontally merged cells. 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.
Here is simple code, which demonstrates the described things.

// Create empty document and DocumentBuilder object.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Configure DocumentBuilder
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = Color.Black;
// Build table, with simply wide cells.
// First row will contains simply wide cell and in MS Word it will look like merged.
builder.InsertCell();
builder.CellFormat.Width = 200;
builder.Write("This is simply wide cell");
builder.EndRow();
// Insert the second row
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.EndRow();
builder.EndTable();
// Insert few paragraphs between table.
builder.Writeln();
builder.Writeln();
// Build table, with merged cells.
// First row will contains merged cells.
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.Write("This is merged cells");
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
// Insert the second row
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.EndRow();
builder.EndTable();
// Save output document
doc.Save(@"Test001\out.doc");

In your particular case, the second row of your document contain only one wide cell instead of two merged cells. That is why you do not get CellMerge of this cell.
Best regards,

Hi Alexey,
Something has been broken between v10.2 and v11.0 with regards to the Horizontal Merge. We have working production code using the style of code you wrote above and it works as expected (v10.2). We have upgraded to v11 and the merge code no longer works. Putting in the

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

line of code now seems to have the effect of destroying the cell completely.
I have proved this by using your code above which also has the same problem.
This is a important regression bug for us and will seriously impact the release we are in the final stages of testing of. This is not the first regression bug we have found in this release. Please investigate this urgently.

Hi Glyn,

Thanks for your inquiry. I have tested the code provided by Alexey in his previous post with Aspose.Words v11.1.0 and was unable to reproduce any issue on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v11.1.0 and let us know how it goes on your side. I hope, this will help.

Moreover, I have attached the output document, i.e. generated on my side, here for your reference. Please let me know if I can be of any further assistance.

Best Regards,

Hi,
Thanks for your reply. I have spent a number of hours putting together an example solution to show you the problem and have isolated the issue to the line of code causing the problem.
We are not setting widths on cells and in 10.2 this didn’t cause a problem. In V11 it does.

See line 53 of default.aspx.cs

I have attached code which proves this. This test project is using the latest 11.1 code. The default.aspx has 2 buttons on it (Good, Bad). When you run ‘Good’ the width is set, ‘Bad’ doesn’t.
The Libraries folder of the attached code has both 10.2 and 11.1 versions. If you swap 11.1 to 10.2 you will seed the ‘Bad’ export working.

I don 't really want to have to set widths, so I’d prefer you guys get this working as for v10 .2

Hi Glyn,

Thanks for your inquiry. I have made a few corrections to your code. Please try using the following code snippet:

private static void GetBuiltDoc(Document doc, bool setWidth)
{
    List<string> headers = new List<string>();
    headers.Add("Header 1");
    headers.Add("Header 2");
    headers.Add("Header 3");

    List<string> details = new List<string>();
    details.Add("Detail 1A");
    details.Add("Detail 1B");
    details.Add("Detail 2A");
    details.Add("Detail 2B");
    details.Add("Detail 3A");
    details.Add("Detail 3B");

    DocumentBuilder builder = new DocumentBuilder(doc);
    // Configure DocumentBuilder
    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
    builder.CellFormat.Borders.Color = Color.Black;
    // THIS IS THE LINE THAT CAUSES THE PROBLEM. IF NO WIDTH IS SET IT GOES BANANAS!~~

    //if (setWidth) builder.CellFormat.Width = 100;
    builder.StartTable();
    builder.InsertCell();
    builder.Write("Header Row");

    foreach (string header in headers)
    {
        builder.InsertCell();
        builder.CellFormat.HorizontalMerge = CellMerge.First;
        builder.Write(header);
        builder.InsertCell();
        builder.CellFormat.HorizontalMerge = CellMerge.Previous;
    }
    builder.EndRow();

    Cell groupCell = builder.InsertCell();
    groupCell.CellFormat.HorizontalMerge = CellMerge.First;

    builder.Write("Group Row");
    foreach (string detail in details)
    {
        builder.InsertCell();
        builder.CellFormat.HorizontalMerge = CellMerge.Previous;
    }
    builder.EndRow();

    builder.CellFormat.HorizontalMerge = CellMerge.None;
    builder.InsertCell();
    builder.Write("Details Row");
    foreach (string detail in details)
    {
        builder.InsertCell();
        builder.Write(detail);
        builder.CellFormat.HorizontalMerge = CellMerge.None;
    }
    builder.EndRow();

    builder.EndTable();

}

I hope. this will help.

Best Regards,

Hi,

i copied the code to my project.

When i save the final document in a binary format and
reopen it again, then the information about ‘HorizontalMerge’ gets
lost.

using(MemoryStream mem = new MemoryStream())
{
    builder.Document.Save(mem, Aspose.Words.SaveFormat.Docx);
    byte[] result = mem.ToArray();
    using(MemoryStream mem2 = new MemoryStream(result))
    {
        Document resultDoc = new Document(mem2);
        resultDoc.Save("test.docx");
        Table wordTable = (Table) resultDoc.GetChild(NodeType.Table, 0, true);æÆ -> wordTable.Rows[1].Cells[0].CellFormat.HorizontalMerge is none
    }
}

Can you explain why ?

Regards,
Guido

Hi
Guido,

Thanks for the additional information. Yes, you’re right; the information about Horizontally merged cells is getting lost when saving to DOCX format. Well, please note that Aspose.Words tries to mimic the way Microsoft Word works. If you please generate a DOC file using the code in my previous post and re-save that DOC file to DOCX format using Microsoft Word and then print the horizontal and vertical merge types of cells in DOCX, you’ll find that the output produced by Aspose.Words and Microsoft Word are equivalent. So, I see no problem with Aspose.Words in here.

Best Regards,

Hi Awais,

in a previous Aspose.Words version we used the information about horizontal and vertical merge to move to a cell and write text via the builder.MoveToCell-Function.

But how do we know now which is the correct column in a row because we don’t have the merge-information anymore?

Additionally we are quite unhappy about changing the behavior and that we can’t rely on previous behavior and feared even worse changes in future versions.

Kind regards,
Guido

Hi
Guido,

Thanks for your inquiry and I am sorry to hear about the troubles you have encountered. But, could you please provide a little more information about the Aspose.Words’ version number for which there were no problems on your side previously? I will further look into the details of this problem and will keep informed of my findings.

Best Regards,

Hi Guido,

Thanks for your patience. It is to inform you that I have just logged a new issue in our issue tracking system as WORDSNET-7095. The title of this issue says ‘Aspose.Words no longer exports Cell merge information to DOCX properly’. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best Regards,

Hi Guido,

I’m sorry that the change in merged cell processing caught you unarmed. However I still think that it is a feature, not a bug because:

  1. It is a simpler concept to have variable number of cells per row, than having fake cells that you have to be careful with. Really you cant or should not do anything to cells other than the first one in case of cell merge. If your code relies on modifying non-first cells in merged case and it works, then it is undocumented behavior not intended to happen. That is how specs describe the situation.
  2. MS Word from version 2007 up has switched to “normalizing” cell merges and we started doing this recently. The legacy scheme is left for compatibility and is not recommended for use. You are likely to get these files as inputs even without Aspose.Words, so it is better to prepare in advance.

In spite of above points I apologize for changes to model behavior without corresponding evangelization to the public. I think we will have to introduce some additional paragraphs into corresponding documentation chapters or maybe create a blog post with background info as well as proper code snippets to enumerate cells. What do you think about this?

Hi Guido,

Just to let you know the issue linked in this thread has been closed as Not a Bug. The issue linked to this thread is now replaced with updating the documentation to describe this exact scenario.

Thanks,

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan