How to Digitally Sign PDF Files in .NET with C#
Why Digitally Sign PDFs with C#?
The pdfRest Sign PDF API Tool is a powerful resource for developers looking to programmatically sign PDF documents. This tutorial will guide you through the process of sending an API call to the Sign PDF endpoint using C#. By leveraging this tool, you can automate the signing process, ensuring that your PDFs are authenticated and secure.
A business might need to sign multiple documents daily, such as contracts or agreements. Using the Sign PDF tool, a developer can integrate this functionality into their application, allowing for seamless and automated document signing, reducing manual effort and increasing efficiency.
Sign PDF with C# Code Example
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Text; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "signed-pdf")) { request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json")); var multipartContent = new MultipartFormDataContent(); var inputByteArray = File.ReadAllBytes("/path/to/input.pdf"); var inputByteArrayContent = new ByteArrayContent(inputByteArray); multipartContent.Add(inputByteArrayContent, "file", "input.pdf"); inputByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); var credByteArray = File.ReadAllBytes("/path/to/credentials.pfx"); var credByteArrayContent = new ByteArrayContent(credByteArray); multipartContent.Add(credByteArrayContent, "pfx_credential_file", "credentials.pfx"); credByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "application/x-pkcs12"); var passphraseByteArray = File.ReadAllBytes("/path/to/passphrase.txt"); var passphraseByteArrayContent = new ByteArrayContent(passphraseByteArray); multipartContent.Add(passphraseByteArrayContent, "pfx_passphrase_file", "passphrase.txt"); passphraseByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "text/plain"); var logoByteArray = File.ReadAllBytes("/path/to/logo.jpg"); var logoByteArrayContent = new ByteArrayContent(logoByteArray); multipartContent.Add(logoByteArrayContent, "logo_file", "logo.jpg"); logoByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "image/jpeg"); var signatureConfiguration = new JObject { ["type"] = "new", ["name"] = "esignature", ["logo_opacity"] = "0.5", ["location"] = new JObject { ["bottom_left"] = new JObject { ["x"] = "0", ["y"] = "0" }, ["top_right"] = new JObject { ["x"] = "216", ["y"] = "72" }, ["page"] = "1" }, ["display"] = new JObject { ["include_distinguished_name"] = "true", ["include_datetime"] = "true", ["contact"] = "My contact information", ["location"] = "My signing location", ["name"] = "John Doe", ["reason"] = "My reason for signing" } }; var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(signatureConfiguration.ToString(Formatting.None))); multipartContent.Add(byteArrayOption, "signature_configuration"); request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult); } }
Source: GitHub
Breaking Down the Code
The code begins by setting up an HttpClient
with a base address pointing to the pdfRest API. It then creates a HttpRequestMessage
for a POST request to the "signed-pdf" endpoint.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
This line adds the API key to the request headers, which is necessary for authentication.
var inputByteArray = File.ReadAllBytes("/path/to/input.pdf");
The input PDF file is read into a byte array and added to the multipart form data. The content type is set to "application/pdf".
var credByteArray = File.ReadAllBytes("/path/to/credentials.pfx");
Similarly, the PFX credential file is read and added to the form data, with the content type "application/x-pkcs12". This file is used for signing the PDF.
var passphraseByteArray = File.ReadAllBytes("/path/to/passphrase.txt");
The passphrase for the PFX file is read and added to the form data, with the content type "text/plain".
var logoByteArray = File.ReadAllBytes("/path/to/logo.jpg");
An optional logo file is added for the signature's visual representation, with the content type "image/jpeg".
var signatureConfiguration = new JObject
This JSON object defines the signature configuration, including its type, display settings, and location on the page. The location
object specifies the coordinates and page number for the signature.
Beyond the Tutorial
In this tutorial, you learned how to send an API call to the pdfRest Sign PDF endpoint using C#. This allows you to automate the process of signing PDF documents programmatically. To further explore pdfRest API tools, visit the API Lab and consult the API Reference Guide for more detailed information.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found on GitHub.