Java: Apply Formatting to Characters in Word

The appearance of a document conveys more than just document's message; it also reveals the information about the creator. A well-organized document that includes consistently formatted content and appropriate graphics, and that contains no spelling or grammatical mistakes, inspires greater trust in your ability to deliver a product or service.

Using Spire.Doc for Java, you're able to format entire paragraphs as well as individual words or phrases. This article focuses on introducing how to apply various types of formatting to characters in a Word document in Java using Spire.Doc for Java.

  • Font Name
  • Font Size
  • Font Color
  • Highlight Color
  • Bold
  • Italic
  • Underline
  • Strikethrough
  • Border
  • Shadow Effect
  • Emphasis Mark
  • Subscript and Superscript

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Apply Formatting to Characters in Word in Java

In order to apply formatting to a piece of text, you need to get the text in a TextRange and then format the characters within the TextRange using the methods under CharacterFormat class. The following are the steps to set character formatting in a Word document using Spire.Doc for Java.

  • Create a Document object.
  • Add a section to the document using Document.addSection() method.
  • Add a paragraph to the section using Section.addParagraph() method.
  • Append text to the paragraph using Paragraph.appendText() method and return a TextRange object.
  • Apply formatting such as font name, font size, border and highlight color to the characters within the text range using the methods under CharacterFormat class.
  • Save the document to a Word file using Document.saveToFile() method.
  • Java
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.Paragraph;
import com.spire.doc.documents.SubSuperScript;
import com.spire.doc.documents.UnderlineStyle;
import com.spire.doc.fields.TextRange;
import com.spire.doc.fields.shape.Emphasis;

import java.awt.*;

public class ApplyFormattingToCharacters {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        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);

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

        //Set font name
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text in the font of Arial Black");
        tr.getCharacterFormat().setFontName("Arial Black");

        //Set font color
        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 highlight color");
        tr.getCharacterFormat().setHighlightColor(Color.green);

        //Apply border to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text with border");
        tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
        tr.getCharacterFormat().getBorder().setColor(Color.black);

        //Apply emphasis mark to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text with emphasis mark");
        tr.getCharacterFormat().setEmphasisMark(Emphasis.Dot_Below);

        //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("output/SetCharacterFormat.docx", FileFormat.Docx);
    }
}

Java: Apply Formatting to Characters in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.