DEV Community

loading...

【Java】Set Background Image/ Color for Word Document

jazzzzz profile image Jane ・2 min read

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

<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>
Enter fullscreen mode Exit fullscreen mode

2. The relevant code snippets

[Example 1] Set background image:

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

[Example 2] Set solid background color:

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

[Example 3] Set 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.*;
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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Discussion (0)

pic
Editor guide