Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the background color of a Word page through Java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to set the background color of Word page through Java, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Preface

Background colors can be set according to different document typesetting design requirements in Word. It is common to set a single color, a gradient, or load a picture to set it as a background. Let's use Java to set the background colors of the above three Word pages.

Using tools: Spire.Doc for Java v2.2.0

Jar file import method

Method 1: download through the official website. Create a new directory directory under the program and name it (lib in this example); copy the Spire.Doc.jar under the lib folder in the control package (as shown in figure 1 below) directly to the new directory in the program. After copying the jar file, right-click the jar file and select "Add as Library". Complete the import

Method 2: import through maven.

Java code example (for reference)

[example 1] add a single color background color

Import com.spire.doc.*;import com.spire.doc.documents.BackgroundType;import java.awt.*;import java.io.IOException;public class BackgroundColor_Doc {public static void main (String [] args) throws IOException {/ / load test text String input= "test.docx"; String output= "backgroundcolor.docx"; Document doc = new Document (input); / / set monochrome background doc.getBackground () .setType (BackgroundType.Color); doc.getBackground () .setColor (Color.PINK) / / Save document doc.saveToFile (output,FileFormat.Docx_2013);}}

[example 2] add a 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.IOException;public class GradientBackground_Doc {public static void main (String [] arg) throws IOException {/ / load test document String input= "test.docx"; String output= "GradientBackgound.docx"; Document doc = new Document (input); / / set gradient doc.getBackground () .setType (BackgroundType.Gradient) Doc.getBackground (). GetGradient (). SetColor1 (Color.white); doc.getBackground (). GetGradient (). SetColor2 (Color.green); doc.getBackground (). GetGradient (). SetShadingVariant (GradientShadingVariant.Shading_Middle); doc.getBackground (). GetGradient (). SetShadingStyle (GradientShadingStyle.Horizontal); / / Save document doc.saveToFile (output, FileFormat.Docx_2010);}}

[example 3] load the picture and set it as the background

Import com.spire.doc.*;import com.spire.doc.documents.BackgroundType;import java.io.IOException;public class ImgBackground_Doc {public static void main (String [] arg) throws IOException {/ / load file String input= "test.docx"; String output= "ImgBackgound.docx"; String img= "lye.png"; Document doc = new Document (input); / / set picture background doc.getBackground (). SetType (BackgroundType.Picture); doc.getBackground (). SetPicture (img) / / Save document doc.saveToFile (output, FileFormat.Docx);}}

This is the answer to the question about how to set the background color of the Word page through Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report