Skip to main content

Merge PDFs and Other Resources


Merge multiple PDF documents into one combined document.

The DynamicPDF API easily merges multiple PDFs into a combined PDF. You can also merge Word, HTML, and images into a combined PDF.

tip

Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.

info

Refer to the other solutions on how to convert HTML, images, and Word documents to PDFs. Each type is an input, and multiple inputs are combined together, in the order they occur in the inputs array to form the combined PDF.

Calling Endpoint Directly

The following JSON instructions document illustrates combining three PDF documents into a combined PDF.

  1. Create an instructions JSON document and add an inputs array.
  2. Add the inputs to the array in the order in which they are to be merged.
{
"author": "Alex Smith",
"inputs": [
{
"type": "pdf",
"resourceName": "DocumentA.pdf",
"startPage": 1,
"pageCount": 1
},
{
"type": "pdf",
"resourceName": "DocumentB.pdf"
},
{
"type": "pdf",
"resourceName": "samples/merge-pdfs-pdf-endpoint/DocumentC.pdf"
}
]
}

Run In Postman

curl --location 'https://api.dynamicpdf.com/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--
--form 'Instructions=@C:/temp/solutions/merge-pdfs/instructions.json'
--form 'Resource=@C:/temp/solutions/merge-pdfs/DocumentA.pdf'
--form 'Resource=@C:/temp/solutions/merge-pdfs/DocumentB.pdf'
--form 'Resource=DocumentC.pdf'

Calling Endpoint Using Client Library

The following examples illustrate converting three PDFs to a combined PDF using the six different client libraries.

using System;
using System.IO;

namespace MergePdfs
{
class Program
{

static void Main(string[] args)
{
Run("DP.xxx-api-key-xxx", "C:/temp/dynamicpdf-api-samples/");
}

public static void Run(String apiKey, String basePath)
{
// create new pdf instance and set api key
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;

//add pdf and get reference to PdfInput then set pages to append
var inputA = pdf.AddPdf(new PdfResource(basePath + "DocumentA.pdf"));
inputA.StartPage = 1;
inputA.PageCount = 1;

// add all of pdf from local system
pdf.AddPdf(new PdfResource(basePath + "DocumentB.pdf"));

// add pdf from cloud in resource manager
pdf.AddPdf("/samples/merge-pdfs-pdf-endpoint/DocumentC.pdf");

//call the pdf endpoint and get response
PdfResponse pdfResponse = pdf.Process();

// if successful, save merged pdf to file otherwise print JSON error
if(pdfResponse.IsSuccessful)
{
File.WriteAllBytes(Path.Combine(basePath, "merge-pdfs-output.pdf"), pdfResponse.Content);
} else
{
Console.WriteLine(pdfResponse.ErrorJson);
}

// process and get response from pdf endpoint
PdfResponse pdfResponse = pdf.Process();

// if the response is successful, save merged pdf to file if error, then printout JSON error
if(pdfResponse.IsSuccessful)
{
File.WriteAllBytes(Path.Combine(basePath, "merge-output.pdf"), pdfResponse.Content);
} else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}
}
info

The pdf endpoint takes a JSON instructions document that provides instructions for creating, transforming, merging, and formatting inputs into a combined PDF. Refer to documentation on the instructions schema for information on how to use the pdf endpoint.