How to Set Font, Size, Color and Bold in a Word Document in Java
Aspose.Words FOSS for Java formats text through the Font of a DocumentBuilder or of a Run. This guide writes text in several fonts, saves the file and reads the formatting 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: Set the Font Before You Write
Set the name, size, bold, color, italic, underline or superscript on builder.getFont() and call write. clearFormatting() reset the name, size, bold and italic in the example, so the next piece of text started from the defaults. Colors are java.awt.Color objects, so import java.awt.Color.
import com.aspose.words.*;
import java.awt.Color;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.getFont().setSize(16);
builder.getFont().setBold(true);
builder.getFont().setColor(Color.BLUE);
builder.write("Bold blue Arial 16 pt. ");
builder.getFont().clearFormatting();
builder.getFont().setItalic(true);
builder.getFont().setUnderline(Underline.SINGLE);
builder.writeln("Italic and underlined.");
builder.getFont().clearFormatting();
builder.write("Plain text, then ");
builder.getFont().setSuperscript(true);
builder.write("superscript");
doc.save("fonts.docx");
Document reloaded = new Document("fonts.docx");
NodeCollection runs = reloaded.getChildNodes(NodeType.RUN, true);
for (int i = 0; i < runs.getCount(); i++) {
Run run = (Run) runs.get(i);
Font font = run.getFont();
System.out.println("\"" + run.getText().trim() + "\" -> " + font.getName() + " " + font.getSize() + " pt, bold=" + font.getBold()
+ ", italic=" + font.getItalic() + ", underline=" + Underline.getName(font.getUnderline()) + ", superscript=" + font.getSuperscript()
+ ", color " + font.getColor().getRed() + "," + font.getColor().getGreen() + "," + font.getColor().getBlue());
}The program printed:
"Bold blue Arial 16 pt." -> Arial 16.0 pt, bold=true, italic=false, underline=NONE, superscript=false, color 0,0,255
"Italic and underlined." -> Times New Roman 12.0 pt, bold=false, italic=true, underline=SINGLE, superscript=false, color 0,0,0
"Plain text, then" -> Times New Roman 12.0 pt, bold=false, italic=false, underline=NONE, superscript=false, color 0,0,0
"superscript" -> Times New Roman 12.0 pt, bold=false, italic=false, underline=NONE, superscript=true, color 0,0,0Step 4: Read the Formatting Back
Reload the file and read run.getFont() for every run from getChildNodes(NodeType.RUN, true). The first run came back as Arial 16.0 pt, bold, color 0,0,255, the others in the default Times New Roman 12.0 pt, one italic and underlined, one superscript.
Step 5: Format an Existing Run
run.getFont() changes text that is already in the document, for example a highlight, strikethrough or all caps. After clearFormatting() the example read strikethrough and all caps back as false.
import com.aspose.words.*;
import java.awt.Color;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("The quick brown fox jumps over the lazy dog.");
Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph();
Run run = paragraph.getRuns().get(0);
run.getFont().setHighlightColor(Color.YELLOW);
run.getFont().setStrikeThrough(true);
run.getFont().setAllCaps(true);
System.out.println("Highlight: " + run.getFont().getHighlightColor().equals(Color.YELLOW));
System.out.println("Strikethrough: " + run.getFont().getStrikeThrough());
System.out.println("All caps: " + run.getFont().getAllCaps());
run.getFont().clearFormatting();
System.out.println("After clearFormatting -> strikethrough: " + run.getFont().getStrikeThrough() + ", all caps: " + run.getFont().getAllCaps());The program printed:
Highlight: true
Strikethrough: true
All caps: true
After clearFormatting -> strikethrough: false, all caps: falseCommon Issues and Fixes
The compiler reports cannot find symbol for Color. Color is java.awt.Color. Add import java.awt.Color;.
The text I wrote before did not change. builder.getFont() formats the text that you write next. Use run.getFont() for existing text. In a test a run written earlier still had bold=false after the builder font was set to bold.
An unhighlighted run reports a black highlight color. An unset highlight color read as r=0, g=0, b=0 with alpha 0 in a test, while a highlight set to Color.BLACK read alpha 255. Check getAlpha().
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
What is the default font?
Times New Roman 12.0 pt, not bold, for a new Document in the test.
Does the font have to be installed?
The examples set the name Arial and read it back from the file. They do not render the document, so they do not show whether a font has to be installed on the reader’s machine.