How to Rasterize PDF Files in .NET with C#
Why Rasterize PDF Files with C#?
The pdfRest Rasterize PDF API Tool allows developers to convert PDF documents into rasterized images using a simple API call. This tutorial will guide you through the process of sending an API request to the Rasterize PDF endpoint using C#. By leveraging this tool, you can easily integrate PDF rasterization capabilities into your C# applications, enabling you to convert PDFs into images for various use cases.
You might want to use the Rasterize PDF functionality to generate image previews of PDF documents for a web application. This can be particularly useful for displaying thumbnails or preview images of PDFs, allowing users to quickly view the content without downloading the entire document. Rasterizing PDFs can also be helpful when you need to ensure consistent rendering of PDF content across different platforms and devices.
Rasterize 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, "rasterized-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"); 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); } }
Source: GitHub
Breaking Down the Code
The code begins by setting up an HttpClient
with the base address of the pdfRest API:
var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }
This initializes the HTTP client to communicate with the pdfRest API server.
Next, a HttpRequestMessage
is created for the POST method to the "rasterized-pdf" endpoint:
var request = new HttpRequestMessage(HttpMethod.Post, "rasterized-pdf")
This specifies the API endpoint to which the request will be sent.
The API key is added to the request headers for authentication:
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
Replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
with your actual API key.
The request is set to accept JSON responses:
request.Headers.Accept.Add(new("application/json"))
This indicates that the client expects a JSON response from the server.
A MultipartFormDataContent
is created to hold the PDF file:
var multipartContent = new MultipartFormDataContent()
This is used to send the PDF file as part of the request payload.
The PDF file is read into a byte array and added to the multipart content:
var byteArray = File.ReadAllBytes("/path/to/file") var byteAryContent = new ByteArrayContent(byteArray) multipartContent.Add(byteAryContent, "file", "file_name.pdf")
Ensure to replace /path/to/file
and file_name.pdf
with the actual file path and name.
The content type for the file is set to "application/pdf":
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf")
This specifies that the file being sent is a PDF.
The request content is set to the multipart content, and the request is sent asynchronously:
request.Content = multipartContent var response = await httpClient.SendAsync(request)
This sends the request to the server and waits for a response.
The response content is read as a string and printed to the console:
var apiResult = await response.Content.ReadAsStringAsync() Console.WriteLine("API response received.") Console.WriteLine(apiResult)
This outputs the API response to the console for review.
Beyond the Tutorial
In this tutorial, you learned how to use the pdfRest Rasterize PDF API Tool to flatten each page of a PDF into a rasterized image using C#. This example demonstrated how to construct and send a multipart API request to the pdfRest server and handle the response.
To explore more about pdfRest API Tools, you can try out the API Lab. For detailed documentation, visit the API Reference Guide. Additionally, note that this example uses a multipart API call, and code samples using JSON payloads can be found here.