Skip to main content

Command Palette

Search for a command to run...

C# - Set Background Color and Image for Word

Published
3 min read

In .NET development scenarios, automated formatting of Word documents is a common requirement. Among these tasks, setting document backgrounds (either color or image) is a fundamental operation to enhance the visual presentation of documents. As a free Word document manipulation component, Free Spire.Doc for .NET enables the creation, editing, and formatting of Word documents without relying on Microsoft Office. This article will introduce how to use this component to set background colors or background images for Word documents in C#.


I. Environment Preparation

Free Spire.Doc for .NET supports quick installation via the NuGet Package Manager, which is the most convenient method:

  • Open Visual Studio and create any .NET project (e.g., Console App, ASP.NET Core, etc.);
  • Right-click the project → "Manage NuGet Packages" → Search for "Free Spire.Doc" → Install the latest version;
  • Alternatively, install it via the NuGet Command Line:
    Install-Package FreeSpire.Doc
    

II. Set Background Color for Word Documents

Free Spire.Doc provides the core Document.Background property. You need to first specify the background type as Color via Background.Type, then set the specific color through Background.Color.

Complete Code Example:

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 an existing local Word document
            document.LoadFromFile("Test.docx"); 

            // Specify the document background type
            document.Background.Type = BackgroundType.Color;

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

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

III. Set Background Image for Word Documents

To set a background image, simply change Background.Type to BackgroundType.Picture, then specify the image path via the Picture property.

Complete Code Example

using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Create a Document instance and load the source document
                Document document = new Document();
                document.LoadFromFile("Test.docx");

                // Specify the background type as "Picture"
                document.Background.Type = BackgroundType.Picture;
                // Set the background image
                document.Background.Picture = Image.FromFile("background.jpg");

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

                Console.WriteLine("Background image set successfully!");
            }
            catch (Exception ex)
            {
                // Catch exceptions such as non-existent files or invalid image formats
                Console.WriteLine($"Operation failed: {ex.Message}");
            }
        }
    }
}

Notes

  • Supported Image Formats: Common formats such as JPG, PNG, and BMP are all compatible. It is recommended to use images that match the document page size (A4 default: 210×297mm) to avoid display distortion.
  • Display Mode: Background images are displayed in "tiled" mode by default, and direct setting of "stretch" mode is not supported for now. If a stretch effect is required, adjust the image size via System.Drawing first before setting it as the background.
  • Path Issues: It is recommended to use absolute paths for images (e.g., D:\docs\background_img.png) to avoid file lookup failures caused by relative paths.

With the methods introduced in this article, developers can easily customize document backgrounds in C# applications. Although the free version has some page limitations, it remains a fully functional and easy-to-use solution for basic document processing needs.