DEV Community


Posted on

Java/ 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 characters and a whole paragraph by using Free Spire.Doc for Java.

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.

Enter fullscreen mode Exit fullscreen mode

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");
        String text = " is a true masterpiece of American Literature and a must-read for every student of literature. ";

        //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.";


        //Save the document
        doc.saveToFile("AddBorder.docx", FileFormat.Docx_2013);
Enter fullscreen mode Exit fullscreen mode

Add Border

Discussion (0)