Java (480)
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>13.11.2</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.
This article demonstrates how to set different header and footer for the fisrt page using Spire.XLS for Java.
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class SetDifferentHeaderFooter {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Insert text in A1 and J1
sheet.getCellRange("A1").setText("page 1");
sheet.getCellRange("J1").setText("page 2");
//Set different first page
sheet.getPageSetup().setDifferentFirst((byte)1);
//Set header string and footer string for the first page
sheet.getPageSetup().setFirstHeaderString("First header");
sheet.getPageSetup().setFirstFooterString("First footer");
//Set header string and footer string for other pages
sheet.getPageSetup().setCenterHeader("Header of other pages");
sheet.getPageSetup().setCenterFooter("Footer of other pages");
//Save the document
workbook.saveToFile("DifferentFirstPage.xlsx", FileFormat.Version2016);
}
}

This article will demonstrate how to add the traffic lights icons in Java applications by using Spire.XLS for Java.
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class setTrafficLightsIcons {
public static void main(String[] args) {
//Create a workbook
Workbook workbook = new Workbook();
//Add a worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Add some data to the cell range and set the format for them
sheet.getCellRange("A1").setText("Traffic Lights");
sheet.getCellRange("A2").setNumberValue(0.95);
sheet.getCellRange("A2").setNumberFormat("0%");
sheet.getCellRange("A3").setNumberValue(0.5);
sheet.getCellRange("A3").setNumberFormat("0%");
sheet.getCellRange("A4").setNumberValue(0.1);
sheet.getCellRange("A4").setNumberFormat("0%");
sheet.getCellRange("A5").setNumberValue(0.9);
sheet.getCellRange("A5").setNumberFormat("0%");
sheet.getCellRange("A6").setNumberValue(0.7);
sheet.getCellRange("A6").setNumberFormat("0%");
sheet.getCellRange("A7").setNumberValue(0.6);
sheet.getCellRange("A7").setNumberFormat("0%");
//Set the height of row and width of column for Excel cell range
sheet.getAllocatedRange().setRowHeight(20);
sheet.getAllocatedRange().setColumnWidth(25);
//Add a conditional formatting
XlsConditionalFormats conditional = sheet.getConditionalFormats().add();
conditional.addRange(sheet.getAllocatedRange());
IConditionalFormat format1 = conditional.addCondition();
//Add a conditional formatting of cell range and set its type to CellValue
format1.setFormatType(ConditionalFormatType.CellValue);
format1.setFirstFormula("300");
format1.setOperator(ComparisonOperatorType.Less);
format1.setFontColor(Color.black);
format1.setBackColor(Color.lightGray);
//Add a conditional formatting of cell range and set its type to IconSet
conditional.addRange(sheet.getAllocatedRange());
IConditionalFormat format = conditional.addCondition();
format.setFormatType(ConditionalFormatType.IconSet);
format.getIconSet().setIconSetType(IconSetType.ThreeTrafficLights1);
//Save to file
String result = "output/setTrafficLightsIcons_result.xlsx";
workbook.saveToFile(result, ExcelVersion.Version2013);
}
}
Effective screenshot of traffic lights icons on Excel worksheet:
![]()