Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 11, 2022 03:29 am GMT

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


Original Link: https://dev.to/carlwils/java-change-font-color-in-word-5hbf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To