How to Add Attachments to PDF Files in .NET with C#
Why Add Attachments to PDF with C#?
The pdfRest Add to PDF API Tool allows users to programmatically add attachments to PDF documents. This tutorial will guide you through the process of sending an API call to Add to PDF using C#. Imagine you have an application where users can download PDF reports and you want to include related documents, such as source data in an XML file, as an attachment to the PDF.
This functionality can enhance the user experience by providing all relevant information bundled together in a single file.
Add Attachments 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-with-added-attachment")) { 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"); var byteArray2 = File.ReadAllBytes("/path/to/file"); var byteAryContent2 = new ByteArrayContent(byteArray2); multipartContent.Add(byteAryContent2, "file_to_attach", "file_name"); byteAryContent2.Headers.TryAddWithoutValidation("Content-Type", "application/xml"); // Update content type request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult); } }
Source: pdf-rest-api-samples on GitHub
Breaking Down the Code
This code snippet shows how to make a multipart/form-data POST request to the pdfRest API to attach a file to a PDF.
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
This line initializes an HttpClient instance with the base address set to the pdfRest API.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
This line adds your API key to the request headers for authentication purposes.
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");
Here, the code reads the bytes of the PDF file you want to add attachments to and adds it to the multipart content with the appropriate content type.
var byteArray2 = File.ReadAllBytes("/path/to/file"); var byteAryContent2 = new ByteArrayContent(byteArray2); multipartContent.Add(byteAryContent2, "file_to_attach", "file_name"); byteAryContent2.Headers.TryAddWithoutValidation("Content-Type", "application/xml");
Similarly, this snippet reads the bytes of the file you wish to attach and adds it to the multipart content, specifying its content type.
var response = await httpClient.SendAsync(request);
This line sends the assembled request to the pdfRest API and awaits the response.
Beyond the Tutorial
By following the steps above, you have successfully made an API call to add attachments to a PDF document using C#. This can be particularly useful for applications that need to distribute or archive documents with their associated data or supplementary materials.
You are 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 details.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples on GitHub.