How to set gradient background for Word in C#

Gradient can be used for the Word background to catch the eye. In MS Word, we could choose one color or two colors for the gradient, set the shading style and shading variant of the gradient. It's worthy of mention that Spire.Doc supports to set gradient background in C# with all the options mentioned above. This article is going to introduce the simple code to achieve the feature in C# with Spire.Doc.

Note: before start, please download the latest version of Spire.Doc and add Spire.Doc .dll in the bin folder as the reference of Visual Studio.

Step 1: Load the sample document that only has text.

Document doc = new Document("Sample.docx");

Step 2: Set the background type as Gradient.

doc.Background.Type = BackgroundType.Gradient;
BackgroundGradient Test =doc.Background.Gradient;

Step 3: Set the first color and second color for Gradient.

Test.Color1 = Color.White;
Test.Color2 = Color.Green;

Step 4: Set the Shading style and Variant for the gradient.

Test.ShadingVariant = GradientShadingVariant.ShadingDown;
Test.ShadingStyle = GradientShadingStyle.Horizontal;

Step 5: Save the document and launch to see effects.

doc.SaveToFile("Result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Result.docx");

Sample:

How to set gradient background for Word in C#

Effects:

How to set gradient background for Word in C#

Full Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document("Sample.docx");

            doc.Background.Type = BackgroundType.Gradient;
            BackgroundGradient Test =doc.Background.Gradient;
            Test.Color1 = Color.White;
            Test.Color2 = Color.Green;
            Test.ShadingVariant = GradientShadingVariant.ShadingDown;
            Test.ShadingStyle = GradientShadingStyle.Horizontal;

            doc.SaveToFile("Result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Result.docx");

        }
    }
}