Java–Change Font Color in Word

In Microsoft Word, you can change the font color to highlight the specific text and give a different look to the document you are making. This article will share how to programmatically change the font color of a specified paragraph or text the in Word…


This content originally appeared on DEV Community and was authored by carlwils

In Microsoft Word, you can change the font color to highlight the specific text and give a different look to the document you are making. This article will share how to programmatically change the font color of a specified paragraph or text the in Word using a free Java library.

Import Dependency

Method 1: Download the free library (Free Spire.Doc for Java) and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly 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>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>5.2.0</version>
   </dependency>
</dependencies>

▶ Change Font Color of the Paragraphs

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;

import java.awt.*;

public class ChangeFontColorForParagraph {
    public static void main(String []args){
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("test.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Change text color of the first Paragraph
        Paragraph p1 = section.getParagraphs().get(0);
        ParagraphStyle s1 = new ParagraphStyle(document);
        s1.setName("Color1");
        s1.getCharacterFormat().setTextColor(new Color(244, 132, 48));
        document.getStyles().add(s1);
        p1.applyStyle(s1.getName());

        //Change text color of the third Paragraph
        Paragraph p2 = section.getParagraphs().get(2);
        ParagraphStyle s2 = new ParagraphStyle(document);
        s2.setName("Color2");
        s2.getCharacterFormat().setTextColor(new Color(0, 0, 139));;
        document.getStyles().add(s2);
        p2.applyStyle(s2.getName());

        //Save the result document
        document.saveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx);
    }
}

ChangeParagraphTextColor

▶ Change Font Color of a Specific Text

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;

import java.awt.*;

public class ChangeFontColorForText {
    public static void main(String []args){
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("test.docx");

        //Find the text that you want to change font color for
        TextSelection[] text = document.findAllString("marketing", false, true);
        //Change the font color for the searched text
        for (TextSelection selection : text)
        {
            selection.getAsOneRange().getCharacterFormat().setTextColor(Color.red);
        }

        //Save the result document
        document.saveToFile("ChangeCertainTextColor.docx", FileFormat.Docx);

    }
}

ChangeCertainTextColor


This content originally appeared on DEV Community and was authored by carlwils


Print Share Comment Cite Upload Translate Updates
APA

carlwils | Sciencx (2022-07-11T03:29:34+00:00) Java–Change Font Color in Word. Retrieved from https://www.scien.cx/2022/07/11/java-change-font-color-in-word/

MLA
" » Java–Change Font Color in Word." carlwils | Sciencx - Monday July 11, 2022, https://www.scien.cx/2022/07/11/java-change-font-color-in-word/
HARVARD
carlwils | Sciencx Monday July 11, 2022 » Java–Change Font Color in Word., viewed ,<https://www.scien.cx/2022/07/11/java-change-font-color-in-word/>
VANCOUVER
carlwils | Sciencx - » Java–Change Font Color in Word. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/11/java-change-font-color-in-word/
CHICAGO
" » Java–Change Font Color in Word." carlwils | Sciencx - Accessed . https://www.scien.cx/2022/07/11/java-change-font-color-in-word/
IEEE
" » Java–Change Font Color in Word." carlwils | Sciencx [Online]. Available: https://www.scien.cx/2022/07/11/java-change-font-color-in-word/. [Accessed: ]
rf:citation
» Java–Change Font Color in Word | carlwils | Sciencx | https://www.scien.cx/2022/07/11/java-change-font-color-in-word/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.