You can add a text or an image as a watermark to your Word documents programmatically on the cloud. The watermarks are used to identify the document’s creator or other information such as copyright or logo, etc. Usually, the watermark is used in the form of a superimposed image, logo, pattern, or text placed inside the document. In this article, you will learn how to add watermarks in Word documents using a REST API in C#.
The following topics shall be covered in this article:
- Watermark REST API and .NET SDK
- Add Text Watermark to Word Documents using REST API in C#
- Add Image Watermark to Word Documents using REST API
Watermark REST API and .NET SDK
For adding text or image watermark to DOCX files, I will be using the .NET SDK of GroupDocs.Watermark Cloud API. It allows you to programmatically add, remove, search and replace watermarks from images and documents of the supported file formats such as PDF, Microsoft Word, and Powerpoint. Moreover, you can control the customization of watermarks by specifying the text style, font, size, color, or position as per your requirements. Currently, it also provides Java SDK for the Cloud API.
You can install GroupDocs.Watermark Cloud into your Visual Studio project from the NuGet Package Manager or using the following command in the Package Manager console:
Install-Package GroupDocs.Watermark-Cloud
Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:
Add Text Watermark to Word Documents using REST API in C#
You can add any text as a watermark to your Word documents programmatically by following the simple steps mentioned below:
- Upload the DOCX file to the Cloud
- Add Text Watermark to DOCX using C#
- Download the watermarked file
Upload the Document
Firstly, upload the Word document to the Cloud using the code example given below:
As the result, the DOCX file will be uploaded to the cloud storage and will be available in the files section of your dashboard.
Adding Watermark in Word (DOC,DOCX) using C#
You can add a text watermark to the DOCX file programmatically by following the steps given below.
- Create an instance of the WatermarkApi
- Create an instance of the FileInfo
- Set the DOCX file path
- Create WatermarkOptions and set FileInfo
- Create TextWatermarkOptions
- Set Text, Font Family, Font Size, and the Text Alignment
- Set _Foreground _color of the watermark text
- Define the watermark Position
- Define WatermarkDetails and set TextWatermarkOptions and Position
- Create AddRequest with WatermarkOptions
- Get results by calling the WatermarkApi.add() method
The following code sample shows how to add text as a watermark to a Word document using a REST API in C#.
Here is the CURL example:
# First get JSON Web Token
# Please get your Client Id and Client Secret from https://dashboard.groupdocs.cloud/applications.
# Kindly place Client Id in "client_id" and Client Secret in "client_secret" argument.
curl -v "https://api.groupdocs.cloud/connect/token" \
-X POST \
-d "grant_type#client_credentials&client_id#xxxx&client_secret#xxxx" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
# cURL example to join several documents into one
curl -v "https://api.groupdocs.cloud/v1.0/watermark" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer <jwt token>" \
-d "{
"FileInfo": {
"FilePath": "documents\\sample.docx",
},
"WatermarkDetails": [
{
"TextWatermarkOptions": {
"Text": "Watermark text",
"FontFamilyName": "Arial",
"FontSize": 20.0,
"ForegroundColor": {
"Name": "red"
},
"TextAlignment": "center",
"Padding": {
"Left": 0,
"Top": 0,
"Right": 0,
"Bottom": 0
}
},
"Position": {
"X": 0.0,
"Y": 0.0,
"Width": 0.0,
"Height": 0.0,
"HorizontalAlignment": "Center",
"RotateAngle": 0.0,
"ConsiderParentMargins": false,
"IsBackground": false
}
}
]
}"
Download the Updated File
The above code sample will save the Word file with a text watermark on the cloud. You can download it using the following code sample:
Add Image Watermark to Word Documents using REST API
You can add an image or a logo as a watermark to your Word documents programmatically by following the steps given below.
- Create an instance of the WatermarkApi
- Create an instance of the FileInfo
- Set the DOCX file path
- Create WatermarkOptions and set FileInfo
- Create ImageWatermarkOptions
- Set FilePath of a PNG image to watermark with
- Define the watermark Position
- Create WatermarkDetails
- Set ImageWatermarkOptions and Position
- Set WatermarkDetails to List
- Create AddRequest with WatermarkOptions
- Get results by calling the WatermarkApi.add() method
The following code sample shows how to add an image as a watermark to DOCX using a REST API in C#. Please follow the steps mentioned earlier to upload and download the files.
Try Online
Please try the following free online Watermark tool, which is developed using the above API. https://products.groupdocs.app/watermark/docx
Conclusion
In conclusion, you have learned how to add text or image watermark to a Word document on the cloud. You have also learned how to programmatically upload the DOCX files on the cloud and then download the updated file from the cloud. You can learn more about GroupDocs.Watermark Cloud API from the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, feel free to contact support.