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.
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
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.
HttpClient
is created with the base address set to the pdfRest API.HttpRequestMessage
is then initialized with the POST method and the "word" endpoint.MultipartFormDataContent
object is created to hold the form data.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.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.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.