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

Free Support Forum - aspose.com

Trouble re-aligning cells after deleting a "column"

I’m trying to delete a column from a table in a DOC file. The column crosses cells that have been horizontally merged into the column and I’m having trouble getting those merged cells to line up again in the final table.

I’ve got an executable example that I’ll include below, but here’s the gist. I am attempting to delete the third column (i.e. Column C) from the following table:

“Initial Table”
| A1 | B1 | C1 | D1 | E1 | F1 |
| A2 | B2/C2 | D2 | E2 | F2 |

After deleting the column, I expect the table to look like this:

“Expected Table”
| A1 | B1 | D1 | E1 | F1 |
| A2 | B2/C2 | D2 | E2 | F2 |

Alas, it does not.

If I can figure out how to attach the Word Document and the Java source… I’ll do that…

Any advice or alternate implementation of removeColumnCFromWorkingTable(File) would be great!


@drewstovall can you please attach the sources that you mention?

CellMergeBehaviorTest.java.zip (2.2 KB)
CellMergeBehaviorTest-Final.12534550460293806336.doc.zip (6.7 KB)

1 Like

2023-02-28-12-41-41.png (14.5 KB)

I meant to remove the calls to my own debugging tools on lines 80-82. - Sorry about that :slight_smile:

@drewstovall delete the cell could not be the best approach in this case, I prepared an example for you to use as guide (is in C#, but should be easy to translate to Java) :

Document doc = new Document(@"C:\Temp\input.doc");
Document clon = (Document)doc.Clone(true);

Table table = clon.FirstSection.Body.Tables[0];
table.Rows[0].Cells[1].CellFormat.HorizontalMerge = CellMerge.First;
table.Rows[0].Cells[2].CellFormat.HorizontalMerge = CellMerge.Previous;


output.docx (9.5 KB)

I’ll work on translating the C# to Java now… but the output.docx that you attached is exhibiting the same (unexpected) behavior as my code:
2023-02-28-13-30-59.png (29.5 KB)

Ok, I’ve got it working now. (Java version of code include below)

Thanks for the advice. I’m sure it would have been a long time before I tried just merging the cell into the previous cell rather than just removing it.

Can you provide a brief technical explanation of why remove doesn’t work as expected? The more I know about the underlying model, the more likely I am to use it correctly.


private File removeColumnCFromWorkingTable(File file) throws Exception {

    // Implementation based on the recommendation provided here:
    // https://forum.aspose.com/t/trouble-re-aligning-cells-after-deleting-a-column/260696/7?u=drewstovall

    Document document = new Document(file.getAbsolutePath());

    // Extract the second table - the "working" table
    Table table = (Table) document.getChild(NodeType.TABLE, 1, true);

    // Extract cells
    Cell cellB1 = table.getRows().get(0).getCells().get(1);
    Cell cellC1 = table.getRows().get(0).getCells().get(2);
    Cell cellB2 = table.getRows().get(1).getCells().get(1);

    // Remove the content of C1

    // Merge B1 into C1

    // Resize C1 to 0 to ensure column B cells are aligned
    setCellWidth(cellC1, 0.0d);
    setCellWidth(cellB2, 36.0d);

    // Resize Table now that column C is removed

    // Save to disk
    File tmpFile = File.createTempFile("CellMergeBehaviorTest-Final.", ".doc");
    document.save(tmpFile.getAbsolutePath(), SaveFormat.DOC);

    return tmpFile;


MS Word and Aspose.Words (for transitivity) do not contain columns, only rows and cells as you know. So, to make MS Word render a square shaped table, you need to have the same number of cells in each row, because the width of the cells is calculated once and then replicated to each row during the rendering process.
Cells displayed in more than one column are actually multiple cells labeled as “merged”. For that reason, it’s not a good idea to delete a cell if you still want to have a square shaped table. And, if you really need to delete a cell, you’ll need to process the rest of the rows in the table to adjust the final result.

PS: please notice that the solution that I provide only work if the index of the cell to “remove” is greater than 0 (the first cell in the row), in that case the code will be:

Table table = clon.FirstSection.Body.Tables[0];
table.Rows[0].Cells[0].CellFormat.HorizontalMerge = CellMerge.First;
table.Rows[0].Cells[1].CellFormat.HorizontalMerge = CellMerge.Previous;