News Category

Set Style and Border of Table in Word in Java

2020-03-30 09:06:00 Written by  support iceblue
Rate this item
(0 votes)

In this article, we will introduce how to set style and border of a Word table using Spire.Doc for Java.

The following screenshot shows the table before setting style and border:

Set Style and Border of Table in Word in Java

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.Table;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.DefaultTableStyle;

import java.awt.*;

public class SetTableStyleAndBorder {
    public static void main(String[] args){
        //create a Document instance
        Document document = new Document();
        //Load the Word document
        document.loadFromFile("tableSample.docx");

        Section section = document.getSections().get(0);

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

        //apply the table style
        table.applyStyle(DefaultTableStyle.Colorful_List);

        //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);

        //set bottom border of table
        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);

        //save the result file
        document.saveToFile("setTableStyleAndBorder.docx", FileFormat.Docx_2013);
    }
}

The following screenshot shows the table after setting style and border:

Set Style and Border of Table in Word in Java

Additional Info

  • tutorial_title:
Last modified on Tuesday, 31 August 2021 09:18