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

Free Support Forum - aspose.com

Merging Cells creates a column only the size of the first column

I have a word doc with a single table in it that I copy and then add rows. At the end of the code I merge the table's first row to center the title. When I do this in 11.5 the cell is only as wide as the first column not the entire span of all the merged columns. I even tried your MergeCells() method in the documentation and got the same result. Here is my code for merging a row's cells. I have also attachment my word file that I start with and the resulting word file.

Thanks for any help or fix.

Bryan

 public void MergeRow(Aspose.Words.Tables.Row poRow, int iStart, int iCols)
        {
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["TableRowTitleMerged"]))
            {
                try //merge top and bottom with number of columns
                {
                    int count = 0;
                    foreach (Aspose.Words.Tables.Cell cell in poRow)
                    {
                        if (count == iStart)
                        {
                            cell.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.First;
                        <span style="color: red;">break</span>;
                    }
                    <span style="color: red;">if</span> (count > iStart && count < (iStart + iCols))
                    {
                        cell.CellFormat.HorizontalMerge = Aspose.Words.Tables.<span style="color: rgb(43, 145, 175);">CellMerge</span>.Previous;
                    }
                    count++;
                }
            }
            <span style="color: red;">catch</span> (<span style="color: magenta;">Exception</span> ee)
            {
                System.Diagnostics.<span style="color: magenta;">Trace</span>.WriteLine(ee.Message);
                System.Diagnostics.<span style="color: magenta;">Trace</span>.WriteLine(ee.StackTrace);
            }
        }
    }v</pre>

Hi Yuri,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please omit break statement from the IF block, setting CellMerge property for first cell. As execution in MergeRow() method terminates after setting CellMerge property of first cell. Hopefully it will serve your purpose.

Please feel free to contact us for any further assistance.

Best Regards,

I have three other overloads of code like the MergeRow above. None of them work. I copied the wrong one into the post. In any case, the break code is only fired if the merge starts at the beginning of a row. The MergeRow above is used to merge cells 2,3,4 out of 1,2,3,4,5 columns. It worked before 11.5. The code below which also worked prior to 11.5 merges the entire row with the same results as the files attached to the first post.

public void MergeRow(Aspose.Words.Tables.Row poRow, int iStart, int iCols)
{
if (Convert.ToBoolean(ConfigurationManager.AppSettings["TableRowTitleMerged"]))
{
try //merge top and bottom with number of columns
{
bool isFirst = true;
foreach (Aspose.Words.Tables.Cell cell in poRow)
{
if (isFirst)
{
cell.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.First;

}
else
{
cell.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.Previous;
}

}
}
catch (Exception ee)
{
System.Diagnostics.Trace.WriteLine(ee.Message);
System.Diagnostics.Trace.WriteLine(ee.StackTrace);
}
}
}

I have uninstalled all the Aspose packages on my machine and reinstalled 11.5. I have also copied all the dlls into my Asp.net bin folder. I am now trying what you did which was just merging the row without adding any rows of data. If that works then it is either the appending of rows to the table or the autofit causing the problem.

Hi Yuri,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry for the inconvenience faced. Could you please share a sample solution and input document causing the issue? So we will reproduce the issue at our end and investigate further to suggest you accordingly.

Please feel free to contact us for any further assistance.

Best Regards,

Has there been any update on this issue? Or any workaround?


We are using 11.4 right now and testing with the second example of code from http://www.aspose.com/docs/display/wordsnet/Working+with+Merged+Cells

we end up with an ā€œLā€ shaped table where the merged cells at the top only span over the first column.

Hi,


Thanks for your inquiry. Please first upgrade to the latest version of Aspose.Words i.e. 11.8.0. You can download it from the following link:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

Then please try running the following code on your side:

Document doc = new
Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.Write("Text in merged cells.");

builder.InsertCell();

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

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.EndRow();

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

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

builder.InsertCell();

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

builder.EndRow();

builder.EndTable();

doc.Save(@"C:\Temp\out.doc");


I hope this helps.

Best Regards,