In a Word document, you can quickly add visual
appeal to your text by adding a background image or color. Adding a colorful
background image can be helpful when creating a brochure, Word document, or
marketing materials. In this article, I’ll share with you how to set background
image and background color for Word document by using Free Spire.Doc for Java.
Installation
Before coding, please download the latest version of Free
Spire.Doc for Java through this link, unzip the package and then add
Spire.Doc.jar in lib folder into your application as dependency. The following
screenshot is what it finally looks like.
Example
1 Set background image
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
public class BackgroundImage {
public static void main(String[] args) {
String inputFile="C:\\Users\\Test1\\Desktop\\Sample.docx";
String backgroundImg="C:\\Users\\Test1\\Desktop\\Image.jpg";
String outputFile="output/BackgroundImage.docx";
//load a word document
Document document= new Document(inputFile);
//set the background type as picture
document.getBackground().setType(BackgroundType.Picture);
//set the background picture
document.getBackground().setPicture(backgroundImg);
//save the file
document.saveToFile(outputFile, FileFormat.Docx);
}
}
import com.spire.doc.documents.BackgroundType;
public class BackgroundImage {
public static void main(String[] args) {
String inputFile="C:\\Users\\Test1\\Desktop\\Sample.docx";
String backgroundImg="C:\\Users\\Test1\\Desktop\\Image.jpg";
String outputFile="output/BackgroundImage.docx";
//load a word document
Document document= new Document(inputFile);
//set the background type as picture
document.getBackground().setType(BackgroundType.Picture);
//set the background picture
document.getBackground().setPicture(backgroundImg);
//save the file
document.saveToFile(outputFile, FileFormat.Docx);
}
}
Output
Example
2 Set background color
Part
1
Set background solid color
import com.spire.doc.*;import com.spire.doc.documents.BackgroundType;import java.awt.*; public class BackgroundSolidColor { public static void main(String[] args) { String inputFile="C:\\Users\\Test1\\Desktop\\Sample.docx"; String outputFile="output/BackgroundSolidClor.docx"; //load a word document Document document= new Document(inputFile); document.getBackground().setType(BackgroundType.Color); document.getBackground().setColor(Color.lightGray); //save the file document.saveToFile(outputFile, FileFormat.Docx); } }
Output
Part
2
Set background gradient 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.*; public class BackgroundGradientColor { public static void main(String[] args) { String inputFile="C:\\Users\\Test1\\Desktop\\Sample.docx"; String outputFile="output/BackgroundGradientColor.docx"; //load a word document Document document= new Document(inputFile); document.getBackground().setType(BackgroundType.Gradient); document.getBackground().getGradient().setColor1(Color.white); document.getBackground().getGradient().setColor2(Color.cyan); document.getBackground().getGradient().setShadingVariant(GradientShadingVariant.Shading_Down); document.getBackground().getGradient().setShadingStyle(GradientShadingStyle.Horizontal); //save the file document.saveToFile(outputFile, FileFormat.Docx_2013); } }Output
No comments:
Post a Comment