Preface
When we make Word After the documents , Want to make a boring text appear to be dynamic , Or you want to highlight a specific paragraph or text in the document , At this point, we can do this for the entire document or for a specific text / Add background color to the paragraph . This article will use Free Spire.Doc for Java Control to demonstrate how Java In the program Word Add background color to the document .
This article code demonstrates Content Can be divided into :
- to Whole Word file Add background color
1) add to Pure color Background color
2) add to gradient Background color
- to Word In document Specify a paragraph or text Add background color
Test environment
Before running the code , You need to build a test environment . First download, install and configure JDK and IntelliJ IDEA, And then Free Spire.Doc for Java Control Jar Package import IDEA. Here we focus on how to import Jar package . There are two ways to import : firstly , stay Official website Download the product package , After decompression lib Under folder Spire.Doc.jar Manual import IDEA; second ( Recommended ), stay IDEA Create a Maven project , And then in pom.xml Type the following code into the file , Finally, click “Import Changes” that will do .
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Final Import effect As shown in the figure below :
Code demonstration
Example 1 To the whole Word Add background color to the document
1) add to Pure color Background color
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
public class SolidBackgroundColor {
public static void main(String[] args) {
// load Word Sample document
Document document= new Document("C:UsersTest1DesktopSample.docx");
// Add background color and set color type
document.getBackground().setType(BackgroundType.Color);
document.getBackground().setColor(Color.lightGray);
// Save the result file
document.saveToFile("output/AddSolidColor.docx", FileFormat.Docx);
}
}
design sketch :
2) add to gradient Background color
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import com.spire.doc.documents.GradientShadingStyle;
import com.spire.doc.documents.GradientShadingVariant;
import java.awt.*;
public class GradientBackgroundColor {
public static void main(String[] args) {
// load Word Sample document
Document document= new Document("C:UsersTest1DesktopSample.docx");
// Add background color and set color type
document.getBackground().setType(BackgroundType.Gradient);
document.getBackground().getGradient().setColor1(Color.white);
document.getBackground().getGradient().setColor2(Color.cyan);
document.getBackground().getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);
document.getBackground().getGradient().setShadingStyle(GradientShadingStyle.Horizontal);
// Save the result file
document.saveToFile("output/AddGradientColor.docx", FileFormat.Docx_2013);
}
}
design sketch :
Example 2 Adds a background color to a specified paragraph or text in a document
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;
public class SetParagraphShading {
public static void main(String[] args) {
// load Word Sample document
Document document = new Document();
document.loadFromFile("C:UsersTest1DesktopSample.docx");
// Get the specified paragraph in the document
Paragraph paragaph = document.getSections().get(0).getParagraphs().get(3);
// Adds a background color to the specified paragraph
paragaph.getFormat().setBackColor(Color.yellow);
// Get the specified text in the document
paragaph = document.getSections().get(0).getParagraphs().get(1);
TextSelection selection = paragaph.find(" Christmas ", true, false);
// Adds a background color to the specified text
TextRange range = selection.getAsOneRange();
range.getCharacterFormat().setTextBackgroundColor(Color.pink);
// Save the result file
document.saveToFile("output/AddParagraphShading.docx", FileFormat.Docx_2013);
}
}
design sketch :