How to Convert PDF to Word in .NET with C#

Share this page

Why Convert PDF to Word with C#?

The pdfRest PDF to Word API Tool is a powerful service that allows users to convert PDF documents into editable Word files. This can be particularly useful in scenarios where you need to extract text from a PDF to edit, format, or repurpose the content without having to manually retype the entire document. For instance, a user might need to convert a PDF report into a Word document to apply company branding or to make substantial edits that are easier to perform in a word processor.

In this tutorial, we will demonstrate how to send an API call to the PDF to Word endpoint using C#. This will involve setting up an HTTP client, creating a multipart form data request, and handling the response.

Convert PDF to Word 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, "word"))
    {
        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("converted"));
        multipartContent.Add(byteArrayOption, "output");

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

        var apiResult = await response.Content.ReadAsStringAsync();

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

Source: pdf-rest-api-samples on GitHub

Breaking Down the Code

The code snippet above demonstrates how to make a POST request to the "word" endpoint of the pdfRest API to convert a PDF to a Word document.

  1. Firstly, an instance of HttpClient is created with the base address set to the pdfRest API.
  2. An HttpRequestMessage is then initialized with the POST method and the "word" endpoint.
  3. The API key is added to the request headers without validation. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual API key.
  4. The request header is set to accept JSON responses.
  5. A MultipartFormDataContent object is created to hold the form data.
  6. The PDF file is read into a byte array and added to the multipart content with the name "file" and the file name "file_name.pdf". The content type for this part is set to "application/pdf".
  7. An additional part is added to the multipart content, which indicates the desired output format ("converted").
  8. The multipart content is assigned to the request's content.
  9. The request is sent asynchronously, and the response is awaited.
  10. The response content is read as a string, which contains the API result.
  11. Finally, the API response is printed to the console.

Each field or parameter in the API request is crucial:

  • Api-Key: This is your unique API key provided by pdfRest for authentication.
  • file: This is the actual file content of the PDF you want to convert.
  • output: This field specifies the output format. In this case, "converted" indicates that the output should be a Word document.

Beyond the Tutorial

This tutorial has walked you through the process of setting up and executing an API call to convert a PDF document to a Word file using the pdfRest API in C#. By following the steps outlined above, you can integrate PDF to Word conversion functionality into your applications.

Feel free to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further information.

Note: This is an example of a multipart API call. Code samples using JSON payloads for the same endpoint can be found at pdf-rest-api-samples on GitHub.

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