The pdfRest Flatten Layers API Tool is designed to simplify PDFs by merging layers into a single layer, which can be useful in various scenarios such as ensuring compatibility with older PDF viewers, reducing file size, or preparing a document for printing.
This tutorial will guide you through the process of sending an API call to Flatten Layers using C#. For example, a graphic designer might use Flatten Layers to ensure that a complex brochure with multiple layers prints correctly without any layering issues.
using System.Text; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "flattened-layers-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"); 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 code reference: GitHub - pdfRest API Samples
The code snippet above demonstrates how to use C# to call the pdfRest Flatten Layers API. Let's break down what each part of the code is doing:
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.
using (var request = new HttpRequestMessage(HttpMethod.Post, "flattened-layers-pdf"))
Here, we're creating a new POST request to the "flattened-layers-pdf" endpoint.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
This line adds your API key to the request headers. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual pdfRest API key.
var multipartContent = new MultipartFormDataContent();
This initializes a new MultipartFormDataContent object, which allows you to send files as part of the request.
var byteArray = File.ReadAllBytes("/path/to/file"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name");
These lines read the PDF file you want to flatten from the specified path, create a ByteArrayContent with the file's bytes, and add it to the multipart content with the form field name "file" and the filename "file_name".
var response = await httpClient.SendAsync(request);
This line sends the request to the pdfRest API and waits for the response.
var apiResult = await response.Content.ReadAsStringAsync();
Once the response is received, this line reads the content of the response as a string.
By following the steps in this tutorial, you've successfully called the pdfRest Flatten Layers API using C#. This allows you to programmatically flatten layers in a PDF document and handle the resulting file as needed. You're encouraged to demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/ for more information.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - pdfRest API Samples (JSON Payload).
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.