C#/VB.NET: Add Background Color or Image to Word Documents

2022-10-18 08:11:00 Written by  support iceblue
Rate this item
(0 votes)

The default background of a Word document is white, and in the vast majority of cases, a simple white background is sufficient. However, if you are creating a resume, a broacher or other creative document that needs to be eye-catching, setting a unique background color or image may also be essential. This article will demonstrate how to programmatically add a background color or image to a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add a Background Color to a Word Document

Adding a background color to a Word document is quite simple. You just need to set the background type as color and then choose a color as the background. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Set the background type as color using Document.Background.Type property.
  • Set a background color for the document using Document.Background.Color property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document 
            document.LoadFromFile("Test.docx");

            //Set the background type as color
            document.Background.Type = BackgroundType.Color;

            //Set the background color
            document.Background.Color = Color.AliceBlue;

            //Save the document
            document.SaveToFile("PureColorBackground.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Add Background Color or Image to Word Documents

Add a Gradient Background to a Word Document

Adding gradient background requires more steps. You need to set the background type as gradient, choose two colors, and then set shading variant and style. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Set the background type as gradient using Document.Background.Type property.
  • Get the background gradient using Document.Background.Gradient property.
  • Select two colors using BackgroundGradient.Color1 and BackgroundGradient.Color2 properties.
  • Set shading variant and style for the gradient using BackgroundGradient.ShadingVariant and BackgroundGradient. ShadingStyle properties.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document 
            document.LoadFromFile("Test.docx");

            //Set the background type as gradient
            document.Background.Type = BackgroundType.Gradient;

            //Get the background gradient
            BackgroundGradient gradient = document.Background.Gradient;

            //Select two colors
            gradient.Color1 = Color.White;
            gradient.Color2 = Color.LightBlue;

            //Set shading variant and style for the gradient
            gradient.ShadingVariant = GradientShadingVariant.ShadingDown;
            gradient.ShadingStyle = GradientShadingStyle.Horizontal;

            //Save the document
            document.SaveToFile("AddGradientBackground.docx", FileFormat.Docx);

        }
    }
}

C#/VB.NET: Add Background Color or Image to Word Documents

Insert a Background Image to a Word Document

To insert a background image to a Word document, you need to set the background type as picture, and then insert a picture as the background. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Set the background type as picture using Document.Background.Type property.
  • Set a background picture for the document using Document.Background.Picture property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetImageBackground
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                //Create a Document instance
                Document document = new Document();

                //Load a sample Word document 
                document.LoadFromFile("Test.docx");

                //Set the background type as picture
                document.Background.Type = BackgroundType.Picture;

                //Set background picture
                document.Background.Picture = Image.FromFile("background.jpg");

                //Save the document
                document.SaveToFile("AddBackgroundPicture.docx", FileFormat.Docx);
            }

        }
    }
}

C#/VB.NET: Add Background Color or Image to Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 31 May 2023 09:12