excel watermark

How to insert watermark in Excel(XLS, XLSX) using C#

Excel is a powerful tool that is widely used for managing and analyzing data. As it’s primarily known for its numerical and data manipulation features, it also offers many useful formatting and presentation tools. One such tool is the ability to insert watermarks, which can be used to add a background image or text to an Excel worksheets. Watermarks are useful for adding branding elements to a document, indicating the document’s status or version, or for adding a layer of protection against unauthorized copying or distribution. In this article, we will explore how to add and remove watermarks in Excel using C#, providing a step-by-step guide for those who want to enhance their Excel documents’ visual appeal and protect their valuable content.

Excel Watermark API

Aspose.Cells Cloud provides a simple and efficient way to work with Excel documents in the cloud, allowing you to streamline your workflow and automate many of your Excel-related tasks. Owing to its cross-platform compatibility, seamless integration, robust security, and cost-effectiveness, it is an amazing choice to work with Excel files in the cloud. Furthermore, this powerful API allows you to perform various operations on Excel documents programmatically, including adding and removing watermarks.

Now, in order to insert watermark in Excel using C# .NET, we need to add the reference of Aspose.Cells Cloud SDK for .NET in our project. Therefore, search Aspose.Cells-Cloud in NuGet packages manager and click the “Add Package” button. Furthermore, we also need to create an account over Dashboard using a valid email address.

Add Watermark to Excel using C#

Let’s have a quick look over the C# .NET code snippet to add a watermark to Excel workbook.

Given below are the details of above code snippet:

CellsApi cellsInstance = new CellsApi(clientID, clientSecret);

Create an object of CellsApi while passing client credentials as arguments.

var imageStream = System.IO.File.OpenRead(imageFile);

Read the input image to FileStream instance.

cellsInstance.UploadFile(input_Excel, File.OpenRead(input_Excel));

Upload input Excel to cloud storage.

var memoryStream = new MemoryStream();
imageStream.CopyTo(memoryStream);
byte[] imageBytes = memoryStream.ToArray();

The input FileStream is converted to ByteArray.

var response = cellsInstance.CellsWorkbookPutWorkbookBackground(input_Excel, imageBytes, null);

Finally, we call the API to add a watermark to Excel and save the resultant Workbook to the cloud storage.

The input Excel and Image files used in the above example can be downloaded from input.xls and Landscape.jpg respectively.

Remove Excel Watermark using C#

With Aspose.Cells Cloud, removing watermarks from Excel documents is fast and straightforward, allowing you to streamline your workflow and automate many of your Excel-related tasks. This task is useful when you need to update or replace the existing watermark, or if you want to remove it entirely. With the Aspose.Cells Cloud API, you can easily remove watermarks from all the Excel worksheets. The API will then remove the watermark from the specified worksheet, leaving the rest of the document unchanged.

In above code snippet, following code line is responsible for removing the watermark images from Excel workbook.

var response = cellsInstance.CellsWorkbookDeleteWorkbookBackground(input_Excel, null);

Set Excel Background Image using cURL Commands

Aspose.Cells Cloud provides a simple and easy-to-use REST API, allowing you to integrate Excel documents’ watermarking functionality into your workflows seamlessly. Furthermore, with the help of cURL commands, we can automate this operation and simplify our Excel-related tasks. Now, in order to add a watermark, we need to send a cURL POST request to the Aspose.Cells Cloud API with the watermark settings and the Excel document file as parameters.

However, first we need to have cURL installed on our system and then generate an accessToken based on client credentials:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Secondly, use the following command to upload the input Excel to cloud storage:

curl -v "https://api.aspose.cloud/v3.0/cells/storage/file/{filePath}" \
-X PUT \
-F file=@{localFilePath} \
-H "Content-Type: multipart/form-data" \
-H "Authorization: Bearer {accessToken}"

Replace {filePath} with the path where you want to store the file in the cloud storage, {localFilePath} with the path of an Excel on your local system, and {accessToken} with your Aspose Cloud access token (generated above).

Lastly, execute the following command to insert watermark in Excel workbook online:

curl -v "https://api.aspose.cloud/v3.0/cells/{excelFile}/background" \
-X PUT \
-H  "accept: multipart/form-data" \
-H  "authorization: Bearer {accessToken}" \
-H  "Content-Type: multipart/form-data" \
-d  "File":{"watermarkImage"}

Replace {excelFile} with the name of input Excel file in cloud storage Replace {accessToken} with the access token generated above Replace {watermarkImage} with raster image available on local drive

  • After successful operation, the watermarked Excel will be stored in the same cloud storage.

Concluding Remarks

Overall, adding and removing watermarks in Excel documents can help to protect your data and maintain document integrity. Aspose.Cells Cloud offers a powerful solution for accomplishing these tasks in a simple and streamlined way. By using the Aspose.Cells Cloud API and cURL commands, you can easily automate these processes and integrate them into your existing workflows. With additional features such as document conversion, formatting, and manipulation, Aspose.Cells Cloud is a valuable tool for managing your Excel documents in the cloud.

Please visit the following links to learn more about: