Set Border Colors & Line Widths of Custom Table Cells in Word Document using Java Code of Aspose.Words | Border Collection

Hello,
I would like to set the border colour for selected cells unfortunately, this solution does not work without setting the property
cell.getCellFormat().getBorders().setLineWidth(...)

code presents bug:

        documentBuilder.startTable();
        final Cell[][] tableCells = new Cell[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                tableCells[i][j] = documentBuilder.insertCell();
            }
            documentBuilder.endRow();
        }

        final Color red = new Color(255, 0, 0);
        final CellFormat cellFormat = tableCells[1][1].getCellFormat();

        /* Set red borders color only for one cell */
        final BorderCollection borders = cellFormat.getBorders();
        borders.getLeft().setColor(red);
        borders.getRight().setColor(red);
        borders.getBottom().setColor(red);
        borders.getTop().setColor(red);


        final Color blue = new Color(68, 114, 196);
        tableCells[1][2].getCellFormat().getBorders().getRight().setColor(blue);

        /* With set line width works ... */
        tableCells[2][2].getCellFormat().getBorders().setLineWidth(2D);
        tableCells[2][2].getCellFormat().getBorders().setColor(blue);
        documentBuilder.endTable();
        saveDocument(documentBuilder);


    protected void saveDocument(DocumentBuilder documentBuilder) {
        DateFormat dateFormat = new SimpleDateFormat("_HH_mm_ss");
        Date currentDate = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(currentDate);
        final String outPath = System.getProperty("user.dir") + File.separator + "table_test_word" + dateFormat.format(currentDate) + ".docx";

        OoxmlSaveOptions options = new OoxmlSaveOptions();
        options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
        try {
            documentBuilder.getDocument().save(outPath, options);
        } catch (Exception e) {
            fail("Test failed during save due to: " + e.getMessage());
            e.printStackTrace();
        }
    }

@aolo23,

Please try the following code of Aspose.Words for Java:

Document doc = new Document();
DocumentBuilder documentBuilder = new DocumentBuilder(doc);

Table table = documentBuilder.startTable();
final Cell[][] tableCells = new Cell[3][3];
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        tableCells[i][j] = documentBuilder.insertCell();
    }
    documentBuilder.endRow();
}
documentBuilder.endTable();

Cell cell_1 = table.getRows().get(1).getCells().get(0); // first Cell on second Table Row
Cell cell_2 = table.getRows().get(1).getCells().get(1); // second cell on second Table row

// The Right border of cell_1 and the Left border of cell_2 are Shared (Common)
// Lets set different colors to this shared border
cell_1.getCellFormat().getBorders().getRight().setColor(Color.BLUE);
cell_2.getCellFormat().getBorders().getLeft().setColor(Color.RED);

// By default, both these borders have 0.5 Line Width
// MS Word will apply the color to Shared Cell border whose width is greater (try the following cases)

// Case 1: Red color will appear
// cell_1.getCellFormat().getBorders().getRight().setLineWidth(1);
// cell_2.getCellFormat().getBorders().getLeft().setLineWidth(1.1);

// Case 2: Blue color will appear
cell_1.getCellFormat().getBorders().getRight().setLineWidth(1.1);
cell_2.getCellFormat().getBorders().getLeft().setLineWidth(1);

doc.save("C:\\Temp\\output.docx");