Expose a REST API to Watermark Word Documents
How to Add Watermarks to Word Documents via ASP.NET Core REST API
This tutorial guides you on how to expose a REST API for adding watermarks to Word documents in ASP.NET Core. It includes step-by-step instructions, setup details, and deployment guidelines for major platforms.
Steps to Add Watermarks to Word Documents via REST API
- Set up an ASP.NET Core Web API project for adding watermarks.
- Install Aspose.Words for .NET via NuGet Package Manager.
- Create a controller with an endpoint to accept Word files and watermark text or image parameters.
- Write code to add text or image watermarks to Word documents.
- Test the API locally using tools like Postman or cURL.
- Deploy the API on Windows, Linux, or macOS environments.
- Configure Nginx or IIS for production deployment.
These steps provide a detailed approach to create and expose the watermark API.
Code Example: REST API for Adding Watermarks
Below is a runnable code snippet for exposing a REST API that adds text watermarks to Word documents:
using System.IO;
using System.Threading.Tasks;
using Aspose.Words;
using Aspose.Words.Drawing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace WatermarkAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WatermarkController : ControllerBase
{
[HttpPost("add-watermark")]
public async Task<IActionResult> AddWatermark(IFormFile file, [FromQuery] string watermarkText)
{
if (file == null || file.Length == 0 || string.IsNullOrWhiteSpace(watermarkText))
return BadRequest("Please upload a valid Word document and provide a watermark text.");
try
{
var tempFilePath = Path.GetTempFileName();
using (var stream = new FileStream(tempFilePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
Document doc = new Document(tempFilePath);
AddTextWatermark(doc, watermarkText);
var outputStream = new MemoryStream();
doc.Save(outputStream, SaveFormat.Docx);
outputStream.Position = 0;
return File(outputStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "watermarked.docx");
}
catch (System.Exception ex)
{
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
private void AddTextWatermark(Document doc, string text)
{
foreach (Section section in doc.Sections)
{
var watermark = new Shape(doc, ShapeType.TextPlainText)
{
TextPath = { Text = text, FontFamily = "Arial" },
Width = 300,
Height = 70,
Rotation = -40,
FillColor = System.Drawing.Color.LightGray,
StrokeColor = System.Drawing.Color.LightGray,
WrapType = WrapType.None,
BehindText = true,
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page,
Left = 100,
Top = 200
};
section.HeadersFooters[HeaderFooterType.HeaderPrimary]?.AppendChild(watermark);
}
}
}
}
Deployment on Major Platforms
Windows
- Install IIS and configure the site to point to the published application folder.
- Publish the application:
dotnet publish -c Release -o publish
Linux
- Install the ASP.NET Core runtime:
sudo apt-get install -y aspnetcore-runtime-7.0
- Publish the application:
dotnet publish -c Release -o publish
- Configure Nginx to proxy traffic to the Kestrel server.
macOS
- Install the .NET runtime from the official site.
- Publish and run:
dotnet publish -c Release -o publish cd publish dotnet WatermarkAPI.dll
Common Issues and Fixes
- Invalid Input Errors: Ensure that the uploaded file is a valid Word document and the watermark text is non-empty.
- Access Denied Errors: On Linux/macOS, grant proper permissions to the application folder.
chmod -R 755 /path/to/app
- Performance Issues: For large files, optimize memory usage by processing files directly from disk rather than streams.
This guide has shown you how to create a REST API for adding watermarks to Word documents using Aspose.Words for .NET and deploy it on all major platforms.