Java adds background color to word document

Time:2020-11-29

preface

When we make a word document, we can add background color to the whole document or a specific text / paragraph if we want to make the boring text appear lively or highlight the specified paragraph or text in the document. This article will use theFree Spire.Doc for JavaControl to demonstrate how to add a background color to a word document in a java program.

This article demonstrates the codecontentIt can be divided into:

  • toEntire word documentAdd background color

1) AddSolid colorBackground color

2) AddGradientsBackground color

  • For theSpecify paragraph or textAdd background color

testing environment

Before running the code, you need to build a test environment. First download, install and configure JDK and IntelliJ idea, and then install free Spire.Doc The jar package in the for Java control is imported into idea. This article focuses on how to import jar package. There are two import methods: one is in theOfficial websiteDownload the product package from the Spire.Doc.jar Import idea manually; second, create one in ideaMavenProject, and then in pom.xml Type the following code in the file, and finally click “import changes”.

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

finalImport effectAs shown in the figure below:

Java adds background color to word document

Code demonstration

Example 1 adding background color to the entire word document

1) AddSolid colorBackground 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 result document
 document.saveToFile("output/AddSolidColor.docx", FileFormat.Docx);
    }
}

design sketch:

Java adds background color to word document

2) AddGradientsBackground 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 result document
 document.saveToFile("output/AddGradientColor.docx", FileFormat.Docx_2013);
    }
}

design sketch:

Java adds background color to word document

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");
        //Gets 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);
        //Gets 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 result document
 document.saveToFile("output/AddParagraphShading.docx", FileFormat.Docx_2013);
    }
}

design sketch:

Java adds background color to word document