Java: Add, Modify, or Remove Word Table Borders

In Word documents, the ability to add, modify, and remove table borders flexibly can significantly enhance readability and professionalism. Firstly, customizing border styles highlights important information, helping readers quickly locate key data or paragraphs and enhancing visual impact. Secondly, by adjusting the thickness, color, and style of border lines, finer design control can be achieved, ensuring a uniform and aesthetically pleasing document style. Lastly, removing unnecessary borders helps reduce visual clutter, making page layouts cleaner and more comprehensible, improving the reading experience. This article will introduce how to add, modify, or remove Word table borders in Java projects using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>12.9.0</version>
    </dependency>
</dependencies>
    

Java Add Word Table Borders

To uniformly add borders to all cells in a table, you need to visit each cell individually and visually set its border properties. Here are the detailed steps:

  • Create a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document using Document.getSections().get(0).
  • Get the first table within the section using Section.getTables().get(0).
  • Use a for loop to iterate through all cells in the table.
  • Set the cell border to a single line style by using TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single).
  • Define the border width to 1 point by using TableCell.getCellFormat().getBorders().setLineWidth(1f).
  • Set the border color to black by using TableCell.getCellFormat().getBorders().setColor(Color.black).
  • Save the changes to the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;

public class AddBorder {
    public static void main(String[] args) throws Exception{
        // Create a new Document object
        Document doc = new Document();

        // Load the document from file
        doc.loadFromFile("TableExample1.docx");

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

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

        // Declare TableRow and TableCell variables for use in the loop
        TableRow tableRow;
        TableCell tableCell;

        // Iterate through all rows of the table
        for (int i = 0; i < table.getRows().getCount(); i++) {
            // Get the current row
            tableRow = table.getRows().get(i);

            // Iterate through all cells in the current row
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // Get the current cell
                tableCell = tableRow.getCells().get(j);

                // Set the border style of the current cell to single line
                tableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single);

                // Set the width of the border
                tableCell.getCellFormat().getBorders().setLineWidth(1f);

                // Set the color of the border
                tableCell.getCellFormat().getBorders().setColor(Color.black);
            }
        }

        // Save the modified document as a new file
        doc.saveToFile("AddBorders.docx", FileFormat.Docx);

        // Close the document to release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

Java Modify Word Table Borders

Spire.Doc empowers users with extensive customization options for borders, allowing adjustments such as selecting border styles through TableCell.getCellFormat().getBorders().getBottom().setBorderType(), setting border thickness via TableCell.getCellFormat().getBorders().getBottom().setLineWidth(), and specifying border colors with TableCell.getCellFormat().getBorders().getBottom().setColor(). This enables fine-tuned design of table borders within documents according to personal or project needs. Below are the detailed steps:

  • Instantiate a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document by calling Document.getSections().get(0).
  • Get the first table within the section using Section.getTables().get(0).
  • Iterate over the cells in the table that require border style changes using a for loop.
  • Change the color of the bottom border to orange by invoking TableCell.getCellFormat().getBorders().getBottom ().setColor(Color.ORANGE).
  • Alter the style of the bottom border to a dashed line by calling TableCell.getCellFormat().getBorders().getBottom ().setBorderType(BorderStyle.Dot_Dash).
  • Modify the width of the bottom border to 2 points by executing TableCell.getCellFormat().getBorders().getBottom ().setLineWidth(2).
  • Save the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;

public class ModifyBorder {
    public static void main(String[] args) {
        // Create a new Document object
        Document doc = new Document();

        // Load the document from a file
        doc.loadFromFile("TableExample2.docx");

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

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

        // Declare a TableRow to use within the loop
        TableRow tableRow;

        // Iterate through all rows of the table
        for (int i = 1; i < table.getRows().getCount() - 1; i++) {
            tableRow = table.getRows().get(i);

            // Set the border color of the current cell     tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setColor(Color.ORANGE);
            // Set the border style of the current cell to dotted line
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash);

            // Set the width of the border
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setLineWidth(2);
        }

        // Save the modified document as a new file
        doc.saveToFile("ModifyBorder.docx", FileFormat.Docx);

        // Close the document to release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

Java Remove Word Table Borders

When editing Word documents, the flexibility of border design extends beyond the entire table level, allowing for meticulous personalized adjustments at the individual cell level. To comprehensively remove all traces of borders both inside and outside of tables, a phased approach is recommended: Firstly, address the macro-level by clearing the overall border style of the table; subsequently, enter the micro-adjustment phase where each cell within the table is iterated over to revoke its unique border settings. Below are the detailed steps:

  • Instantiate a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document by calling Document.getSections().get(0).
  • Access the first table within the section using Section.getTables().get(0).
  • Iterate over all cells in the table using a for loop.
  • Remove the border of the table by invoking Table.getTableFormat().getBorders().setBorderType(BorderStyle.None).
  • Eliminate the borders of each cell individually by applying TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.None).
  • Save the modified document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;

public class RemoveBorder {
    public static void main(String[] args) {
        // Create a new Document object
        Document doc = new Document();

        // Load the document from a file
        doc.loadFromFile("TableExample2.docx");

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

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

        // Remove the borders set on the table
        table.getTableFormat().getBorders().setBorderType(BorderStyle.None);

        // Declare a TableRow to use in the loop
        TableRow tableRow;

        // Iterate through all rows of the table
        for (int i = 0; i < table.getRows().getCount(); i++) {
            tableRow = table.getRows().get(i);
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // Remove all borders set on the cell          tableRow.getCells().get(j).getCellFormat().getBorders().setBorderType(BorderStyle.None);
            }
        }

        // Save the modified document as a new file
        doc.saveToFile("RemoveBorder.docx", FileFormat.Docx);

        // Close the document and release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.