Java/ Apply Character Formatting to Text in Word Document

NateBennett
2 min readDec 2, 2020

--

In daily work, you can make a Word document more noticeable and eye-catching not only by formatting an entire paragraph, but also by formatting individual words, phrases or sentences.

Common character formatting includes font, font size, bold, italic, underline, strikethrough, subscript, superscript, font color, etc. In this article, I will share the Java code used to apply character formatting to text in Word document.

Tools Used:
● Free Spire.Doc for Java
● IntelliJ IDEA

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>

Code Snippets

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class SetCharacterFormat {

public static void main(String[] args) {

//Create a Word document
Document document = new Document();
Section sec = document.addSection();

//Add a paragraph
Paragraph paragraph = sec.addParagraph();
paragraph.appendText("Here is a paragraph with various character formatting styles");

//Append text to the paragraph and return a TextRange object
paragraph.appendText(". This is ");
TextRange tr = paragraph.appendText("text with strikeout");
//Set the character format to strikeout via TextRange object
tr.getCharacterFormat().isStrikeout(true);

//Apply shadow effect to text
paragraph.appendText(". This is ");
tr = paragraph.appendText("text with shadow");
tr.getCharacterFormat().isShadow(true);

//Apply a large font size to text
paragraph.appendText(". This is ");
tr = paragraph.appendText("text in a large font size");
tr.getCharacterFormat().setFontSize(20);

//Apply color to text
paragraph.appendText(". This is ");
tr = paragraph.appendText("text in red");
tr.getCharacterFormat().setTextColor(Color.red);

//Apply bold & italic to text
paragraph.appendText(". This is ");
tr = paragraph.appendText("text in bold & italic");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setItalic(true);

//Apply underline to text
paragraph.appendText(". This is ");
tr = paragraph.appendText("underlined text");
tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.Single);

//Apply background color to text
paragraph.appendText(". This is ");
tr = paragraph.appendText("text with background color");
tr.getCharacterFormat().setTextBackgroundColor(Color.GREEN);

//Apply superscript to text
paragraph.appendText(". This is a math formula: a");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText(" + b");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText(" = c");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText(".");

//Save to file
document.saveToFile("SetCharacterFormat.docx", FileFormat.Docx);
}
}

Output:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

NateBennett
NateBennett

Written by NateBennett

Sharing code to help developers deal with office files

No responses yet

Write a response