C # Set the background of a Word document (solid color/gradient/image background ),
Word is an essential tool for document processing in our daily life, study, and work. Exquisite and beautiful documents can provide a visual aesthetic for reading. This article describes how to use the Free Spire. Doc for. NET (Community edition) component to set the document background for Word. In the following example, adding a background to a Word is described in three cases: adding a solid color background, gradient background, and image background.
Tool usage: After downloading and installing the Free Spire. Doc control, add Spire. Doc. dll to the project (This dll can be obtained in the Bin folder of the installation file)
I,
Add solid color background
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 = new Document (); Document. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Test.docx"); // set the background filling mode of the document to color filling document. background. type = Spire. doc. documents. backgroundType. color; // set the background Color document. background. color = Color. mistyRose; // save and open the document. saveToFile ("PureBackground.docx", FileFormat. docx2013); System. diagnostics. process. start ("PureBackground.docx ");}}}
After debugging and running the program, generate a document
II,
Add gradient background
Using Spire. doc; using System. drawing; using Spire. doc. documents; namespace AddGradientBackground {class Program {static void Main (string [] args) {// create a Document class instance and load the Word Document document = new Document (); Document. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Test.docx"); // set the background filling mode of the document to gradient filling 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 in gradient mode. shadingVariant = GradientShadingVariant. shadingMiddle; gradient. shadingStyle = GradientShadingStyle. fromCenter; // save and open the document. saveToFile ("GradientColor.docx", FileFormat. docx2013); System. diagnostics. process. start ("GradientColor.docx ");}}}
3. Add an image background
Using System. drawing; using Spire. doc; namespace ImageBackground {class Program {static void Main (string [] args) {// create a Document class instance and load the Word Document document = new Document (); Document. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Test.docx"); // set the background filling mode of the document to fill the document with images. 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. saveToFile ("ImageBackground.docx", FileFormat. docx2013); System. diagnostics. process. start ("ImageBackground.docx ");}}}
The preceding three methods are used to add the background of a Word document. If you like this article, you are welcome to reprint it. (For more information, see the source ).
Thank you for browsing!