Free Support Forum - aspose.com

Merge cell information from Words

Hi,

My requirement is to extract text from tables using aspose.words for Java.

I get the horizontal merge property 0 always.

Please help.

Regards,

Sheeba

Hi Sheeba,

Thanks for your reqeust. 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.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);

builder.getCellFormat().getBorders().setColor(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.getCellFormat().setWidth(200);

builder.write("This is simply wide cell");

builder.endRow();

// Insert the second row

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.insertCell();

builder.getCellFormat().setWidth(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.getCellFormat().setWidth(100);

builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);

builder.write("This is merged cells");

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);

builder.endRow();

// Insert the second row

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);

builder.endRow();

builder.endTable();

// Save output document

doc.save("C:\\Temp\\out.doc");

Best regards,