Combine PDF Files
Combine two or more documents into a single PDF file
Rest API
See our public API Reference for Combine PDF
Combine Files
This sample combines up to 20 PDF files into a single PDF file.
Please refer the API usage guide to understand how to use our APIs.
Java
.NET
Node JS
Rest API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.combinepdf.CombinePDF45 public class CombinePDF {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(CombinePDF.class);910 public static void main(String[] args) {11 try {12 // Initial setup, create credentials instance.13 Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()14 .withClientId("PDF_SERVICES_CLIENT_ID")15 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")16 .build();1718 //Create an ExecutionContext using credentials and create a new operation instance.19 ExecutionContext executionContext = ExecutionContext.create(credentials);20 CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();2122 // Add operation input from source files.23 FileRef combineSource1 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput1.pdf");24 FileRef combineSource2 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput2.pdf");25 combineFilesOperation.addInput(combineSource1);26 combineFilesOperation.addInput(combineSource2);2728 // Execute the operation.29 FileRef result = combineFilesOperation.execute(executionContext);3031 // Save the result to the specified location.32 result.saveAs("output/combineFilesOutput.pdf");3334 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {35 LOGGER.error("Exception encountered while executing operation", e);36 }37 }38 }39
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd CombinePDF/4// dotnet run CombinePDF.csproj56 namespace CombinePDF7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()19 .WithClientId("PDF_SERVICES_CLIENT_ID")20 .WithClientSecret("PDF_SERVICES_CLIENT_SECRET")21 .Build();2223 //Create an ExecutionContext using credentials and create a new operation instance.24 ExecutionContext executionContext = ExecutionContext.Create(credentials);25 CombineFilesOperation combineFilesOperation = CombineFilesOperation.CreateNew();2627 // Add operation input from source files.28 FileRef combineSource1 = FileRef.CreateFromLocalFile(@"combineFilesInput1.pdf");29 FileRef combineSource2 = FileRef.CreateFromLocalFile(@"combineFilesInput2.pdf");30 combineFilesOperation.AddInput(combineSource1);31 combineFilesOperation.AddInput(combineSource2);3233 // Execute the operation.34 FileRef result = combineFilesOperation.Execute(executionContext);3536 // Save the result to the specified location.37 result.SaveAs(Directory.GetCurrentDirectory() + "/output/combineFilesOutput.pdf");3839 }40 catch (ServiceUsageException ex)41 {42 log.Error("Exception encountered while executing operation", ex);43 }44 // Catch more errors here. . .45 }4647 static void ConfigureLogging()48 {49 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());50 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));51 }52 }53 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/combinepdf/combine-pdf.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .servicePrincipalCredentialsBuilder()11 .withClientId("PDF_SERVICES_CLIENT_ID")12 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")13 .build();1415 // Create an ExecutionContext using credentials and create a new operation instance.16 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),17 combineFilesOperation = PDFServicesSdk.CombineFiles.Operation.createNew();1819 // Set operation input from a source file.20 const combineSource1 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput1.pdf'),21 combineSource2 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput2.pdf');22 combineFilesOperation.addInput(combineSource1);23 combineFilesOperation.addInput(combineSource2);2425 // Execute the operation and Save the result to the specified location.26 combineFilesOperation.execute(executionContext)27 .then(result => result.saveAsFile('output/combineFilesOutput.pdf'))28 .catch(err => {29 if (err instanceof PDFServicesSdk.Error.ServiceApiError30 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {31 console.log('Exception encountered while executing operation', err);32 } else {33 console.log('Exception encountered while executing operation', err);34 }35 });36 } catch (err) {37 console.log('Exception encountered while executing operation', err);38 }
Copied to your clipboard1// Please refer our Rest API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Combine-PDF34curl --location --request POST 'https://pdf-services.adobe.io/operation/combinepdf' \5--header 'x-api-key: {{Placeholder for client_id}}' \6--header 'Content-Type: application/json' \7--header 'Authorization: Bearer {{Placeholder for token}}' \8--data-raw '{9 "assets": [10 {11 "assetID": "urn:aaid:AS:UE1:23c30ee0-2c4d-46d6-87f2-087832fca718"12 },13 {14 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718"15 }16 ]17}'1819// Legacy API can be found here20// https://documentcloud.adobe.com/document-services/index.html#post-combinePDF
Combine pages from multiple files
This combine sample combines specific pages from up to 20 different PDF files into a single PDF file. Optional arguments allow specifying page ranges for each file to combine in the output file.
Please refer the API usage guide to understand how to use our APIs.
Java
.NET
Node JS
Rest API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.combinepdf.CombinePDFWithPageRanges45 public class CombinePDFWithPageRanges {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(CombinePDFWithPageRanges.class);910 public static void main(String[] args) {1112 try {1314 // Initial setup, create credentials instance.15 Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()16 .withClientId("PDF_SERVICES_CLIENT_ID")17 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")18 .build();1920 //Create an ExecutionContext using credentials and create a new operation instance.21 ExecutionContext executionContext = ExecutionContext.create(credentials);22 CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();2324 // Create a FileRef instance from a local file.25 FileRef firstFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput1.pdf");26 PageRanges pageRangesForFirstFile = getPageRangeForFirstFile();27 // Add the first file as input to the operation, along with its page range.28 combineFilesOperation.addInput(firstFileToCombine, pageRangesForFirstFile);2930 // Create a second FileRef instance using a local file.31 FileRef secondFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput2.pdf");32 PageRanges pageRangesForSecondFile = getPageRangeForSecondFile();33 // Add the second file as input to the operation, along with its page range.34 combineFilesOperation.addInput(secondFileToCombine, pageRangesForSecondFile);3536 // Execute the operation.37 FileRef result = combineFilesOperation.execute(executionContext);3839 // Save the result to the specified location.40 result.saveAs("output/combineFilesWithPageOptionsOutput.pdf");4142 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {43 LOGGER.error("Exception encountered while executing operation", ex);44 }45 }4647 private static PageRanges getPageRangeForSecondFile() {48 // Specify which pages of the second file are to be included in the combined file.49 PageRanges pageRangesForSecondFile = new PageRanges();50 // Add all pages including and after page 3.51 pageRangesForSecondFile.addAllFrom(3);52 return pageRangesForSecondFile;53 }5455 private static PageRanges getPageRangeForFirstFile() {56 // Specify which pages of the first file are to be included in the combined file.57 PageRanges pageRangesForFirstFile = new PageRanges();58 // Add page 1.59 pageRangesForFirstFile.addSinglePage(1);60 // Add page 2.61 pageRangesForFirstFile.addSinglePage(2);62 // Add pages 3 to 4.63 pageRangesForFirstFile.addRange(3, 4);64 return pageRangesForFirstFile;65 }66 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd CombinePDFWithPageRanges/4// dotnet run CombinePDFWithPageRanges.csproj56 namespace CombinePDFWithPageRanges7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {1718 // Initial setup, create credentials instance.19 Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()20 .WithClientId("PDF_SERVICES_CLIENT_ID")21 .WithClientSecret("PDF_SERVICES_CLIENT_SECRET")22 .Build();2324 //Create an ExecutionContext using credentials and create a new operation instance.25 ExecutionContext executionContext = ExecutionContext.Create(credentials);26 CombineFilesOperation combineFilesOperation = CombineFilesOperation.CreateNew();2728 // Create a FileRef instance from a local file.29 FileRef firstFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput1.pdf");30 PageRanges pageRangesForFirstFile = GetPageRangeForFirstFile();31 // Add the first file as input to the operation, along with its page range.32 combineFilesOperation.AddInput(firstFileToCombine, pageRangesForFirstFile);3334 // Create a second FileRef instance using a local file.35 FileRef secondFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput2.pdf");36 PageRanges pageRangesForSecondFile = GetPageRangeForSecondFile();37 // Add the second file as input to the operation, along with its page range.38 combineFilesOperation.AddInput(secondFileToCombine, pageRangesForSecondFile);3940 // Execute the operation.41 FileRef result = combineFilesOperation.Execute(executionContext);4243 // Save the result to the specified location.44 result.SaveAs(Directory.GetCurrentDirectory() + "/output/combineFilesOutput.pdf");4546 }47 catch (ServiceUsageException ex)48 {49 log.Error("Exception encountered while executing operation", ex);50 }51 // Catch more errors here. . .52 }5354 private static PageRanges GetPageRangeForSecondFile()55 {56 // Specify which pages of the second file are to be included in the combined file.57 PageRanges pageRangesForSecondFile = new PageRanges();58 // Add all pages including and after page 5.59 pageRangesForSecondFile.AddAllFrom(5);60 return pageRangesForSecondFile;61 }6263 private static PageRanges GetPageRangeForFirstFile()64 {65 // Specify which pages of the first file are to be included in the combined file.66 PageRanges pageRangesForFirstFile = new PageRanges();67 // Add page 2.68 pageRangesForFirstFile.AddSinglePage(2);69 // Add page 3.70 pageRangesForFirstFile.AddSinglePage(3);71 // Add pages 5 to 7.72 pageRangesForFirstFile.AddRange(5, 7);73 return pageRangesForFirstFile;74 }7576 static void ConfigureLogging()77 {78 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());79 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));80 }81 }82 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/combinepdf/combine-pdf-with-page-ranges.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 const getPageRangesForFirstFile = () => {8 // Specify which pages of the first file are to be included in the combined file.9 const pageRangesForFirstFile = new PDFServicesSdk.PageRanges();10 // Add page 1.11 pageRangesForFirstFile.addSinglePage(1);12 // Add page 2.13 pageRangesForFirstFile.addSinglePage(2);14 // Add pages 3 to 4.15 pageRangesForFirstFile.addPageRange(3, 4);16 return pageRangesForFirstFile;17 };1819 const getPageRangesForSecondFile = () => {20 // Specify which pages of the second file are to be included in the combined file.21 const pageRangesForSecondFile = new PDFServicesSdk.PageRanges();22 // Add all pages including and after page 3.23 pageRangesForSecondFile.addAllFrom(3);24 return pageRangesForSecondFile;25 };2627 try {28 // Initial setup, create credentials instance.29 const credentials = PDFServicesSdk.Credentials30 .servicePrincipalCredentialsBuilder()31 .withClientId("PDF_SERVICES_CLIENT_ID")32 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")33 .build();3435 // Create an ExecutionContext using credentials and create a new operation instance.36 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),37 combineFilesOperation = PDFServicesSdk.CombineFiles.Operation.createNew();3839 // Create a FileRef instance from a local file.40 const combineSource1 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput1.pdf'),41 pageRangesForFirstFile = getPageRangesForFirstFile();42 // Add the first file as input to the operation, along with its page range.43 combineFilesOperation.addInput(combineSource1, pageRangesForFirstFile);4445 // Create a second FileRef instance using a local file.46 const combineSource2 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput2.pdf'),47 pageRangesForSecondFile = getPageRangesForSecondFile();48 // Add the second file as input to the operation, along with its page range.49 combineFilesOperation.addInput(combineSource2, pageRangesForSecondFile);5051 // Execute the operation and Save the result to the specified location.52 combineFilesOperation.execute(executionContext)53 .then(result => result.saveAsFile('output/combineFilesWithPageRangesOutput.pdf'))54 .catch(err => {55 if(err instanceof PDFServicesSdk.Error.ServiceApiError56 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {57 console.log('Exception encountered while executing operation', err);58 } else {59 console.log('Exception encountered while executing operation', err);60 }61 });62 } catch (err) {63 console.log('Exception encountered while executing operation', err);64 }
Copied to your clipboard1// Please refer our Rest API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Combine-PDF34curl --location --request POST 'https://pdf-services.adobe.io/operation/combinepdf' \5--header 'x-api-key: {{Placeholder for client_id}}' \6--header 'Content-Type: application/json' \7--header 'Authorization: Bearer {{Placeholder for token}}' \8--data-raw '{9 "assets": [10 {11 "assetID": "urn:aaid:AS:UE1:23c30ee0-2c4d-46d6-87f2-087832fca718",12 "pageRanges": [13 {14 "start": 1,15 "end": 316 }17 ]18 },19 {20 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718",21 "pageRanges": [22 {23 "start": 2,24 "end": 425 }26 ]27 }28 ]29}'3031// Legacy API can be found here32// https://documentcloud.adobe.com/document-services/index.html#post-combinePDF