Sometimes, a Word document with a blank background may look tedious to readers. Adding a background color or picture to the document can help you make it more vivid and appealing. In this article, I will demonstrate how to add background color or picture to Word documents in C# and VB.NET.
- Add a Background Color to a Word Document
- Add a Gradient Background to a Word Document
- Add a Background Picture to a Word Document
Installation
To add background color or picture to Word documents, this article uses Free Spire.Doc for .NET. You can install Free Spire.Doc for .NET via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console, and then executing the following command:
PM> Install-Package FreeSpire.Doc
Alternatively, you can also download the DLL files of Free Spire.Doc for .NET from the official website, extract the package and then add the DLL files under the Bin folder to your project as references.
Add a Background Color to a Word Document in C# and VB.NET
The following are the steps to add a background color to a Word document:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Set the background type as color through Document.Background.Type property.
- Set the background color through Document.Background.Color property.
- Save the result document using Document.SaveToFile() method.
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetBackgroundColor
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Sample.docx");
//Set the background type as color
doc.Background.Type = BackgroundType.Color;
//Set the background color
doc.Background.Color = Color.LightSeaGreen;
//Save the result document
doc.SaveToFile("AddBackgroundColor.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace SetBackgroundColor
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim doc As Document = New Document()
'Load a Word document
doc.LoadFromFile("Sample.docx")
'Set the background type as color
doc.Background.Type = BackgroundType.Color
'Set the background color
doc.Background.Color = Color.LightSeaGreen
'Save the result document
doc.SaveToFile("AddBackgroundColor.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
Add a Gradient Background to a Word Document in C# and VB.NET
The following are the steps to add a gradient background to a Word document:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get the document background through Document.Background property.
- Set the background type as gradient through Background.Type property.
- Set two gradient colors through Background.Gradient.Color1 and Background.Gradient.Color2 properties.
- Set shading variant through Background.Gradient.ShadingVariant property.
- Set shading style through Background.Gradient.ShadingStyle property.
- Save the result document using Document.SaveToFile() method.
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetGradientBackground
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Sample.docx");
//Get the document background
Background background = doc.Background;
//Set the background type as Gradient
background.Type = BackgroundType.Gradient;
//Set two gradient colors
background.Gradient.Color1 = Color.LightSkyBlue;
background.Gradient.Color2 = Color.PaleGreen;
//Set shading variant
background.Gradient.ShadingVariant = GradientShadingVariant.ShadingMiddle;
//Set shading style
background.Gradient.ShadingStyle = GradientShadingStyle.FromCenter;
//Save the result document
doc.SaveToFile("AddGradientBackground.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace SetGradientBackground
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim doc As Document = New Document()
'Load a Word document
doc.LoadFromFile("Sample.docx")
'Get the document background
Dim background As Background = doc.Background
'Set the background type as Gradient
background.Type = BackgroundType.Gradient
'Set two gradient colors
background.Gradient.Color1 = Color.LightSkyBlue
background.Gradient.Color2 = Color.PaleGreen
'Set shading variant
background.Gradient.ShadingVariant = GradientShadingVariant.ShadingMiddle
'Set shading style
background.Gradient.ShadingStyle = GradientShadingStyle.FromCenter
'Save the result document
doc.SaveToFile("AddGradientBackground.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
Add a Background Picture to a Word Document in C# and VB.NET
The following are the steps to add a background picture to a Word document:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get the document background through Document.Background property.
- Set the background type as picture through Background.Type property.
- Set the background picture through Background.Picture property.
- Save the result document through Document.SaveToFile() method.
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetBackgroundPicture
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Sample.docx");
//Get the document background
Background background = doc.Background;
//Set the background type as picture
background.Type = BackgroundType.Picture;
//Set background picture
background.Picture = Image.FromFile("bgd.png");
//Save the result document
doc.SaveToFile("AddBackgroundPicture.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace SetBackgroundPicture
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim doc As Document = New Document()
'Load a Word document
doc.LoadFromFile("Sample.docx")
'Get the document background
Dim background As Background = doc.Background
'Set the background type as picture
background.Type = BackgroundType.Picture
'Set background picture
background.Picture = Image.FromFile("bgd.png")
'Save the result document
doc.SaveToFile("AddBackgroundPicture.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace