Issue with merging cells in Table (not using DocumentBuilder)

I’m having a lot of problems trying to merge the first row in a table I have dynamically created. All I want to do is insert text into the first row, and span it to the rest of the cells in that row (I know the number of cells total that need to be merged)

So far I can insert the header text, and create blank cells after that, but I have tried horizontal merges on all of the cells and even copied the mergeCells method from here https://docs.aspose.com/words/java/working-with-merged-cells/.

Here is my code that creates the header text in the first cell, and creates the blank cells after.

Table table = new Table(doc);
doc.getFirstSection().getBody().appendChild(table);
Row row = new Row(doc);
row.getRowFormat().setAllowBreakAcrossPages(false);
table.appendChild(row);
row.appendChild(new Cell(doc));
row.getLastCell().appendChild(new Paragraph(doc));
row.getLastCell().getLastParagraph().appendChild(new Run(doc, "Header Text"));
// Code to add blank Cells to the end of the row
for (int c = 1; c <numColumns; c++)
{
    row.appendChild(new Cell(doc));
}
// After this I have tried many different ways of HorizonalMerge with FIRST and PREVIOUS
// I have also tried the mergeCells(row.getFirstCell(), row.getLastCell()); method in the link i posted

row = new Row(doc);
row.getRowFormat().setAllowBreakAcrossPages(false);
table.appendChild(row);

The mergeCells method looks like it completely removes the blank cells that I inserted with my for loop.
Thanks!

Hi,

Thanks for your inquiry. Please follow this code snippet to merge cells horizontally.

Document doc = new Document();
Table table = new Table(doc);
doc.getFirstSection().getBody().appendChild(table);
Row row = new Row(doc);
row.getRowFormat().setAllowBreakAcrossPages(false);
table.appendChild(row);
row.appendChild(new Cell(doc));
row.getLastCell().appendChild(new Paragraph(doc));
row.getLastCell().getLastParagraph().appendChild(new Run(doc, "Header Text"));
// Code to add blank Cells to the end of the row
for (int c = 1; c <3; c++)
{
    row.appendChild(new Cell(doc));
}
row.getCells().get(0).getCellFormat().setHorizontalMerge(CellMerge.FIRST);
row.getCells().get(1).getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
row.getCells().get(2).getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
// After this I have tried many different ways of HorizonalMerge with FIRST and PREVIOUS
// I have also tried the mergeCells(row.getFirstCell(), row.getLastCell()); method in the link i posted
// row = new Row(doc);
// row.getRowFormat().setAllowBreakAcrossPages(false);
// table.appendChild(row);
doc.save("d:/temp/table2.doc");

Hope this will help you.

Hi there,

Thanks for your inquiry.

I think this issue may occur because you insert the content into the last paragraph of the row. When you merge cells with content in Aspose.Words the first cell becomes the “visible” one, the content from the other cells is disregarded.

Please try using row.getFirstCell() when inserting content instead of row.getLastCell().

Thanks,

imran.rafique,
I have already tried something like this by adding this loop after the first.

for (Cell cell: row.getCells())
{
    if (cell.equals(row.getCells().get(0)))
        cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
    else
        cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}

// This if statement is to set the first cell to CellMerge.FIRST
// which I verified that it works by debugging.
// This is the same code as yours, but will allow for a dynamic number of cells.

The last 3 lines are required since the row is not inserted into the table at all if they are commented out.
The result of this loop just inserts the first cell into the table, and the 2 follow cells are completely removed.

aske012,
I am inserting content into the first cell only. My code shows that the first cell has the text “Header Text” and I am inserting blank cells AFTER the first cell.
I even tried making all blank rows then adding my content with this code (after the merge loop)

row.getFirstCell().appendChild(new Paragraph(doc));
row.getFirstCell().getLastParagraph().appendChild(new Run(doc, "Header Text"));

The resulting table looks exactly the same as if I added the content to the first cell before adding the blank cell (like the code I have in my first post)
I even tried putting this code above all loops and the result is the same, so I’m ruling this out as a possible solution.

I should have said that I was saving the file to a PDF.
I am attaching an example of the problem I am having. I was able to get the first row merged, but only if it is the ONLY row in the table, which defeats the purpose of having a table.

The output PDF is what the merged cells look like if you have 2 or more rows. If you comment out line 61 (table.appendChild(row2) the table displays the merged “Header Text” row perfectly.
And setting the preferred width of the cells does not seem to have an effect on the header row cells disappearing.

PS: Allowing the upload of .java files would be convenient.

Hi,

Thanks for your additional information. Please update table layout before saving document in PDF format.

doc.updateTableLayout();
doc.save(fName, SaveFormat.PDF);

I have attached output Pdf file. In case of any ambiguity, please let me know.

Thanks for the help, updateTableLayout worked but messed up my table right above this one.

I had to rework the other tables so they didn’ t use

table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);

It seems that autoFit and updateTableLayout do not work well when combined (even in separate tables).

I am attaching and example of what happens to the first table if autoFit is set.
If you comment out 75 (table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS) you will have 2 well formed tables. But if line 75 is there, the first table is mangled.

My issue is solved but I was just letting you know about the autoFit issue in case it’s a bug that needs to be corrected. Maybe the updateTableLayout can be table specific instead of document specific.

Thanks!

Hi there,

Thanks for your inquiry.

You should not need to call UpdateTableLayout in your code, that is a helper method used to recalculate outdated tables in existing documents.

I’m afraid I wasn’t able to reproduce any issue using your code. The row merge worked perfectly and the autofit was okay. Could you attach the incorrect and the expected output PDF here for testing?

Thanks,