DEV Community

CodeSharing
CodeSharing

Posted on

1 1

【Java】Set Background Image/ Color for Word Document

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

Please leave your appreciation by commenting on this post!

It takes just one minute and is worth it for your career.

Get started now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay