How to Add Text to PDF Files in .NET with C#
Why Add Text to PDF Files with C#?
The pdfRest Add to PDF API Tool is a powerful resource for developers looking to programmatically add text to PDF documents. This tutorial will guide you through the process of sending an API call to the Add to PDF endpoint using C#. By integrating this tool into your C# applications, you can automate the process of modifying PDF files, making it easier to manage and update documents without manual intervention.
A user might need to add text to a PDF for various reasons, such as personalizing documents with user-specific information, adding annotations or comments, or dynamically generating reports with specific data. For instance, a company could use this API to automatically add customer names and order details to invoices before sending them out, ensuring each document is customized and accurate.
Add Text to PDF Files 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, "pdf-with-added-text")) { request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json")); var multipartContent = new MultipartFormDataContent(); var byteArray = File.ReadAllBytes("/path/to/file"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); var text_option_array = new JArray(); var text_options = new JObject { ["font"] = "Times New Roman", ["max_width"] = "175", ["opacity"] = "1", ["page"] = "1", ["rotation"] = "0", ["text"] = "sample text in PDF", ["text_color_rgb"] = "0,0,0", ["text_size"] = "30", ["x"] = "72", ["y"] = "144" }; text_option_array.Add(text_options); var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(text_option_array))); multipartContent.Add(byteArrayOption, "text_objects"); 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 Repository
Breaking Down the Code
The code begins by setting up an HttpClient
with a base address pointing to the pdfRest API:
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
Next, an HttpRequestMessage
is created to send a POST request to the pdf-with-added-text
endpoint:
using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf-with-added-text"))
The API key is added to the request headers for authentication:
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
A MultipartFormDataContent
object is created to hold the file and text options. The PDF file is read as a byte array and added to the multipart content:
var byteArray = File.ReadAllBytes("/path/to/file"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
The text options are defined in a JSON object, specifying parameters like font, size, color, and position:
var text_options = new JObject { ["font"] = "Times New Roman", ["max_width"] = "175", ["opacity"] = "1", ["page"] = "1", ["rotation"] = "0", ["text"] = "sample text in PDF", ["text_color_rgb"] = "0,0,0", ["text_size"] = "30", ["x"] = "72", ["y"] = "144" };
These options are serialized to JSON and added to the multipart content:
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(text_option_array))); multipartContent.Add(byteArrayOption, "text_objects");
The request is sent, and the response is read and printed to the console:
var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync();
Beyond the Tutorial
In this tutorial, you've learned how to use C# to make an API call to pdfRest's Add to PDF endpoint, allowing you to add text to PDF files programmatically. This is a practical solution for automating PDF modifications in your applications.
To explore more capabilities of the pdfRest API Tools, visit the API Lab. For detailed documentation, refer to the API Reference Guide.
Note: This example demonstrates a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.