Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Sep 17, 2026 4:13 pm

Hi,

What is the correct way of adding a bottom border to an arbitrary row in a table in Spire.DOC for Java?

I create a table based on database data, and at specific point in processing (currently processing row 3) I try to add a bottom border to that row. Below is the function I use in adding a border to a table row.

Code: Select all
    private void addBottomBorderToRow(Table dataTable, int rowIndex) {
        log.error("ADD BORDER TO {}", rowIndex);
        var row = dataTable.getRows().get(rowIndex);
        var format = row.getRowFormat();
        var bottom = format.getBorders().getBottom();
        bottom.setBorderType(BorderStyle.Single);
        bottom.setColor(Color.RED);
    }


However, after document has been created, the border is at the bottom of the table, not on the third row as logged by the function.

When I tried to convert the above function so that it would get all cells from the row, and add bottom border to all cells, then every row of the table had a bottom border.

I'm currently using Spire.DOC 13.12.2, and at the moment cannot update due to client process.

juha.ruotsalainen
 
Posts: 3
Joined: Thu Sep 17, 2026 4:03 pm

Fri Sep 18, 2026 3:54 am

Hi Juha,

Thank you for reaching out.
To add a bottom border to a specific row, please use the code below:

Code: Select all
TableRow row = table.getRows().get(2);
        for (int i = 0; i < row.getCells().getCount(); i++) {
            Border bottom = row.getCells().get(i).getCellFormat().getBorders().getBottom();
            bottom.setBorderType(BorderStyle.Single);
            bottom.setColor(Color.RED);
            bottom.setLineWidth(1.5f);
        }


Feel free to contact us if you have any further questions.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3043
Joined: Wed Jun 27, 2012 8:50 am

Fri Sep 18, 2026 4:30 am

Thanks for replying,

Are these the guidelines?

a) The border should be added _after_ the table generation is otherwise complete?
b) The border must be added to cells, not a row?

Thanks,
Juha

juha.ruotsalainen
 
Posts: 3
Joined: Thu Sep 17, 2026 4:03 pm

Fri Sep 18, 2026 4:49 am

Answering to myself based on my demo project: table-topper by rumpu-jussi at codeberg.

1. Yes, you need to add the border to cells, not the row.

2. Yes, you need to add the border after the table is ready. While making my demo project, I noticed that if I add the border while processing the data, all rows after the row that should've received a bottom border get bottom border, too. Therefore, if I would want to add the border while generating the table, I would have to have an if-else controlling the border generation:
Code: Select all
if (shouldAddBorder) {
  // Loop over row cells to add bottom border.
} else {
  // Loop over row cells to clear bottom border.
}

juha.ruotsalainen
 
Posts: 3
Joined: Thu Sep 17, 2026 4:03 pm

Fri Sep 18, 2026 5:51 am

Hi Juha,

Thank you for sharing your findings based on your demo project.

We really appreciate you taking the time to summarize your experience. Your observation about adding borders after the table is ready is very helpful, and the if-else approach you proposed for controlling border generation during table creation makes perfect sense.

We're glad to hear that you were able to resolve the issue. If you have any other questions or need further assistance in the future, please don't hesitate to reach out.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3043
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc