Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set the background of Word document in C #

Shulou Source: shulou.com Published: 2022-06-02 12:17:01 09月20日 Update

This article will explain in detail how to set up the background of Word documents in C#. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Word is an indispensable document processing tool in our daily life, study and work. Exquisite and beautiful documents can bring visual beauty when reading. In this article, I will show you how to use the component Free Spire.Doc for .NET (Community Edition) to set the document background for Word. In the following example, adding a background to Word is divided into three situations, namely, adding a solid background, a graded background, and a picture background.

Tool use: after downloading the installation control Free Spire.Doc, you can add Spire.Doc.dll to the project program (the dll can be obtained in the Bin folder under the installation file)

Add a solid color background

C#

Using Spire.Doc;using System.Drawing;namespace AddBackground {class Program {static void Main (string [] args) {/ / create a Document class object and load the Word document Document document = new Document (); document.LoadFromFile (@ "C:\ Users\ Administrator\ Desktop\ Test.docx") / / set the background fill mode of the document to document.Background.Type = Spire.Doc.Documents.BackgroundType.Color; / / set the background color document.Background.Color = Color.MistyRose; / / Save and open the document document.SaveToFile ("PureBackground.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start ("PureBackground.docx");}

After debugging and running the program, generate documentation

Second, add a gradient background

C#

Using Spire.Doc;using System.Drawing;using Spire.Doc.Documents;namespace AddGradientBackground {class Program {static void Main (string [] args) {/ / create a Document class object and load the Word document Document document = new Document (); document.LoadFromFile (@ "C:\ Users\ Administrator\ Desktop\ Test.docx") / / set the background fill mode of the document to gradient fill document.Background.Type = Spire.Doc.Documents.BackgroundType.Gradient; / / set the gradient background color BackgroundGradient gradient = document.Background.Gradient; gradient.Color1 = Color.LightSkyBlue; gradient.Color2 = Color.PaleGreen / / set gradient mode gradient.ShadingVariant = GradientShadingVariant.ShadingMiddle; gradient.ShadingStyle = GradientShadingStyle.FromCenter; / / Save and open the document document.SaveToFile ("GradientColor.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start ("GradientColor.docx");}

Third, add picture background

C#

Using System.Drawing;using Spire.Doc;namespace ImageBackground {class Program {static void Main (string [] args) {/ / create a Document class object and load the Word document Document document = new Document (); document.LoadFromFile (@ "C:\ Users\ Administrator\ Desktop\ Test.docx") / / set the background fill mode of the document to document.Background.Type = Spire.Doc.Documents.BackgroundType.Picture; / / set the background image document.Background.Picture = Image.FromFile (@ "C:\ Users\ Administrator\ Desktop\ 1.jpg"); / / Save and open the document document.SaveToFile ("ImageBackground.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start ("ImageBackground.docx");}

This is the end of this article on "how to set the background of Word documents in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Background documents pictures patterns objects articles colors clips tools files articles more programs solid colors discoloration good practical beautiful content available in Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Huawei Shulou Information OPPO Reno