Spire.Doc for .NET (337)
Children categories
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.
- Add a Background Color to a Word Document
- Add a Gradient Background to a Word Document
- Insert a Background Image to a Word Document
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);
}
}
}

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);
}
}
}

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);
}
}
}
}

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.
A comment in Word can contain rich elements such as text, image, OLE object, and etc. This article presents how we can insert a picture to a comment in Word using Spire.Doc.
Step 1: Initialize an instance of Document class and load a sample document.
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
Step 2: Get the third paragraph from section one.
Paragraph paragraph = doc.Sections[0].Paragraphs[2];
Step 3: Append a comment to the paragraph.
Comment comment = paragraph.AppendComment("This is a comment.");
comment.Format.Author = "E-iceblue";
Step 4: Insert an image to comment body.
DocPicture docPicture = new DocPicture(doc); Image img = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png"); docPicture.LoadImage(img); comment.Body.AddParagraph().ChildObjects.Add(docPicture);
Step 5: Save the file.
doc.SaveToFile("result.docx",FileFormat.Docx2013);
Output:

Full Code:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertPicture
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
Paragraph paragraph = doc.Sections[0].Paragraphs[2];
Comment comment = paragraph.AppendComment("This is a comment.");
comment.Format.Author = "E-iceblue";
DocPicture docPicture = new DocPicture(doc);
Image img = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
docPicture.LoadImage(img);
comment.Body.AddParagraph().ChildObjects.Add(docPicture);
doc.SaveToFile("result.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace InsertPicture
Class Program
Private Shared Sub Main(args As String())
Dim doc As New Document()
doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.docx")
Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(2)
Dim comment As Comment = paragraph.AppendComment("This is a comment.")
comment.Format.Author = "E-iceblue"
Dim docPicture As New DocPicture(doc)
Dim img As Image = Image.FromFile("C:\Users\Administrator\Desktop\logo.png")
docPicture.LoadImage(img)
comment.Body.AddParagraph().ChildObjects.Add(docPicture)
doc.SaveToFile("result.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
RTF is a document language used for encoding formatted text and graphics for easy transfer between applications. This article presents how to insert a piece of RTF encoded string to Word document using Spire.Doc.
Step 1: Initialize an instance of Document class, add a section to it.
Document doc = new Document(); Section section = doc.
Step 2: Add a paragraph to the section.
Paragraph para = section.AddParagraph();
Step 3: Declare a String variable to store the RTF string.
String rtfString = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}";
Step 4: Append RTF string to paragraph.
para.AppendRTF(rtfString);
Step 5: Save the file.
doc.SaveToFile("Output.docx");
Output:

Full Code:
using Spire.Doc;
using Spire.Doc.Documents;
using System;
namespace InsertRTF
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();
String rtfString = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}";
para.AppendRTF(rtfString);
doc.SaveToFile("Output.docx");
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace InsertRTF
Class Program
Private Shared Sub Main(args As String())
Dim doc As New Document()
Dim section As Section = doc.AddSection()
Dim para As Paragraph = section.AddParagraph()
Dim rtfString As [String] = "{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}"
para.AppendRTF(rtfString)
doc.SaveToFile("Output.docx")
End Sub
End Class
End Namespace