Merged cells in Word tables

Hi, everybody!
Sorry for my bad english.
I try to parse Word table with Aspose.Words to convert it to another format. How can i detect what cells in a table is merged. I can detect visible cells only but i can’t determine an invisible cells. Can i do it?
By the way, maybe i can detect size of table, size of row and size of cell? Then access to invisible cells isn’t needed in my case.
Thanks in advance for help.

Hi
Thanks for your request.
1. 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");

2. Vertically merged cells. You can create vertically merged cells only using CellFormat.VerticalMerge. The first cell in the merged region should have VerticalMerge = CellMerge.First, all other cells in the merged region should have CellMerge.Previous. Simple code, which shows how to create table with vertically merged cells is shown below:

// 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 vertacally merged cells.
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.VerticalMerge = CellMerge.First;
builder.Write("This is vertically merged cells");
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.VerticalMerge = CellMerge.None;
builder.EndRow();
// Insert the second row
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.VerticalMerge = CellMerge.Previous;
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.CellFormat.VerticalMerge = CellMerge.None;
builder.EndRow();
builder.EndTable();
// Save output document
doc.Save(@"Test001\out.doc");

Hope this could help you. Please let me know in case of any issues. I will be glad to help you.
Best regards.

Hi!
Thank you.
But how can i parse a table? How can i detect height of cells? CellFormat doesn’t contain such property.

Hi
Thanks for your request. You cannot get height of cell; however, you can get height of row. Please see the following link:
https://reference.aspose.com/words/net/aspose.words.tables/rowformat/height/
But you should note that there few height rules you can use. If height rule of the row is “Exactly” then you will get actual height of the row. But if it is “Auto” or “At Least” the actual height of the row is calculated on the fly and is not stored in the document.
https://reference.aspose.com/words/net/aspose.words/heightrule/
Best regards,

Alexey, thank you for your answers. But i have one more question. I learned how to determine a height of the row. But how to determine a height of the vertically merged cell?
Or maybe i can use another way to build the very same cells of the very same table but in another document and in another format.
Don’t be surpised. This is because the initial task for me is conversion of tables from Word document to another format.
Thanks in advance.

Hi
Thanks for your request. You can use CellFormat.VerticalMerge to determine whether cell is vertically merged with another cells.
Also, in what format do you need to convert the tables? Maybe it would be easier just convert the document in another format. Aspose.Words supports a wide set of formats:
https://docs.aspose.com/words/net/supported-document-formats/
Best regards,

Hi!
No. I have not found such format in the article. But i want to try using of some way from this article. The article says that i can programmatically get text of any document element. Perhaps a plain text maid from a table by this manner is useful for me in this case. But i have not found any information about this in Aspose.Words help. How can i get a play text of table?
Thank you for your answers.

Hi
Thank you for additional information. But you did not answered my question. If you need to convert MS Word document to some custom format, I think, DocumentVisitor is just what you need. Here you can find a simple code example that demonstrates how it is easy to parse MS Word document using DocumentVisitor:
https://reference.aspose.com/words/net/aspose.words/documentvisitor/
Answering your original question, you can get plain text of any node in the DOM. Please follow the link to learn how to achieve this:
https://docs.aspose.com/words/java/extract-selected-content-between-nodes/
Best regards,

Hi, Alexey!
Target format is Lexicon.
Unfortunately DocumentVisitor didn’t help. This is just another way to parse document. DocumentVisitor gets a Cell but this Cell have not any properties with height or properties with information about merged cells.
I must draw the cell in another document but i have width only. Maybe there is some other way to detect parameters of the merged cell to draw the same table?
Thank you for your answers.

Alexey, maybe i can get some information about count of rows in vertically merged cell? Something like tag “rowspan” in HTML, maybe?

Hi
Thanks for your request. As I mentioned earlier, you can detect whether the Cell is vertically merged using CellFormat.VerticalMerge property. If this property is CellMerge.First then the cell is the first cell in merged region. If this property is CellMerge.Previous then cell is vertically merged with cell in the previous row. If this property is CellMerge.none than cell is not merged vertically.
https://reference.aspose.com/words/net/aspose.words.tables/cellformat/verticalmerge/
Best regards.

Hi, Alexey!
Thank you for your answer. But how can i access cells with CellMerge.Previous? foreach on Row.ChildNodes ignores these cells as well as foreach on Row.Cells. And attempt to get a cell explicitly using Cells[index] returns null.
Thank you.

Hi
Thanks for your request. Could you please attach your sample document here for testing? I will check it and provide you more information.
Best regards,

Hi!
Sorry, i was wrong. it works in case of vertically merged cells. It doesn’t work in case of horizontally merged cells.
P.S. Attach a document means to create an external link to it? I don’t see anything else like that in the interface.

Hi there,
Thanks for your inquiry.
You can attach documents by clicking “Reply” (not Quick Reply) and then the “Add/Update attachments” button near the bottom.
I think you are unable to find these horizontally merged cells due to the behaviour of MS Word described in Alexey’s first post. The cells look merged but MS Word is exporting them as a single wide cell.
Thanks,

Hi

Thanks for your request. I already explained earlier why this might occur.

alexey.noskov:
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.*

Most likely in your case there are simply wide cells, but not actually merged cells.

Best regards,

Thank you! I understand.