DEV Community

Alexis
Alexis

Posted on

Java - How to Add a Background Color, Picture or Texture to Word Documents

The background of a Word document plays a significant role in determining the style and character of the document. We all know that Word’s default page background is white. In order to make the document more stylish and eye-catching, you can set up a colored background, an image background, or a textured background for it. In this article, I am going to introduce how to programmatically change the background of a Word document using Free Spire.Doc for Java.

Add Spire.Doc.jar as Dependency

If you are working on a maven project, you can include the dependency in pom.xml file using this:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

Enter fullscreen mode Exit fullscreen mode

If you are not using maven, then you can find the required jar files from the zip file available in this location. Include all the jar files into the application lib folder to run the sample code given in this tutorial.

Add a Solid Color to Document Background in Java

The steps to apply a solid color to the background of a Word document are as follows.

  • Create a Document object.
  • Load a Word file using Document.loadFromFile() method.
  • Set the background type to Color using Document.getBackground().setType() method.
  • Set the background color using Document.getBackground().setColor() method.
  • Save the document using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.BackgroundType;

import java.awt.*;

public class AddColorToBackground {

    public static void main(String[] args) {

        //Create a Document object
        Document document= new Document();

        //Load a Word document
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Report.docx");

        //Set the background type to color
        document.getBackground().setType(BackgroundType.Color);

        //Set the background color to light blue
        document.getBackground().setColor(new Color(173,216,230));

        //Save the document
        document.saveToFile("output/BackgroundColor.docx", FileFormat.Docx);
    }
}

Enter fullscreen mode Exit fullscreen mode

coloredBackround

Add a Gradient Color to Document Background in Java

The steps to apply a gradient color to the background of a Word document are as follows.

  • Create a Document object.
  • Load a Word file using Document.loadFromFile() method.
  • Set the background type to Gradient using Document.getBackground().setType() method.
  • Set two colors for the gradient using BackgroundGradient.setColor1() and BackgroundGradient.setColor2() methods.
  • Set the gradient variant to Shading_Down using BackgroundGradient.setShadingVariant() method.
  • Set the shading style to Vertical using BackgroundGradient.setShadingStyle() method.
  • Save the document using Document.saveToFile() method.
import com.spire.doc.BackgroundGradient;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.BackgroundType;
import com.spire.doc.documents.GradientShadingStyle;
import com.spire.doc.documents.GradientShadingVariant;

import java.awt.*;

public class AddGradientColorToBackground {

    public static void main(String[] args) {

        //Create a Document object
        Document document= new Document();

        //Load a Word document
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Report.docx");

        //Set the background type to gradient
        document.getBackground().setType(BackgroundType.Gradient);

        //Set two colors for gradient
        BackgroundGradient gradient = document.getBackground().getGradient();
        gradient.setColor1(Color.white);
        gradient.setColor2(new Color(173,216,230));

        //Set gradient variant to shading down
        gradient.setShadingVariant(GradientShadingVariant.Shading_Down);

        //Set gradient style to vertical
        gradient.setShadingStyle(GradientShadingStyle.Vertical);

        //Save the document
        document.saveToFile("output/BackgroundGradient.docx", FileFormat.Docx);
    }
}

Enter fullscreen mode Exit fullscreen mode

GradientBackground

Add a Picture to Document Background in Java

The following are the steps to add a picture to the background of a Word document.

  • Create a Document object.
  • Load a Word file using Document.loadFromFile() method.
  • Set the background type to Picture using Document.getBackground().setType() method.
  • Set the background picture using Document.getBackground().setPicture() method.
  • Save the document using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.BackgroundType;

public class AddPictureToBackground {

    public static void main(String[] args) {

        //Create an object of Document class
        Document document= new Document();

        //Load a Word document
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Report.docx");

        //Set the background type to picture
        document.getBackground().setType(BackgroundType.Picture);

        //Set the background picture
        document.getBackground().setPicture("C:\\Users\\Administrator\\Desktop\\background.jpg");

        //Save the document
        document.saveToFile("output/BackgroundPicture.docx", FileFormat.Docx);
    }
}
Enter fullscreen mode Exit fullscreen mode

ImageBackground

Add Texture to Document Background in Java

Texture is the feel or shape of a surface or substance, and the texture background is essentially an image added to the background. The following are the steps to add texture to the background of a Word document.

  • Create a Document object.
  • Load a Word file using Document.loadFromFile() method.
  • Set the background type to Texture using Document.getBackground().setType() method.
  • Set the background picture using Document.getBackground().setPicture() method.
  • Save the document using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.BackgroundType;

public class AddTextureToBackground {

    public static void main(String[] args) {

        //Create an object of Document class
        Document document= new Document();

        //Load a Word document
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Report.docx");

        //Set the background type to texture
        document.getBackground().setType(BackgroundType.Texture);

        //Set the texture picture
        document.getBackground().setPicture("C:\\Users\\Administrator\\Desktop\\texture.jpg");

        //Save the document
        document.saveToFile("output/BackgroundTexture.docx", FileFormat.Docx);
    }
}

Enter fullscreen mode Exit fullscreen mode

TexturedBackground

Conclusion

It’s quite easy to add a background color or image to a Word document using Free Spire.Doc for Java. In addition to that, this library offers many easy-to-use interfaces that can help handle complex tasks in Word documents with no effort.

Top comments (0)