Spire.Cloud.Word offers the WatermarksApi interface to work with watermarks in Word. This article demonstrates how to add text and image watermarks to a Word document using Spire.Cloud.Word.
Add text watermark
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Model;
namespace AddTextWatermark
{
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 an WatermarksApi instance
static WatermarksApi watermarksApi = new WatermarksApi(wordConfiguration);
static void Main(string[] args)
{
//Specify the input Word document
string fileName = "WatermarkExample.docx";
//Specify the folder storing the 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 password used to open the document, and it's "null" if nothing
string password = null;
//Create a TextWatermark instance
TextWatermark body = new TextWatermark("Confidential")
{
Layout = TextWatermark.LayoutEnum.Diagonal,
Font = new Font(60, "Arial")
{
Color = new Color(100, 100, 100)
}
};
//Specify the file path of the output Word document
string destFilePath = "output/setTextWatermark_output.docx";
//Call the SetTextWatermark method to add text watermark to the document and save it to the specified path
watermarksApi.SetTextWatermark(name, body, destFilePath, folder, storage, password);
}
}
}
Output
Add image watermark
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace AddImageWatermark
{
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 an WatermarksApi instance
static WatermarksApi watermarksApi = new WatermarksApi(wordConfiguration);
static void Main(string[] args)
{
//Specify the input Word document
string fileName = "WatermarkExample.docx";
//Specify the image used as watermark
string imagePath = "images/logo.jpg";
//Specify the folder storing the 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 scaling and washout of the image watermark
int scaling = 120;
bool washout = true;
//Specify the password used to open the document, and it's "null" if nothing
string password = null;
//Specify the file path of the output Word document
string destFilePath = "output/setImageWatermark_output.docx";
//Call the SetImageWatermark method to add image watermark to the document and save it to the specified path
watermarksApi.SetImageWatermark(fileName, imagePath, destFilePath, folder, storage, scaling, washout, password);
}
}
}
Output