avaJava/ Add Borders to Some Text in Word

In a Word document, when we want to emphasize some text or paragraphs, in addition to bolding the text or changing their font color, we can also add borders to them. This article will demonstrate how to apply a border around a set of charaters and a whole paragraph by using Free Spire.Doc for Java.

Installation

Method 1: Download the Free Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

Method 2: You can also 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>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Add Borders

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class AddBorders {

public static void main(String[] args) {

//Create a Document instance
Document doc = new Document();

//Add a section
Section section = doc.addSection();

//Add a border to a set of characters
Paragraph para = section.addParagraph();
TextRange tr = para.appendText("The Scarlet Letter");
tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
tr.getCharacterFormat().getBorder().setColor(Color.BLUE);
String text = " is a true masterpiece of American Literature and a must-read for every student of literature. ";
para.appendText(text);
para.appendBreak(BreakType.Line_Break);

//Add a border to a paragraph
para = section.addParagraph();
String text2 = "This classic novel from the cannon of American Literature exemplifies the genre of Dark Romanticism. " +
"In this story, the consequences of Hester Prynne's adulterous affair with the reverend Arthur Dimmesdale are " +
"borne out as she gives birth to their child and is forced to wear a Scarlet Letter A, embroidered on her bosom, as a sign of her adultery.";

para.appendText(text2);
para.getFormat().getBorders().setBorderType(BorderStyle.Single);
para.getFormat().getBorders().setColor(Color.BLUE);

//Save the document
doc.saveToFile("AddBorder.docx", FileFormat.Docx_2013);
}
}

Output

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store