The pdfRest PDF to Excel API Tool allows developers to convert PDF documents into Excel files programmatically. This can be particularly useful in scenarios where you need to extract data from a PDF report and manipulate or analyze it within a spreadsheet. For example, a financial analyst might receive monthly reports in PDF format and need to convert them to Excel to perform further analysis or integrate with other financial models.
This tutorial will show how to send an API call to PDF to Excel using C#. By following this guide, you will learn how to make a request to the pdfRest API to convert a PDF file into an Excel document.
using System.Text; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "excel")) { 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"); request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult); } }
Reference: Source Code
This code snippet demonstrates how to send a PDF to Excel conversion request using the pdfRest API:
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
Here, an instance of HttpClient
is created with the base address set to the pdfRest API.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
The API key is added to the request headers. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
with your actual API key.
var multipartContent = new MultipartFormDataContent();
A new instance of MultipartFormDataContent
is created to hold the multipart form data for the request.
var byteArray = File.ReadAllBytes("/path/to/file.pdf"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pdf");
The PDF file is read into a byte array, and a new ByteArrayContent
is created with this array. It is then added to the multipart content with the name "file" and the file name "file_name". Replace "/path/to/file.pdf"
with the actual file path and "file_name.pdf"
with the actual file name.
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
The content type of the file being uploaded is set to application/pdf
.
var response = await httpClient.SendAsync(request);
The request is sent asynchronously, and the response is awaited.
var apiResult = await response.Content.ReadAsStringAsync();
The response content is read as a string, which contains the result of the API call.
In this tutorial, you learned how to use C# to call the pdfRest PDF to Excel API to convert a PDF file into an Excel spreadsheet. By understanding how to make this API call, you can now integrate this functionality into your own applications and automate the conversion process.
I encourage you to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further details on other endpoints and features.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at JSON Payload Examples.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.