Unable to capture horizontal merge in table cells

Hi!
I’m working with tables in word and I’m trying to get information about cell merging. I can read the mering on vertical merge but the horizontal merge is always set to none. Why is that so? I’ll send the word file attached so you can see what information I’m talking about.
In cell -> cellformat you have horizontal merge and vertical merge.
If you for example need to collect the rowspan and colspan information.

<table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0>
 <tr>
  <td width=130 align=top><p></p></td>
  <td width=130 rowspan=3 valign=top><p>Cells merged</p></td>
  <td width=130 valign=top><p></p></td>
  <td width=130 valign=top><p></p></td>
  <td width=130 valign=top><p></p></td>
 </tr>
 <tr>
  <td width=130 valign=top><p></p>  </td>
  <td width=130 valign=top><p></p></td>
  <td width=130 valign=top>><p></p></td>
 <td width=130 valign=top>><p></p></td>
</tr>
 <tr>
  <td width=130 valign=top><p></p></td>
  <td width=261 colspan=2 valign=top><p>Cells merged</p></td>
  <td width=130 valign=top><p></p></td>
 </tr>
 <tr>
  <td width=130 valign=top><p></p></td>
  <td width=130 valign=top><p></p></td>
  <td width=130 valign=top><p></p></td>
  <td width=130 valign=top><p></p> </td>
  <td width=130 valign=top><p></p></td>
 </tr>
</table>

tero

Hi

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

If you take a look at the output document, you will see that both tables looks exactly the same, but the second table contains horizontally merged cells, but the first one does not.

Hope my explanation made things clearer.

Best regards.

Thanks for the quick response.
I’m afraid that this does not help in me in the way I’m trying to work with aspose (conversions from word to xml).
I need the information about “colspan” and “rowspan” while converting word documents into xml and
the tables must be in CALS table syntax.
My question is if aspose is able to read horisontalmerge from a loaded word document.
I tested your example and I saw that the the horizontalmerge was set (but it is also created and set in aspose) but if you test my example which is created in word 2007 the information isn’t there.
As I mentioned earlier the only thing that I could capture was verticalmerge.
your example

<table>
    <row>
        <cell horizontalmerge='First'>
            <p align='left'>This is merged cells</p>
        </cell>
        <cell horizontalmerge='Previous'>
        </cell>
    </row>
    <row width='100'>
        </cell>
        <cell width='100'>
        </cell>
    </row>
</table>

my example - the attached word file created in word 2007.

<table>
    <row>
        <cell width='97,75'>
        </cell>
        <cell width='97,75' verticalmerge='First'>
            <p align='left'>Cells merged</p>
        </cell>
        <cell width='97,8'>
        </cell>
        <cell width='97,8'>
        </cell>
        <cell width='97,8'>
        </cell>
    </row>
    <row align='left'>
        <cell width='97,75'>
        </cell>
        <cell width='97,75' verticalmerge='Previous'>
        </cell>
        <cell width='97,8'>
        </cell>
        <cell width='97,8'>
        </cell>
        <cell width='97,8'>
        </cell>
    </row>
    <row>
        <cell width='97,75'>
        </cell>
        <cell width='97,75' verticalmerge='Previous'>
        </cell>
        <cell width='195,6'>
            <p align='left'>Cells merged</p>
        </cell>
        <cell width='97,8'>
        </cell>
    </row>
    <row>
        <cell width='97,75'>
        </cell>
        <cell width='97,75'>
        </cell>
        <cell width='97,8'>
        </cell>
        <cell width='97,8'>
        </cell>
        <cell width='97,8'>
        </cell>
    </row>
</table>

Hi

Thank you for more information. The main goal of my explanation was to tell that MS Word usually do not write merged cells as merged cells (sorry for tautology), but it writes merged cells as simply wide cells. So visually, you see horizontally merged cell, but internally you have just one wide cell. For example see the following XML(extracted from the attached DOCX file)

<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
  <w:body>
    <w:tbl>
      <w:tblPr>
        <w:tblStyle w:val="TableGrid" />
        <w:tblW w:w="0" w:type="auto" />
        <w:tblLook w:val="04A0" />
      </w:tblPr>
      <w:tblGrid>
        <w:gridCol w:w="4952" />
        <w:gridCol w:w="4953" />
      </w:tblGrid>
      <w:tr w:rsidR="00874B32" w:rsidTr="000D38F5">
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="9905" w:type="dxa" />
            <w:gridSpan w:val="2" />
          </w:tcPr>
          <w:p w:rsidR="00874B32" w:rsidRDefault="00874B32" />
        </w:tc>
      </w:tr>
      <w:tr w:rsidR="00874B32" w:rsidTr="00874B32">
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="4952" w:type="dxa" />
          </w:tcPr>
          <w:p w:rsidR="00874B32" w:rsidRDefault="00874B32" />
        </w:tc>
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="4953" w:type="dxa" />
          </w:tcPr>
          <w:p w:rsidR="00874B32" w:rsidRDefault="00874B32" />
        </w:tc>
      </w:tr>
    </w:tbl>
    <w:p w:rsidR="007A3B73" w:rsidRDefault="007A3B73" />
    <w:sectPr w:rsidR="007A3B73" w:rsidSect="007A3B73">
      <w:pgSz w:w="12240" w:h="15840" />
      <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />
      <w:cols w:space="708" />
      <w:docGrid w:linePitch="360" />
    </w:sectPr>
  </w:body>
</w:document>

As you can see the first row contains only one wide cell, but it is supposed to have two merged cells. This is how MS Word usually writes horizontally merged cells.

Best regards,

ok. Thanks Alexey for the explanation.
/tero

One Question though - the information that I’m interested in capturing is what is written in <w:gridSpan w:val="2" />
is that possible in aspose?
/tero

Hi

Thanks for your inquiry. I think the information provided here could be useful for you:
https://forum.aspose.com/t/102529
You should note that <w:gridSpan w:val="2" /> is optional, so it can be there but it can be omitted.

Best regards,

Hi again Alexey!
the suggestion to load the word as html and then traverse takes out performance (we have files that are over 4 mb, and many tables in them) so this is not a solution that I can go on with. One of our criteria is that a conversion must go fast.
You mentioned that "<w:gridSpan w:val="2" /> is optional, so it can be there but it can be omitted." - but in the word document that I also attached earlier in this message I can see that word does show it (in word xml as below) so it isn’t omitted in my example but aspose does not capture it.
Is this a bug - my table as word xml below with gridspan marked in purple?
/tero

<w:tbl>
	<w:tblPr>
		<w:tblStyle w:val="Tabel-Gitter"/>
		<w:tblW w:w="0" w:type="auto"/>
		<w:tblLook w:val="04A0"/>
	</w:tblPr>
	<w:tblGrid>
		<w:gridCol w:w="1955"/>
		<w:gridCol w:w="1955"/>
		<w:gridCol w:w="1956"/>
		<w:gridCol w:w="1956"/>
		<w:gridCol w:w="1956"/>
	</w:tblGrid>
	<w:tr w:rsidR="003F4198" w:rsidTr="003F4198">
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
				<w:vMerge w:val="restart"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
				<w:vMerge w:val="restart"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
				<w:r>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
					<w:t>Cells merged</w:t>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
	</w:tr>
	<w:tr w:rsidR="003F4198" w:rsidTr="003F4198">
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
				<w:vMerge/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
				<w:vMerge/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="003F4198" w:rsidRDefault="003F4198">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
	</w:tr>
	<w:tr w:rsidR="00340AF5" w:rsidTr="003F4198">
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
				<w:vMerge/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="3912" w:type="dxa"/>
				<w:gridSpan w:val="2"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
				<w:r>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
					<w:t>Cells merged</w:t>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
	</w:tr>
	<w:tr w:rsidR="00340AF5" w:rsidTr="003F4198">
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1955" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
		<w:tc>
			<w:tcPr>
				<w:tcW w:w="1956" w:type="dxa"/>
			</w:tcPr>
			<w:p w:rsidR="00340AF5" w:rsidRDefault="00340AF5">
				<w:pPr>
					<w:rPr>
						<w:lang w:val="en-US"/>
					</w:rPr>
				</w:pPr>
			</w:p>
		</w:tc>
	</w:tr>
</w:tbl>

Hi

Thanks for your inquiry. No, this is not a bug. Could you please tell me why you need to convert your document to XML? Do you need to use your custom XML schema or you just need to convert document to XML? Maybe in your case it would be easier to convert document to WordML format (XML) and use it instead of using your custom XML schema.

Best regards.

Hi again Alexey!
Converting word documents to other formats is the only thing we use aspose for (at the moment anyway - we have plans to use it in other areas too), and yes, we use custom xml schemas that are designed so the xml files can be feeded into our content management system for further processing (complex schemas).
“Maybe in your case it would be easier to convert document to WordML format (XML) and use it instead of using your custom XML schema” - This would mean that we don’t use aspose at all, at any time.
I must say that I’m satisfied with aspose.words and how it works(it is easy to use and coding goes fast ) - the only thing I have problems is this issue with column merging.
What I don’t understand is why when you load a word file in aspose as Aspose.Words.Document, you can’t read the information of “horizontal merging” (in the table.docx example) in cells though you have a public property in CellFormat class that is called HorizontalMerge just for that purpose and you can see the gridspan in wordxml (it isn’t omitted).

Regards
Tero

ok - thanks for the answer. I’ll give a try for the other suggestion you made with the other url that you wrote about.
/tero

Re: unable to capture horizontal merge in table cells

Hi
Thanks for your inquiry. I think the information provided here could be useful for you:
https://forum.aspose.com/t/102529