How to Watermark PDF in .NET with C#
Why Watermark PDF with C#?
The pdfRest Watermark PDF API Tool allows developers to easily add watermarks to PDF documents programmatically. This tutorial will demonstrate how to send an API call to the Watermark PDF endpoint using C#.
Watermarking PDFs can be crucial in various scenarios, such as adding a company logo to business documents, marking drafts with "CONFIDENTIAL" text, or simply personalizing documents for branding purposes.
Watermark PDF with C# Code Example
using System.Text; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "watermarked-pdf")) { request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json")); var multipartContent = new MultipartFormDataContent(); var byteArray = File.ReadAllBytes("/path/to/file.pdf"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pdf"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("Watermarked")); multipartContent.Add(byteArrayOption, "watermark_text"); request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult); } }
Source code reference: pdfRest API Samples on GitHub
Breaking Down the C# Code
The provided code snippet is a C# example of how to make a multipart/form-data POST request to the Watermark PDF endpoint of the pdfRest API.
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
This initializes an instance of HttpClient
with the base address set to the pdfRest API.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
The API key is added to the request headers for authentication. Replace the placeholder with your actual API key.
var byteArray = File.ReadAllBytes("/path/to/file.pdf"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pdf");
Here, the PDF file is read into a byte array, which is then added to the multipart content with the name "file" and a placeholder file name.
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("Watermarked")); multipartContent.Add(byteArrayOption, "watermark_text");
The watermark text "Watermarked" is encoded into a byte array and added to the multipart content with the name "watermark_text".
The request is then sent, and the response is read as a string. The response contains the result of the watermark operation.
Beyond the Tutorial
By following this tutorial, you've learned how to make a multipart/form-data POST request to the Watermark PDF endpoint of the pdfRest API using C#. You can now integrate this functionality into your applications to watermark PDF documents programmatically.
Feel free to explore and demo all of the pdfRest API Tools in the API Lab. For more details on the API and its capabilities, refer to the API Reference documentation.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdfRest API Samples on GitHub.