The pdfRest Convert to PDF API Tool offers a straightforward way to convert various document formats to PDF. This tutorial will guide you through the process of sending an API call to Convert to PDF using C#.
This functionality is particularly useful in scenarios where you need to standardize document formats for archiving, sharing, or printing. For instance, you might want to convert user-uploaded documents into PDFs before storing them on a server to ensure compatibility across different platforms and devices.
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_name.pptx"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pptx"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation"); 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: pdfRest API Samples on GitHub
This code snippet demonstrates how to make a multipart/form-data POST request to the pdfRest Convert to PDF API endpoint using C#:
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
This line initializes a new instance of the HttpClient class with the base address set to the pdfRest API endpoint.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
Here, we add the API key to the request headers. Replace the placeholder with your actual pdfRest API key.
var multipartContent = new MultipartFormDataContent();
This line creates a new instance of MultipartFormDataContent, which allows us to send files and data in the same request.
var byteArray = File.ReadAllBytes("/path/to/file_name.pptx"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pptx"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
This section reads the file you want to convert and adds it to the multipart content. The 'Content-Type' header is set based on the file format.
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("output.pdf")); multipartContent.Add(byteArrayOption, "output");
The 'output' parameter defines the name of the output PDF file.
var response = await httpClient.SendAsync(request);
This line sends the assembled request to the pdfRest API and waits for the response.
var apiResult = await response.Content.ReadAsStringAsync();
Finally, this line reads the response content as a string, which you can then use as needed.
By following the steps outlined above, you've learned how to convert documents to PDF using the pdfRest API and C#. This is just one of the many features provided by pdfRest. You're encouraged to explore and demo all of the pdfRest API Tools in the API Lab. For more detailed information about the API, refer to the API Reference documentation.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdfRest API Samples on GitHub.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.