Set Table Styles and Borders in Word in Java

Andrew Wilson
2 min readMay 19, 2023

In Word, tables are a useful tool for organizing information into a format that is easy to view and compare. If you want to create a stylish table to catch attention or beautify the document, you can set its style and borders in Word. In this article, you will learn how to programmatically set the styles and borders for tables in Word using Free Spire.Doc for Java.

Install the Library (Two Methods)

Method 1: Download the free library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>

Sample Code

Free Spire.Doc for Java offers several built-in table styles which you can apply to specified tables using Table.applyStyle() method. Apart from that, you are also allowed to set the top, bottom, left, right as well as inside borders for tables using the free Java Word library. The complete sample code is shown below.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;

public class setTableStyleAndBorder {
public static void main(String[] args) {

//Create a Document instance and load file
Document document = new Document();
document.loadFromFile("Table.docx");

//Get the first section
Section section = document.getSections().get(0);

//Get the first table
Table table = section.getTables().get(0);

//Apply the table style
table.applyStyle(DefaultTableStyle.Medium_Shading_1_Accent_3);

//Set right border of table
table.getTableFormat().getBorders().getRight().setBorderType(BorderStyle.Hairline);
table.getTableFormat().getBorders().getRight().setLineWidth(1.0F);
table.getTableFormat().getBorders().getRight().setColor(Color.RED);

//Set top border of table
table.getTableFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline);
table.getTableFormat().getBorders().getTop().setLineWidth(1.0F);
table.getTableFormat().getBorders().getTop().setColor(Color.GREEN);

//Set left border of table
table.getTableFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline);
table.getTableFormat().getBorders().getLeft().setLineWidth(1.0F);
table.getTableFormat().getBorders().getLeft().setColor(Color.YELLOW/*.getYellow()*/);

//Set bottom border to none
table.getTableFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash);

//Set vertical and horizontal border
table.getTableFormat().getBorders().getVertical().setBorderType(BorderStyle.Dot);
table.getTableFormat().getBorders().getHorizontal().setBorderType(BorderStyle.None);
table.getTableFormat().getBorders().getVertical().setColor(Color.ORANGE /*.getOrange()*/);

//Save the file
document.saveToFile("setTableStyleAndBorder.docx", FileFormat.Docx);
}
}

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.