How to Translate PDF Text in .NET with C#

Learn how to use the pdfRest Translate PDF API to translate PDF text to a different language with C#
Share this page

Why Translate PDF Text with C#?

The pdfRest Translate PDF API Tool is designed to help developers seamlessly integrate PDF translation capabilities into their applications. By leveraging this API, you can send a PDF document and receive the translated text in your desired language. This tutorial will guide you through the process of making an API call to the Translate PDF endpoint using C#.

Imagine you're working in a multinational company that frequently deals with documents in various languages. Using the Translate PDF API, you can automate the translation of these documents, ensuring that all team members, regardless of their native language, can access and understand the content. This can significantly enhance communication and collaboration across different regions.

Translate PDF Text with C# Code Example

/*
 * What this sample does:
 * - Translates PDF text via multipart/form-data.
 * - Routed from Program.cs as: `dotnet run -- translated-pdf-text-multipart `.
 *
 * Setup (environment):
 * - Copy .env.example to .env
 * - Set PDFREST_API_KEY=your_api_key_here
 * - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
 *     PDFREST_URL=https://eu-api.pdfrest.com
 *   For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 *
 * Usage:
 *   dotnet run -- translated-pdf-text-multipart /path/to/input.pdf
 *
 * Output:
 * - Prints the JSON response. Validation errors (args/env) exit non-zero.
 */

using System.Text;

namespace Samples.EndpointExamples.MultipartPayload
{
    public static class TranslatedPdfText
    {
        public static async Task Execute(string[] args)
        {
            if (args == null || args.Length < 1)
            {
                Console.Error.WriteLine("translated-pdf-text-multipart requires ");
                Environment.Exit(1);
                return;
            }
            var inputPath = args[0];
            if (!File.Exists(inputPath))
            {
                Console.Error.WriteLine($"File not found: {inputPath}");
                Environment.Exit(1);
                return;
            }
            var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
                Environment.Exit(1);
                return;
            }
            var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";

            using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) })
            using (var request = new HttpRequestMessage(HttpMethod.Post, "translated-pdf-text"))
            {
                request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
                request.Headers.Accept.Add(new("application/json"));
                var multipartContent = new MultipartFormDataContent();

                var byteArray = File.ReadAllBytes(inputPath);
                var byteAryContent = new ByteArrayContent(byteArray);
                multipartContent.Add(byteAryContent, "file", Path.GetFileName(inputPath));
                byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");

                // Translates text to American English. Format the output_language as a 2-3 character ISO 639 code, optionally with a region/script (e.g., 'en', 'es', 'zh-Hant', 'eng-US').
                var outLang = new ByteArrayContent(Encoding.UTF8.GetBytes("en-US"));
                multipartContent.Add(outLang, "output_language");

                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 checking if the required arguments are provided. If not, it exits the program with an error message:

if (args == null || args.Length < 1)
{
    Console.Error.WriteLine("translated-pdf-text-multipart requires ");
    Environment.Exit(1);
    return;
}

Next, it verifies if the input file exists:

var inputPath = args[0];
if (!File.Exists(inputPath))
{
    Console.Error.WriteLine($"File not found: {inputPath}");
    Environment.Exit(1);
    return;
}

The code retrieves the API key from the environment variables, which is essential for authenticating the API request:

var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
    Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
    Environment.Exit(1);
    return;
}

The base URL for the API is set, defaulting to https://api.pdfrest.com unless overridden by an environment variable:

var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";

A new HTTP client and request are created, with the API key added to the request headers:

using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) })
using (var request = new HttpRequestMessage(HttpMethod.Post, "translated-pdf-text"))
{
    request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
    request.Headers.Accept.Add(new("application/json"));

The PDF file is read into a byte array and added to the multipart form data content:

var byteArray = File.ReadAllBytes(inputPath);
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", Path.GetFileName(inputPath));
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");

The desired output language is specified using the ISO 639 code:

var outLang = new ByteArrayContent(Encoding.UTF8.GetBytes("en-US"));
multipartContent.Add(outLang, "output_language");

The request is sent, and the response is printed to the console:

var response = await httpClient.SendAsync(request);
var apiResult = await response.Content.ReadAsStringAsync();

Console.WriteLine("API response received.");
Console.WriteLine(apiResult);

Beyond the Tutorial

In this tutorial, you learned how to make an API call to the pdfRest Translate PDF endpoint using C#. This allows you to translate PDF text into a specified language seamlessly. To further explore the capabilities of pdfRest, you can try out all the API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.

Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit this GitHub repository.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.