Apply Formatting to Characters in Word in Java

When you create a Word document with Spire. Doc for Java, you can format an entire paragraph by applying a ParagraphStyle, you can also format individual words or phrases by applying a CharacterFormat. This article introduces how to apply formatting to characters in Word.

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 styles. This is ");

        //append text to the paragraph and return a TextRange object
        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

Apply Formatting to Characters in Word in Java