Changing the font color of a certain paragraph or text can help you make the paragraph or text stand out in your Word document. In this article, we will demonstrate how to change the font color in Word in Java using Spire.Doc for Java library.
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>
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.
- 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("Sample.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); } }
Change Font Color of a Specific Text in Java
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.
- 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("Sample.docx"); //Find the text that you want to change font color for TextSelection[] text = document.findAllString("Spire.Doc for .NET", 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); } }
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.