Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active July 19, 2021 06:45
Show Gist options
  • Save groupdocs-cloud-gists/89faffc8b8e3df867cd164985d67e4b2 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/89faffc8b8e3df867cd164985d67e4b2 to your computer and use it in GitHub Desktop.
Add Watermark to Word Documents using a REST API in C#
Add Watermark to Word Documents using a REST API in C#
1. Add Text Watermark to Word Documents using a REST API in C#
2. Add Image Watermark to Word Documents using a REST API in C#
// initialize api
var apiInstance = new WatermarkApi(configuration);
// input file path
var fileInfo = new FileInfo
{
FilePath = "sample.docx"
};
// create watermark options
var options = new WatermarkOptions()
{
FileInfo = fileInfo,
// define watermark details
WatermarkDetails = new List<WatermarkDetails>
{
new WatermarkDetails
{
// define image watermark
ImageWatermarkOptions = new ImageWatermarkOptions()
{
Image = new FileInfo {
FilePath = "logo.png"
}
},
// set watermark position
Position = new Position()
{
X = 180.0,
Y = 280.0
}
}
}
};
// create add request
var request = new AddRequest(options);
// add watermark
var response = apiInstance.Add(request);
Console.WriteLine("Resultant file path: " + response.DownloadUrl);
// initialize api
var apiInstance = new WatermarkApi(configuration);
// input file path
var fileInfo = new FileInfo
{
FilePath = "sample.docx"
};
// create watermark options
var options = new WatermarkOptions()
{
FileInfo = fileInfo,
// define watermark details
WatermarkDetails = new List<WatermarkDetails>
{
new WatermarkDetails
{
// define text watermark
TextWatermarkOptions = new TextWatermarkOptions
{
Text = "Copyright© 2021",
FontFamilyName = "Arial",
FontSize = 22d,
TextAlignment = "Center",
ForegroundColor = new Color()
{
Name = "Red"
}
},
// set watermark position
Position = new Position()
{
X = 230.0,
Y = 500.0
}
}
}
};
// create add request
var request = new AddRequest(options);
// add watermark
var response = apiInstance.Add(request);
Console.WriteLine("Resultant file path: " + response.DownloadUrl);
string clientId = "da0c487d-c1c0-45ae-b7bf-43eaf53c5ad5";
string clientSecret = "479db2b01dcb93a3d4d20efb16dea971";
string myStorage = "";
var configuration = new Configuration(clientId, clientSecret);
configuration.ApiBaseUrl = "https://api.groupdocs.cloud";
// initialize api
var fileApi = new FileApi(configuration);
// defaul file path on the cloud
var file = "watermark/added_watermark/sample_docx/sample.docx";
// create download file request
var downloadRequest = new DownloadFileRequest(file, myStorage);
// download file
Stream downloadResponse = fileApi.DownloadFile(downloadRequest);
// save file in working directory
using (var fileStream = System.IO.File.Create("C:\\Files\\DownloadedFile.docx"))
{
downloadResponse.Seek(0, SeekOrigin.Begin);
downloadResponse.CopyTo(fileStream);
}
// initialize api
var fileApi = new FileApi(configuration);
// open file
var fileStream = File.Open(@"C:\Files\sample.docx", FileMode.Open);
// create file upload request
var request = new UploadFileRequest("sample.docx", fileStream, MyStorage);
// upload file
fileApi.UploadFile(request);
fileStream.Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment