How to Merge Different File Formats Together as a PDF in .NET with C#
In this tutorial, we'll use C# and the Merge PDFs API Tool to convert different file formats and merge them as one PDF. The complete Merge Different Formats Together as a PDF repository sample stays visible while we explain the request fields and response behavior a C# application must adapt safely.
Why Merge Different File Formats Together as a PDF in .NET with C#?
Mixed-format deliverables require a normalization step because Merge PDFs operates on PDF resources. The .NET flow converts each non-PDF source, reads its managed ID, and then assembles those outputs without saving intermediate files locally.
For example, a project-delivery service may need to combine a PNG diagram with a PowerPoint briefing. The sample sends both files through /pdf, then passes their outputId values to /merged-pdf to produce the ordered package.
Each multipart group supplies an ID, input type, and page selection. Preserving the same order across those repeated fields is important because it defines both which resources participate and where their pages appear.
C# Code Example
/* * What this sample does: * - Converts two different file types to PDF via multipart, then merges them. * - Routed from Program.cs as: `dotnet run -- merge-different-file-types`. * * Setup (environment): * - Copy .env.example to .env * - Set PDFREST_API_KEY=your_api_key_here * - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use: * PDFREST_URL=https://eu-api.pdfrest.com * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work * * Usage: * dotnet run -- merge-different-file-types image.png slides.pptx * * Output: * - Prints JSON responses for the two pdf conversions and the final merge. */ using Newtonsoft.Json.Linq; using System.Text; namespace Samples.ComplexFlowExamples { public static class MergeDifferentFileTypes { public static async Task Execute(string[] args) { if (args == null || args.Length < 2) { Console.Error.WriteLine("merge-different-file-types requires "); Environment.Exit(1); return; } var imagePath = args[0]; var pptPath = args[1]; if (!File.Exists(imagePath) || !File.Exists(pptPath)) { Console.Error.WriteLine("One or more input files not found."); Environment.Exit(1); return; } var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY"); if (string.IsNullOrWhiteSpace(apiKey)) { Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY"); Environment.Exit(1); return; } var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com"; using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) }) { // Begin first PDF conversion using var imageRequest = new HttpRequestMessage(HttpMethod.Post, "pdf"); imageRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); imageRequest.Headers.Accept.Add(new("application/json")); var imageMultipartContent = new MultipartFormDataContent(); var imageByteArray = File.ReadAllBytes(imagePath); var imageByteAryContent = new ByteArrayContent(imageByteArray); imageMultipartContent.Add(imageByteAryContent, "file", Path.GetFileName(imagePath)); imageByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream"); imageRequest.Content = imageMultipartContent; var imageResponse = await httpClient.SendAsync(imageRequest); var imageResult = await imageResponse.Content.ReadAsStringAsync(); Console.WriteLine("Image to PDF response received."); Console.WriteLine(imageResult); dynamic imageResponseData = JObject.Parse(imageResult); string imageID = imageResponseData.outputId; // Begin second PDF conversion using var powerpointRequest = new HttpRequestMessage(HttpMethod.Post, "pdf"); powerpointRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); powerpointRequest.Headers.Accept.Add(new("application/json")); var powerpointMultipartContent = new MultipartFormDataContent(); var powerpointByteArray = File.ReadAllBytes(pptPath); var powerpointByteAryContent = new ByteArrayContent(powerpointByteArray); powerpointMultipartContent.Add(powerpointByteAryContent, "file", Path.GetFileName(pptPath)); powerpointByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream"); powerpointRequest.Content = powerpointMultipartContent; var powerpointResponse = await httpClient.SendAsync(powerpointRequest); var powerpointResult = await powerpointResponse.Content.ReadAsStringAsync(); Console.WriteLine("powerpoint to PDF response received."); Console.WriteLine(powerpointResult); dynamic powerpointResponseData = JObject.Parse(powerpointResult); string powerpointID = powerpointResponseData.outputId; // Begin file merge using var request = new HttpRequestMessage(HttpMethod.Post, "merged-pdf"); request.Headers.TryAddWithoutValidation("Api-Key", apiKey); request.Headers.Accept.Add(new("application/json")); var multipartContent = new MultipartFormDataContent(); var imageByteArrayID = new ByteArrayContent(Encoding.UTF8.GetBytes(imageID)); multipartContent.Add(imageByteArrayID, "id[]"); var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("id")); multipartContent.Add(byteArrayOption, "type[]"); var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("all")); multipartContent.Add(byteArrayOption2, "pages[]"); var powerpointByteArrayID = new ByteArrayContent(Encoding.UTF8.GetBytes(powerpointID)); multipartContent.Add(powerpointByteArrayID, "id[]"); var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("id")); multipartContent.Add(byteArrayOption3, "type[]"); var byteArrayOption4 = new ByteArrayContent(Encoding.UTF8.GetBytes("all")); multipartContent.Add(byteArrayOption4, "pages[]"); request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("Merge response received."); Console.WriteLine(apiResult); } } } }
Source for Merge Different Formats Together as a PDF: View the C# sample on GitHub.
Breaking Down the Code
Load the .NET request and JSON types
using Newtonsoft.Json.Linq; using System.Text;
The Merge Different Formats Together as a PDF sample uses .NET HTTP types for requests and Newtonsoft.Json.Linq for response parsing. Its supporting namespaces cover the text and multipart values needed by this C# Merge Different Formats Together as a PDF workflow.
Validate input and load runtime configuration
Console.Error.WriteLine("One or more input files not found.");
Environment.Exit(1);
return;
}
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
Environment.Exit(1);
return;
}
var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
The .NET Merge Different Formats Together as a PDF example reads PDFREST_API_KEY and optionally PDFREST_URL from the environment. That arrangement keeps C# Merge Different Formats Together as a PDF credentials out of source and permits US or EU routing without recompilation.
Convert the first source and read its ID
// Begin first PDF conversion
using var imageRequest = new HttpRequestMessage(HttpMethod.Post, "pdf");
imageRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
imageRequest.Headers.Accept.Add(new("application/json"));
var imageMultipartContent = new MultipartFormDataContent();
var imageByteArray = File.ReadAllBytes(imagePath);
var imageByteAryContent = new ByteArrayContent(imageByteArray);
imageMultipartContent.Add(imageByteAryContent, "file", Path.GetFileName(imagePath));
imageByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
imageRequest.Content = imageMultipartContent;
var imageResponse = await httpClient.SendAsync(imageRequest);
var imageResult = await imageResponse.Content.ReadAsStringAsync();
Console.WriteLine("Image to PDF response received.");
Console.WriteLine(imageResult);
dynamic imageResponseData = JObject.Parse(imageResult);
string imageID = imageResponseData.outputId;
The first C# conversion sends the image to /pdf and captures its outputId. That ID becomes the first Merge Different Formats Together as a PDF input without an intermediate download back to C#.
Convert the presentation and read its ID
// Begin second PDF conversion
using var powerpointRequest = new HttpRequestMessage(HttpMethod.Post, "pdf");
powerpointRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
powerpointRequest.Headers.Accept.Add(new("application/json"));
var powerpointMultipartContent = new MultipartFormDataContent();
var powerpointByteArray = File.ReadAllBytes(pptPath);
var powerpointByteAryContent = new ByteArrayContent(powerpointByteArray);
powerpointMultipartContent.Add(powerpointByteAryContent, "file", Path.GetFileName(pptPath));
powerpointByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
powerpointRequest.Content = powerpointMultipartContent;
var powerpointResponse = await httpClient.SendAsync(powerpointRequest);
var powerpointResult = await powerpointResponse.Content.ReadAsStringAsync();
Console.WriteLine("powerpoint to PDF response received.");
Console.WriteLine(powerpointResult);
dynamic powerpointResponseData = JObject.Parse(powerpointResult);
string powerpointID = powerpointResponseData.outputId;
The second C# conversion repeats the normalization step for the presentation. The Merge Different Formats Together as a PDF workflow must receive this presentation outputId before C# constructs the complete merge request.
Build the ordered merge body
// Begin file merge
using var request = new HttpRequestMessage(HttpMethod.Post, "merged-pdf");
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
request.Headers.Accept.Add(new("application/json"));
var multipartContent = new MultipartFormDataContent();
var imageByteArrayID = new ByteArrayContent(Encoding.UTF8.GetBytes(imageID));
multipartContent.Add(imageByteArrayID, "id[]");
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("id"));
multipartContent.Add(byteArrayOption, "type[]");
var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("all"));
multipartContent.Add(byteArrayOption2, "pages[]");
var powerpointByteArrayID = new ByteArrayContent(Encoding.UTF8.GetBytes(powerpointID));
multipartContent.Add(powerpointByteArrayID, "id[]");
var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("id"));
multipartContent.Add(byteArrayOption3, "type[]");
var byteArrayOption4 = new ByteArrayContent(Encoding.UTF8.GetBytes("all"));
multipartContent.Add(byteArrayOption4, "pages[]");
request.Content = multipartContent;
var response = await httpClient.SendAsync(request);
var apiResult = await response.Content.ReadAsStringAsync();
Console.WriteLine("Merge response received.");
Console.WriteLine(apiResult);
The C# merge body groups each resource ID with its input type and selected pages. Their repeated-field order controls how the converted sources appear in the Merge Different Formats Together as a PDF result returned to C#.
Beyond the Tutorial
You have followed the complete C# sequence from source conversion through resource-ID handoff and final PDF assembly.
Validate the HTTP and JSON result after each conversion instead of assuming that an outputId is present. Keep the source-to-ID mapping with the job so retries and diagnostics cannot silently reverse the intended merge order.
Try the Merge Different Formats Together as a PDF workflow with a representative file in API Lab, then adapt the request in C#. The Merge PDFs API Tool documentation provides the complete C# context for Merge Different Formats Together as a PDF values, defaults, and limitations.