【Java】Set Background Image/ Color for Word Document
Tags: java, word, background
Published: 2021-01-22 07:48:19

Usually the default background of Word documents is white, and adding image or a bit of color is probably the easiest way to liven up these boring document. This article will demonstrate how to set background image, solid background color as well as gradient background color for a word document in Java program.  

**1.** Import the jar dependency of a 3rd party free API to your Java application (2 methods)

● Download the free API ([Free Spire.Doc for Java](https://www.e-iceblue.com/Download/doc-for-java-free.html)) and unzip it, then add the Spire.Doc.jar file to your Java application as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
```xml
<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>3.9.0</version>
   </dependency>
</dependencies>
```

**2.** The relevant code snippets

**[Example 1]** Set background image:
```java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.io.*;

public class WordBackground {
    public static void main(String[] args) throws IOException {

        String inputFile="test2.docx";
        String backgroundImg="C:\\Users\\Administrator\\Desktop\\bg.png";
        String outputFile="out/background.docx";

        //load a word document
        Document document= new Document(inputFile);

        //set background image
        document.getBackground().setType(BackgroundType.Picture);
        document.getBackground().setPicture(backgroundImg);

        //save the file
        document.saveToFile(outputFile, FileFormat.Docx);
    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dco7p838wme7wca3snk2.jpg)


**[Example 2]** Set solid background color:
```java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.*;

public class BackgroundColor {
    public static void main(String[] args) throws IOException {

        String inputFile="test2.docx";
        String outputFile="out/background2.docx";

        //load a word document
        Document document= new Document(inputFile);

        //set the background color to light gray
        document.getBackground().setType(BackgroundType.Color);
        document.getBackground().setColor(Color.lightGray);

        //save the file
        document.saveToFile(outputFile, FileFormat.Docx);
    }
}
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hv2i6m4pwy36i7dogto4.jpg)

**[Example 3]** Set gradient background color:
```java
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.*;
import java.io.*;

public class GradientColor {
    public static void main(String[] args) throws IOException {

        String inputFile="test2.docx";
        String outputFile="out/background3.docx";

        //load a word document
        Document document= new Document(inputFile);

        //set the gradient background color
        document.getBackground().setType(BackgroundType.Gradient);
        document.getBackground().getGradient().setColor1(Color.white);
        document.getBackground().getGradient().setColor2(Color.BLUE);
        document.getBackground().getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);
        document.getBackground().getGradient().setShadingStyle(GradientShadingStyle.Horizontal);

        //save the file
        document.saveToFile(outputFile, FileFormat.Docx_2013);
    }
}
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uc9cwwljkkqbm2xz8zn7.jpg)