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

Free Support Forum - aspose.com

Issue With Manual Table Creation with multiple levels and Cell Merging

I am trying to create a word doc that has a table in it that I am creating manually in the object model that utilizes a number of merged cells, however I can not seem to get it to work correctly.


I understand the idea of marking the first cell as Merge First and then the following items as Previous however upon saving the document the cells do not reflect the size/layout I would think I had set.

I have attached a word doc that displays the format I am trying to create, could you please show me an example on how to generate this output. ( You do not have to worry about the heading styles as that is easy, more so just the cell merging.)

Thank you.

Update on this:


I reviewed the xml generated for the docx file and it seems that for some reason Aspose.Words is combining my “columns” when there isn’t content in the merged cells.

For Instance in my example I have 8 cells total, but on the first line I merge all 8 into one, I do merging similar to this on the next few rows leaving more and more “space cells” on left in order to generate the desired look. But when I generate the docx file it reduces the <w:gridCol … /> to only 4?

I stepped through my code and rows definitely have 8 cells in them.

Please advise.

Hi
Patrick,


Thanks for your inquiry and sorry for the delayed response. I am working over your query and will get back to you as soon as possible.

Best Regards,

Hi Patrick,


Thanks for your patience. Could you please try using the latest version of Aspose.Words i.e. 11.5.0 and let us know how it goes on your side? You can download it from the following link:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

Moreover, I have attached here a DOCX document, that is generated by using the following code snippet on my side, here for your reference.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Build an 8x8 empty Table

builder.StartTable();

for (int x = 0; x < 8; x++)

{

builder.InsertCell();

builder.InsertCell();

builder.InsertCell();

builder.InsertCell();

builder.InsertCell();

builder.InsertCell();

builder.InsertCell();

builder.InsertCell();

builder.EndRow();

}

builder.EndTable();

// Merge cells

Table tab = doc.FirstSection.Body.Tables[0];

for (int i = 0; i < tab.Rows.Count; i++)

{

Row r = tab.Rows[i];

Cell c1 = r.Cells[i];

c1.CellFormat.HorizontalMerge = CellMerge.First;

for (int j = i + 1; j < tab.Rows.Count; j++)

{

Cell c2 = r.Cells[j];

c2.CellFormat.HorizontalMerge = CellMerge.Previous;

}

}

doc.Save("C:\\test\\out.docx");


Please see the following screen shot; there are exactly eight w:gridCol nodes in XML:



Best Regards,

Thanks for the reply.


Does this still work for you if you set widths on the first few cells when they are used as spacer cells? (see my example docx).

The odd thing is if I save to HTML it looks great, if I save to Docx it gives me a crazy layout. I wish I could just serialize my Aspose.Word.Document and give you the object before I saved to docx so you could see the issue. Reproducing the code is kind of an issue. I’ll see if I can make a dumbed down version to show the issue. I do need to find a fix for this today.

Thanks,

Andy

Hi Andy,


Thanks for the additional information.

I think, in this case, you should just call Document.UpdateTableLayout method before saving to DOCX format. It updates widths of cells and tables in the document according to their preferred widths and content. You do not normally need to call this method as cell and table widths are maintained automatically; however, you can call this method only in rare cases where you confirmed that tables appear incorrectly laid out in the output document.

I hope, this will help.

Best Regards,

If you make a new project open the docx I provided in original post UpdateTableLayout and save it as docx you will see what I am getting when I try to build this manually.


It ends up loosing the merged columns making it look like stairs.

here’s the basic code I am writing in my test console app

Aspose.Words.Document doc = new Aspose.Words.Document(Path.Combine(Environment.CurrentDirectory, “Document.docx”));

doc.UpdateTableLayout();
doc.Save(@“c:\test.docx”, Aspose.Words.SaveFormat.Docx);

Please advise soon,

thanks,

Andy

Here’s some simple code to also see the issue.


Aspose.Words.Document doc = new Aspose.Words.Document();<o:p></o:p>

Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc);

doc.FirstSection.Body.AppendChild(table);

//AD FIRST ROW WITH SOME COLORED SPACERS FOR VIEWING PLEASURE

Aspose.Words.Tables.Row row = new Aspose.Words.Tables.Row(doc);

for (var i = 0; i < 6; i++)

{

var spaceCells = new Aspose.Words.Tables.Cell(doc);

spaceCells.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(15);

spaceCells.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Red;

row.Cells.Add(spaceCells);

}

//A CONTENT CELL

var labelCell = new Aspose.Words.Tables.Cell(doc);

labelCell.Paragraphs.Add(new Aspose.Words.Paragraph(doc));

labelCell.FirstParagraph.AppendChild(new Aspose.Words.Run(doc, "Label"));

row.Cells.Add(labelCell);

//ANOTHER CONTENT CELL

var valueCell = new Aspose.Words.Tables.Cell(doc);

valueCell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50);

row.Cells.Add(valueCell);

table.Rows.Add(row);

//ADD ANOTHER CELL THAT HAS 8 CELLS with the first 7 merged (I normally would do all 8 but I tried an end cell to see if it behaved differently)

row = new Row(doc);

//First merged cell

labelCell = new Aspose.Words.Tables.Cell(doc);

labelCell.Paragraphs.Add(new Aspose.Words.Paragraph(doc));

labelCell.FirstParagraph.AppendChild(new Aspose.Words.Run(doc, "Title"));

labelCell.CellFormat.HorizontalMerge = CellMerge.First;

row.Cells.Add(labelCell);

//6 other merged cells

for (var i = 0; i < 6; i++)

{

var spaceCells = new Aspose.Words.Tables.Cell(doc);

spaceCells.CellFormat.HorizontalMerge = CellMerge.Previous;

row.Cells.Add(spaceCells);

}

//An end cap cell to see what it does

var endCapCell = new Aspose.Words.Tables.Cell(doc);

endCapCell.CellFormat.HorizontalMerge = CellMerge.None;

endCapCell.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(15);

row.Cells.Add(endCapCell);

table.Rows.Add(row);

//CHECK THAT ALL ROWS HAVE 8 CELLS

foreach (Aspose.Words.Tables.Row testRow in table.Rows)

{

int count = testRow.Cells.Count;

if (count != 8)

throw new Exception("Not 8");

}

//SAVE THE DOCx

doc.Save(@"c:\test.docx", Aspose.Words.SaveFormat.Docx);

Just to note this does output 8 grid columns. But the a row with 8 tc for the first and a row with 2 w:tc for the second row but no <w:gridSpan w:val=“7”/>

Any updates on this?

Hi Andy,


Thanks for your inquiry.
Andy:
It ends up loosing the merged columns making it look like stairs.
I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system. The issue ID is WORDSNET-6683. Your request has been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best Regards,

Do you have a time frame for this fix?

Hi Andy,

Thanks for your patience.

I am afraid this issue WORDSNET-6683 has now been postponed till a later date due to some other important issues and new features. We will inform you as soon as there are any further developments.

We apologize for your inconvenience.

Any kind of rough time frame? 1 week? 1 Month?


Thanks!

Hi Andy,

Thanks for your query. The issue WORDSNET-6683 had been postponed due to some other important issues and new features. However, I have asked for the ETA of this issue from our development team. As soon as, any information is shared by them, I will be more than happy to share that with you.

We appreciate your patience.

Hi Andy,

I have received response from our development team about the ETA of WORDSNET-6683. I am afraid, I can’t provide you any reliable estimate at the moment. We will inform you via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi


We’ve run into the same issue: MergeCells and PreferredWith don’t like playing with each other.

Is there an ETA yet for when this will be fixed?

Regards

Hanno

Hi Hanno,

Thanks for your inquiry. Unfortunately, there is no ETA for WORDSNET-6683. I am afraid this issue had been postponed till a later date due to some other important issues and new features. We will inform you as soon as there are any further developments.


We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-6683) have been fixed in this Aspose.Words for .NET 22.2 update also available on NuGet.