How to Change Font Color in Word via Java
To make it easier for readers to grasp the main points of an article, we sometimes need to modify the color of the font to highlight the key points. This article will explain how to change the font color in Word programmatically. To explain how to do this, this article will be divided into two parts.I have attached the C#/VB.NET code for your reference, along with the steps I took to sort things out.
- Change Font Color of a Paragraph
- Change Font Color of a Specific Text
Programming Environment
First, 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.
Method 1:
Introduced manually.
Download Free Spire.Doc for Java locally, unzip it, and find the Spire.Doc.jar
file in the lib folder. Open the following interface in IDEA, and import the
jar file in the local path into the Java program:
Method 2:
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>
<url>https://repo.e-iceblue.com/repository/maven-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 a Paragraph in Java
The following are the steps to change the font color of a
paragraph in a Word document:
- Create a Document instance.
- Load the Word document using Document.LoadFromFile() method.
- Get the desired section using Document.getSections().get(sectionIndex) method.
- Get the desired paragraph that you want to change the font color of using Section.getParagraphs().get(paragraphIndex) method.
- Create a ParagraphStyle instance.
- Set the style name and font color using ParagraphStyle.setName() and ParagraphStyle.getCharacterFormat().setTextColor() methods.
- Add the style to the document using Document.getStyles().add() method.
- Apply the style to the paragraph using Paragraph.applyStyle() method.
- Save the result document using Document.saveToFile() method.
Full Code
Java
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("The Great Gatsby.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(188, 143, 143));
document.getStyles().add(s1);
p1.applyStyle(s1.getName());
//Change text color of the second Paragraph
Paragraph p2 = section.getParagraphs().get(1);
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);
}
}
Effective Shot
Change Font Color of a Specific Text
- The following are the steps to change the font color of a specific text in a Word document:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Find the text that you want to change font color of using Document.findAllString() method.
- Loop through all occurrences of the searched text and change the font color for each occurrence using TextSelection.getAsOneRange().getCharacterFormat().setTextColor() method.
- Save the result document using Document.saveToFile() method.
Full Code
Java
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("The Great Gatsby.docx");
//Find the text that you want to change font color for
TextSelection[] text = document.findAllString("The Great Gatsby", false, true);
//Change the font color for the searched text
for (TextSelection seletion : text)
{
seletion.getAsOneRange().getCharacterFormat().setTextColor(Color.red);
}
//Save the result document
document.saveToFile("ChangeCertainTextColor.docx", FileFormat.Docx);
}
}
Effective Shot
Conclusion:
In this post, you have learned how to change
font color in Word in Java. Not only that, we also have other functions, such
as, Java: Insert Superscripts and
Subscripts into Word, Embed Private Fonts When
Saving Word to DOCX and PDF in Java and
so on. Apart from that, if you'd like to learn more, you can visit this
link to explore more about for Spire.Doc for
Java.
Comments
Post a Comment