Table Horizontal merge

Hi all,

I’m trying to create 2 different tables one with 2 columns and the other with 3 columns.
I encounter problems when i try to merge horizzontally the first and the last row of the tables.
I’ve used the horizontal cell merging but the result is not the one expected.

I’ve prepared a simple testcase, and I’ve also attached the result.

Can you please help me to solve this?
Thanks
Mauro P.

Here the test case.

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;

import com.aspose.words.BreakType;
import com.aspose.words.Cell;
import com.aspose.words.CellMerge;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.SaveFormat;

public class TestMerge {

    static String testFileOutputPath = System.getProperty("user.home").replace(’\’,’ /’)+"/Desktop/test.docx";

    public static void main(String[] args) {
        try {
            Document d = new Document();
            DocumentBuilder b = new DocumentBuilder(d);

            b.insertBreak(BreakType.LINE_BREAK);
            buildTable(b, 5, 2);
            b.insertBreak(BreakType.LINE_BREAK);
            buildTable(b, 5, 3);

            File tempfile = new File(testFileOutputPath);
            FileOutputStream fos = new FileOutputStream(tempfile);
            d.save(fos, SaveFormat.DOCX);
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void buildTable(DocumentBuilder b, int rows, int columns) {
        b.startTable();

        // Insert first row made of merged cells.
        addRowAsOneCell(b, columns, "FIRST ROW");

        // Add n rows as rows parameter
        for (int k = 0; k < rows; k++) {
            for (int i = 0; i < columns; i++) {
                b.insertCell();
                b.write("R" + k + " C" + i);
            }
            b.endRow();
        }
        // Insert first row made of merged cells.
        addRowAsOneCell(b, columns, "LAST ROW");

        b.endTable();
    }

    private static void addRowAsOneCell(DocumentBuilder b, int columns, String text) {
        for (int i = 0; i < columns; i++) {
            Cell c = b.insertCell();
            if (i == 0) {
                c.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
                b.write(text);
            } else {
                c.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
            }
        }
        b.endRow();
    }
}

Hi Mauro,

Thanks for your inquiry. Please set the CellFormat.HorizontalMerge as CellMerge.NONE before creating non merged cells as shown in following highlighted code. This will solve the issue which you are facing. Please let us know if you have any more queries.

private static void buildTable(DocumentBuilder b, int rows, int columns)
{
    b.startTable();
    // Insert first row made of merged cells.
    addRowAsOneCell(b, columns, "FIRST ROW");
    b.getCellFormat().setHorizontalMerge(CellMerge.NONE);
    // Add n rows as rows parameter
    for (int k = 0; k < rows; k++)
    {
        for (int i = 0; i < columns; i++)
        {
            b.insertCell();
            b.write("R" + k + " C" + i);
        }
        b.endRow();
    }
    b.getCellFormat().setHorizontalMerge(CellMerge.NONE);
    // Insert first row made of merged cells.
    addRowAsOneCell(b, columns, "LAST ROW");
    b.endTable();
}