This article demonstrates how to set background color and background image for a Word document using the BackgroundApi interface provided by Spire.Cloud.Word.
Set background color
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Model;
namespace SetBackgroundColor
{
class Program
{
static string appId = " App ID";
static string appKey = " App Key";
static string baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration instance based on your AppID and AppKey
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a BackgroundApi instance
static BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
static void Main(string[] args)
{
//Specify the input Word document
string name = "Example.docx";
//Specify the background color
Color color = new Color(127, 255, 170);
//Specify the password used to open the document, and it's "null" if nothing
string password = null;
//Specify the folder storing the input document, and it's "null" if nothing
string folder = "input";
//Use the 2G storage provided by E-iceblue, and it's "null" by default
string storage = null;
//Specify the file path of the output document
string destFilePath = "output/setBackgroundColor_output.docx";
//Call the SetBackgroundColor method to set background color for the document and save it to the specified path
backgroundApi.SetBackgroundColor(name, color, destFilePath, folder, storage, password);
}
}
}
Output
Set background image
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace SetBackgroundImage
{
class Program
{
static string appId = " App ID";
static string appKey = " App Key";
static string baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration instance based on your AppID and AppKey
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a BackgroundApi instance
static BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
static void Main(string[] args)
{
//Specify the input Word document
string name = " Example.docx";
//Specify the image path
string imagePath = "images/background.jpg";
//Specify the password used to open the document, and it's "null" if nothing
string password = null;
//Specify the folder storing the input document, and it's "null" if nothing
string folder = "input";
//Use the 2G storage provided by E-iceblue, and it's "null" by default
string storage = null;
//Specify the file path of the output document
string destFilePath = "output/setBackgroundImage_output.docx";
//Call the SetBackgroundImage method to set background image for the document and save it to the specified path
backgroundApi.SetBackgroundImage(name, imagePath, destFilePath, folder, storage, password);
}
}
}
Output