Fonts and Character Formatting

Fonts and Character Formatting

Overview

This guide shows how to set the font name, size, color, bold, italic, underline and superscript of text in a Word document in Java and read the formatting back with Aspose.Words FOSS for Java. Character formatting in a Word document lives in the Font object of a Run or of the DocumentBuilder. Font has getters and setters for the name, the size in points, the color, bold, italic, underline, strikethrough, superscript, all caps and the highlight color, and clearFormatting() resets the formatting. Formatting that you set on the DocumentBuilder applies to the text you write after that. The examples below were compiled and run on Java 8, 17 and 21 with identical output.


Prerequisites

RequirementDetail
Java8 or later
DependencyThe package from Installation
Importimport com.aspose.words.*;
CodeThe examples are statements. Put them in a method that declares throws Exception.

Format Text with the DocumentBuilder

Set the properties on builder.getFont() before you call write, and call clearFormatting() to start the next piece of text from the defaults. The example writes a bold blue Arial 16 pt piece, an italic underlined piece and a plain piece followed by a superscript piece, saves the file and reads every run back. The first run came back as Arial 16.0 pt, bold, color 0,0,255 (Color.BLUE); the second as Times New Roman 12.0 pt, italic with Underline.SINGLE; the last as superscript. Colors are java.awt.Color objects, so the example imports 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,0

Highlight, Strikethrough, All Caps and clearFormatting

Run.getFont() gives the formatting of one run. The example sets a yellow highlight, strikethrough and all caps on the run of a paragraph, reads them back (all three true) and then calls clearFormatting() on the run, after which strikethrough and all caps were false again.

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: false

Tips and Best Practices

  • The default font of a new Document in the test was Times New Roman 12.0 pt, not bold. Read the default from builder.getFont() if you need it.
  • Changing the DocumentBuilder font does not change text that is already written. In a test, a run written before builder.getFont().setBold(true) still reported bold=false afterwards.
  • Font.getHighlightColor() returned a Color with r=0, g=0, b=0 for a run that had no highlight. In this test that value meant no highlight and its alpha was 0 (transparent); a highlight set to Color.BLACK read back with alpha 255. Check getAlpha(), not only the RGB values. The same happened for the shading and border colors of unformatted paragraphs in the borders guide.
  • The first run of the first example read back after a save and reload as Arial, 16.0 pt (the size is a double) and color 0,0,255.

Common Issues

IssueCauseFix
The compiler reports cannot find symbol for Document.The com.aspose.words package is not imported.Add import com.aspose.words.*;.
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.
The compiler reports cannot find symbol for Color.Color is java.awt.Color and it is not imported.Add import java.awt.Color;.
Text I wrote earlier did not change when I changed the builder font.DocumentBuilder.getFont() formats the text that you write next.Change run.getFont() for text that already exists.
A run without highlight reports a black highlight color.An unset highlight color read as r=0, g=0, b=0 with alpha 0 in the test.Check getAlpha(): an unset color read 0 and a highlight set to Color.BLACK read 255.

FAQ

How do I make text bold or italic?

Call setBold(true) or setItalic(true) on builder.getFont() before write, or on run.getFont() for existing text; the second example uses run.getFont() for the highlight, strikethrough and all caps.

How do I underline text?

setUnderline(Underline.SINGLE). The first example read the underline back with Underline.getName as SINGLE.

How do I reset the formatting?

clearFormatting() on the Font. The second example shows strikethrough and all caps going back to false.

Which formats were tested?

Only DOCX: the examples save a DOCX file and reload it.


API Reference Summary

Class / MethodDescription
FontContains font attributes (font name, font size, color, and so on) for an object.
RunRepresents a run of characters with the same font formatting.
UnderlineIndicates type of the underline applied to a font.
Font.setName(value)Sets the name value.
Font.setSize(value)Sets the size value.
Font.setBold(value)Sets the bold value.
Font.setColor(value)Sets the color value.
Font.setUnderline(value)Sets the underline value.
Font.setHighlightColor(value)Sets the highlight color value.
Font.clearFormatting()Resets to default font formatting.

Related Guides