News Category

Page Background

Page Background (2)

Paragraph and text background color is an important element of document design in Word documents. Appropriate paragraph and text background color can serve to highlight specific paragraphs or text, enhance the contrast of text so that it is easier to read, and fill in the gaps in the layout to assist in typography. Therefore, the reasonable use of paragraph and text background color is very important when creating Word documents. This article is going to show how to set background color for paragraphs and text in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Set Background Color for Paragraphs in Word Documents

To set the background color of a paragraph, we need to get the paragraph and use the Paragraph.getFormat().setBackColor() method to change its background color. The detailed steps are as follows.

  • Create an object of Document.
  • Load a Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Get the third paragraph using Section.getParagraphs().get() method.
  • Set the background color for the paragraph using Paragraph.getFormat().setBackColor() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

import java.awt.*;

public class SetParagraphBackgroundColor {
    public static void main(String[] args) {

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

        //Load a Word document
        document.loadFromFile("C:/Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Get the third paragraph
        Paragraph paragraph = section.getParagraphs().get(2);

        //Set the background color of this paragraph as Light Gray
        paragraph.getFormat().setBackColor(Color.LIGHT_GRAY);

        //Save the document
        document.saveToFile("ParagraphBackgroundColor.docx", FileFormat.Docx_2013);
        document.dispose();
    }
}

Java: Set Background Color for Paragraphs or Text

Set Background Color for Existing Text in Word Documents

Spire.Doc for Java provides Document.findAllString() method to find all the occurrences of specific text in a Word document and TextRange.getCharacterFormat().setTextBackgroundColor() to set the background color for specific text. The detailed steps for setting the background color for existing text are as follows.

  • Create an object of Document.
  • Load a Word document using Document.loadFromFile() method.
  • Find all the occurrences of “timeless universe” using Document.findAllString() method.
  • Loop through the occurrences.
  • Get each occurrence as a text range using TextSelection.getAsOneRange() method.
  • Set the background color for the text range using TextRange.getCharacterFormat().setTextBackgroundColor() method.
  • You can also choose an occurrence by its index from the collection, get it as a text range, and set background color only for the occurrence.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class SetTextBackgroundColor {
    public static void main(String[] args) {

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

        //Load a Word document
        document.loadFromFile("C:/Sample.docx");

        //Find the text to set background color for
        TextSelection[] textSelections = document.findAllString("timeless universe", false, true);

        //Loop through the occurrences of the text
        for (TextSelection selection : textSelections){

            //Get an occurrence as a text range
            TextRange textRange = selection.getAsOneRange();
            //Set background color for the occurrence
            textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);
        }

        //Set the background color for the first occurrence of the text
        //TextRange textRange = textSelections[0].getAsOneRange();
        //textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);

        //Save the document
        document.saveToFile("TextBackgroundColor.docx", FileFormat.Docx_2013);
        document.dispose();
    }
}

Java: Set Background Color for Paragraphs or Text

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

The background of a Word document is blank by default. For documents like brochures, invitations, handouts, and marketing materials, blank backgrounds will be too tedious. A good-looking background can be a great attraction to readers. Therefore, you can add a color or insert an image as a background to make your documents more appealing. This article shows how to set background color or image for Word documents by programming using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Add a Background Color to a Word Document

Adding a background color to a Word document is very easy. You only need to set the background type as color and then choose a color as your background. The detailed steps are as follows.

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Set the background type as color using Document.getBackground().setType() method.
  • Set the background color using Document.getBackground().setColor() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;

import java.awt.*;
import java.io.*;

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

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

        //Load a Word document
        document.loadFromFile("C:/Sample.docx");

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

        //Set the background color as orange
        document.getBackground().setColor(Color.orange);

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

Java: Add Background Color or Picture to Word Documents

Add a Gradient Background to a Word Document

Adding gradient background requires more steps. You need to set the background type as gradient, choose two colors, and then set shading variant and style. The detailed steps are as follows.

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Set the background type as gradient using Document.getBackground().setType() method.
  • Choose two colors using >Background.getGradient().setColor1() and Background.getGradient().setColor2() method.
  • Set shading variant using Background.getGradient().setShadingVariant() method.
  • Set shading style using Background.getGradient().setShadingStyle() method.
  • Save the document using Document.saveToFile() method.
  • 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 addBackgroundColor {
    public static void main(String[] args) throws IOException {

        //load a word document
        Document document= new Document("C:/Sample.docx");

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

        //Choose two colors
        Background background = document.getBackground();
        background.getGradient().setColor1(Color.white);
        background.getGradient().setColor2(Color.orange);

        //Choose gradient variant
        background.getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);

        //Choose gradient style
        background.getGradient().setShadingStyle(GradientShadingStyle.Horizontal);

        //Save the document
        document.saveToFile("AddGradientBackground.docx", FileFormat.Docx_2013);
    }
}

Java: Add Background Color or Picture to Word Documents

Insert Background Image to a Word Document

To insert a background picture to a Word document, you need to set the background type as picture, and then insert a picture as the background. The detailed steps are as follows.

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Change the background type to picture using Document.getBackground().setType() method.
  • Insert a picture as the background using Document.getBackground().setPicture() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.io.*;

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

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

        //Load a Word document
        document.loadFromFile("C:/Sample.docx");

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

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

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

Java: Add Background Color or Picture to Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.