How to Add Borders and Shading to Paragraphs and Table Cells in Java

How to Add Borders and Shading to Paragraphs and Table Cells in Java

Aspose.Words FOSS for Java sets borders and background colors through the paragraph format and the cell format. This guide formats a paragraph and the cells of a table and reads the result back, with the library from Maven Central.

Step-by-Step Guide

Step 1: Install the Package

Add the package to your build. The library needs Java 8 or later.

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-words-foss</artifactId>
  <version>26.3.0</version>
</dependency>

Step 2: Import the Classes

All classes are in the com.aspose.words package, so the example starts with import com.aspose.words.*;. The examples are statements: put them in a method that declares throws Exception.


Step 3: Format a Paragraph

Set the bottom border with getBorders().getBottom() (line style, width in points, color) and the fill with getShading().setBackgroundPatternColor on builder.getParagraphFormat(), then write the paragraph. clearFormatting() resets the format for the next paragraph.

import com.aspose.words.*;
import java.awt.Color;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

ParagraphFormat format = builder.getParagraphFormat();
format.getBorders().getBottom().setLineStyle(LineStyle.SINGLE);
format.getBorders().getBottom().setLineWidth(1.5);
format.getBorders().getBottom().setColor(Color.RED);
format.getShading().setBackgroundPatternColor(new Color(255, 250, 205));
builder.writeln("A paragraph with a red bottom border and a pale yellow background.");

format.clearFormatting();
builder.writeln("A plain paragraph.");
doc.save("borders.docx");

Document reloaded = new Document("borders.docx");
NodeCollection paragraphs = reloaded.getChildNodes(NodeType.PARAGRAPH, true);
for (int i = 0; i < paragraphs.getCount(); i++) {
    Paragraph paragraph = (Paragraph) paragraphs.get(i);
    if (paragraph.getText().trim().length() == 0) {
        continue;
    }
    Border bottom = paragraph.getParagraphFormat().getBorders().getBottom();
    Color background = paragraph.getParagraphFormat().getShading().getBackgroundPatternColor();
    System.out.println("\"" + paragraph.getText().trim() + "\"");
    System.out.println("  bottom border: " + LineStyle.getName(bottom.getLineStyle()) + ", " + bottom.getLineWidth() + " pt, color " + bottom.getColor().getRed() + "," + bottom.getColor().getGreen() + "," + bottom.getColor().getBlue());
    System.out.println("  background: " + background.getRed() + "," + background.getGreen() + "," + background.getBlue());
}

The program printed:

"A paragraph with a red bottom border and a pale yellow background."
  bottom border: SINGLE, 1.5 pt, color 255,0,0
  background: 255,250,205
"A plain paragraph."
  bottom border: NONE, 0.0 pt, color 0,0,0
  background: 0,0,0

Step 4: Read the Format Back

After saving and reloading, the first paragraph had a SINGLE bottom border, 1.5 pt, red (255,0,0) and the background 255,250,205. The plain second paragraph had a NONE border of 0.0 pt.


Step 5: Shade and Border Table Cells

Set builder.getCellFormat().getShading() and getBorders() before insertCell. The header cell read back the background 221,235,247 and the first body cell had a DOUBLE top border. The first header cell also read a SINGLE top border although the example set none, so the first header cell started with a SINGLE top border in the test.

import com.aspose.words.*;
import java.awt.Color;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.startTable();
builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(221, 235, 247));
builder.insertCell();
builder.write("Header A");
builder.insertCell();
builder.write("Header B");
builder.endRow();

builder.getCellFormat().getShading().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.DOUBLE);
builder.insertCell();
builder.write("Body 1");
builder.insertCell();
builder.write("Body 2");
builder.endRow();
builder.endTable();
doc.save("cells.docx");

Document reloaded = new Document("cells.docx");
Table loaded = (Table) reloaded.getChildNodes(NodeType.TABLE, true).get(0);
Cell header = loaded.getRows().get(0).getCells().get(0);
Color shade = header.getCellFormat().getShading().getBackgroundPatternColor();
System.out.println("Header cell background: " + shade.getRed() + "," + shade.getGreen() + "," + shade.getBlue());
for (int r = 0; r < loaded.getRows().getCount(); r++) {
    Cell cell = loaded.getRows().get(r).getCells().get(0);
    System.out.println("Row " + r + ", first cell: top border " + LineStyle.getName(cell.getCellFormat().getBorders().getTop().getLineStyle()));
}

The program printed:

Header cell background: 221,235,247
Row 0, first cell: top border SINGLE
Row 1, first cell: top border DOUBLE

Common Issues and Fixes

The compiler reports cannot find symbol for Color. Color is java.awt.Color. Add import java.awt.Color;.

An unshaded paragraph reports the background 0,0,0. An unset color read as 0,0,0 with alpha 0 in a test. Check getAlpha(): it was 0 for an unset color in the test.

setLineWidth throws IllegalArgumentException. The width was outside 0 to 31 points. In a test -0.5, 31.5, 32 and 100 threw and 0, 0.25 and 31 were accepted.

The compiler reports an unreported exception. The Document constructors, Document.save and some other methods declare throws Exception. Declare it on the calling method or catch it.

Frequently Asked Questions

Which line styles exist?

LineStyle has values such as NONE, SINGLE, THICK, DOUBLE, HAIRLINE and DOT. The examples used SINGLE and DOUBLE.

Does a border default to none?

For a paragraph, yes in the test: a paragraph that never had a border reported NONE and 0.0 pt. Table cells were different: the first cell of the header row read a SINGLE top border although the example set no border for it.

See Also