How to Convert Microsoft Excel to PDF in .NET with C#
Why Convert Excel to PDF with C#?
The pdfRest Convert to PDF API Tool is a powerful resource for developers who need to programmatically convert Excel spreadsheets and other file types to PDF. This API provides a simple way to create PDFs from different source formats, which can be crucial for businesses that need to standardize documentation, such as converting invoices, reports, or user manuals to a more accessible and secure format. This tutorial will guide you through the process of sending an API call to Convert to PDF using C#.
For instance, imagine you are developing an application that allows users to upload documents in various formats, and you need to ensure all documents are stored in a uniform PDF format for ease of distribution and printing. The pdfRest API makes this conversion process seamless and efficient.
Convert Excel to PDF with 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, "pdf")) { 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.xlsx"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.xlsx"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.ms-excel"); var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("output.pdf")); 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: GitHub - datalogics/pdf-rest-api-samples
Breaking Down the Code
The code above creates a new HTTP client with the base address set to the pdfRest API. It then constructs a POST request to the "/pdf" endpoint:
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf")) { //... } }
The 'Api-Key' header is added without validation, and the 'Accept' header is set to expect a JSON response:
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json"));
A multipart form data content is created to hold the file and options for conversion:
var multipartContent = new MultipartFormDataContent();
The file to be converted is read as a byte array and added to the multipart content with the appropriate content type:
var byteArray = File.ReadAllBytes("/path/to/file.xlsx"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.xlsx"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.ms-excel");
Additional options, such as 'output' (the output file name), are also added to the request:
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("output.pdf")); multipartContent.Add(byteArrayOption, "output");
Finally, the request is sent, and the response is read as a string:
request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync();
Beyond the Tutorial
In this tutorial, we've successfully made an API call to the pdfRest Convert to PDF tool using C#. The code sends an Excel file to the API, which then converts it to a PDF and returns the result. This can be a significant step in automating document workflows within your applications.
To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on the API, consult the API Reference documentation.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - datalogics/pdf-rest-api-samples.