Working with Diagonal Borders on table

Hi,

I’m trying to apply a diagonal border in a specific cell of a table (accessing to borders by CellFormat object) without succeeding. Can anyone help me?

Thanks in advice,
F.

@federico.altamura You can set diagonal border with the following code:

cellFormat.Borders[BorderType.DiagonalUp].LineStyle = LineStyle.Single;

Hi,

I’m working with diagonal borders (up and down).
I’ve done two sample code:

In this example goes good:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertCell();
builder.getRowFormat().setHeight(100);
// bottom align in cell
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.BOTTOM);
builder.write("Bottom text");
builder.insertCell();
// create diagonal line.
builder.getCellFormat().setWidth(100);
builder.getCellFormat().getBorders().getByBorderType(BorderType.DIAGONAL_UP).setLineStyle(LineStyle.SINGLE);
builder.write("test");
builder.endRow();
builder.endTable();
doc.save("C:\\Sviluppo\\example.docx");

In this sample code, the output is wrong: I compare border of two tables and I set (hardcoded for example) the lineStyle.

BorderCollection table1Borders = table1CellFormat.getBorders();
BorderCollection table2Borders = table2CellFormat.getBorders();
BorderCollection table3Borders = table3CellFormat.getBorders();

if (table1Borders.getByBorderType(BorderType.DIAGONAL_UP) != null &&
      table2Borders.getByBorderType(BorderType.DIAGONAL_UP) != null &&
      table1Borders.getByBorderType(BorderType.DIAGONAL_UP) != table2Borders.getByBorderType(BorderType.DIAGONAL_UP)) {
  table3Borders.setLineStyle(LineStyle.SINGLE);
}

if (table1Borders.getByBorderType(BorderType.DIAGONAL_DOWN) != null &&
      table2Borders.getByBorderType(BorderType.DIAGONAL_DOWN) != null &&
      table1Borders.getByBorderType(BorderType.DIAGONAL_DOWN) != table2Borders.getByBorderType(BorderType.DIAGONAL_DOWN)) {
table3Borders.setLineStyle(LineStyle.SINGLE);
}

What I do wrong?

Thanks in advice,
F.

@federico.altamura It’s not quite clear what the result you need in the second case. If you need to add diagonal border, then change code to table3Borders.getByBorderType(BorderType.DIAGONAL_UP).setLineStyle(LineStyle.SINGLE);. If not, please provide the full code and excpected result if possible.

@federico.altamura If you need to check if this border is set in the table, change the code to:

cell.getCellFormat().getBorders().getByBorderType(BorderType.DIAGONAL_UP).getLineStyle() != LineStyle.NONE

By default, all borders in the collection are present, but if they are not applied to the table, then LineStyle will be NONE.

Hi,

I want to compare borders of three tables:

  • table1 and table2 are the source of compare
  • table3 is table output

I save the HTML of table3 in a new Aspose document for check the output

here it is full code:

void applyBorderChanges(BorderCollection table1Borders, BorderCollection table2Borders, BorderCollection table3Borders, int borderType)
throws Exception {
try {
  Border table1Border = table1Borders.getByBorderType(borderType);
  Border table2Border = table2Borders.getByBorderType(borderType);
  Border table3Border = table3Borders.getByBorderType(borderType);

  if (table1Border.getLineStyle() != LineStyle.NONE && table2Border.getLineStyle() != LineStyle.NONE) {
	if (BorderType.getName(borderType).equals("DIAGONAL_UP") || BorderType.getName(borderType).equals("DIAGONAL_DOWN")) {
	  table3Border.setLineStyle(LineStyle.SINGLE);
	} else {
	  table3Border.setLineStyle(table2Border.getLineStyle());
	}
  }
  if (table1Border.getLineWidth() != table2Border.getLineWidth()) {
	table3Border.setLineWidth(table2Border.getLineWidth());
  }
  if (table1Border.getColor() != null && table2Border.getColor() != null) {
	table3Border.setColor(table2Border.getColor());
  }
  if (table1Border.getShadow() != table2Border.getShadow()) {
	table3Border.setShadow(table2Border.getShadow());
  }
  if (table1Border.getThemeColor() != table2Border.getThemeColor()) {
	table3Border.setThemeColor(table2Border.getThemeColor());
  }
  if (table1Border.getTintAndShade() != table2Border.getTintAndShade()) {
	table3Border.setTintAndShade(table2Border.getTintAndShade());
  }
  if (table1Border.getDistanceFromText() != table2Border.getDistanceFromText()) {
	table3Border.setDistanceFromText(table2Border.getDistanceFromText());
  }
} catch (Exception e) {
  throw new Exception(e.getMessage());
}
}

Thanks in advice,
F.

@federico.altamura Some tables may have row-level borders, which will be preferred over cell-level borders. Could you please provide your input and expected output documents here?

Oh sure.

In output document i’ll expect table with all borders and diagonal up and down border

docs.zip (33.0 KB)

@federico.altamura I wrote this code to augment your code and get the result:

Document doc = new Document("input.docx");

NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
ArrayList<Cell> cells1 = getCellsFromTable((Table) tables.get(0));
ArrayList<Cell> cells2 = getCellsFromTable((Table) tables.get(1));
ArrayList<Cell> cells3 = getCellsFromTable((Table) tables.get(2));

ArrayList<CellTriple> cellPairs = new ArrayList<>();
for (int i = 0; i < cells1.size(); i++) {
    CellTriple triple = new CellTriple(cells1.get(i), cells2.get(i), cells3.get(i));
    cellPairs.add(triple);
}

int[] borderTypes = { BorderType.TOP, BorderType.BOTTOM, BorderType.LEFT, BorderType.RIGHT, BorderType.DIAGONAL_UP, BorderType.DIAGONAL_DOWN };

for (CellTriple triple : cellPairs) {
    for (int borderType : borderTypes) {
        applyBorderChanges(triple.getCell1().getCellFormat().getBorders(), triple.getCell2().getCellFormat().getBorders(), triple.getCell3().getCellFormat().getBorders(), borderType);
    }
}

doc.save("output.docx");

private static ArrayList<Cell> getCellsFromTable(Table table) {
    ArrayList<Cell> cells = new ArrayList<>();
    for (Row row : table.getRows()) {
        for (Cell cell : row.getCells()) {
            cells.add(cell);
        }
    }
    return cells;
}

private static class CellTriple {
    private Cell cell1;
    private Cell cell2;
    private Cell cell3;

    public CellTriple(Cell cell1, Cell cell2, Cell cell3) {
        this.cell1 = cell1;
        this.cell2 = cell2;
        this.cell3 = cell3;
    }

    public Cell getCell1() {
        return cell1;
    }

    public Cell getCell2() {
        return cell2;
    }

    public Cell getCell3() {
        return cell3;
    }
}

I also swapped tables 2 and 3 in the input document. Here are my results:

input.docx (30.7 KB)

output.docx (27.0 KB)