Combine PDF Files
Combine Files
This sample combines up to 20 PDF files into a single PDF file.
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.serviceAccountCredentialsBuilder()14 .fromFile("pdfservices-api-credentials.json")15 .build();1617 //Create an ExecutionContext using credentials and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();2021 // Add operation input from source files.22 FileRef combineSource1 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput1.pdf");23 FileRef combineSource2 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput2.pdf");24 combineFilesOperation.addInput(combineSource1);25 combineFilesOperation.addInput(combineSource2);2627 // Execute the operation.28 FileRef result = combineFilesOperation.execute(executionContext);2930 // Save the result to the specified location.31 result.saveAs("output/combineFilesOutput.pdf");3233 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {34 LOGGER.error("Exception encountered while executing operation", e);35 }36 }37 }38
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.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 //Create an ExecutionContext using credentials and create a new operation instance.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);24 CombineFilesOperation combineFilesOperation = CombineFilesOperation.CreateNew();2526 // Add operation input from source files.27 FileRef combineSource1 = FileRef.CreateFromLocalFile(@"combineFilesInput1.pdf");28 FileRef combineSource2 = FileRef.CreateFromLocalFile(@"combineFilesInput2.pdf");29 combineFilesOperation.AddInput(combineSource1);30 combineFilesOperation.AddInput(combineSource2);3132 // Execute the operation.33 FileRef result = combineFilesOperation.Execute(executionContext);3435 // Save the result to the specified location.36 result.SaveAs(Directory.GetCurrentDirectory() + "/output/combineFilesOutput.pdf");3738 }39 catch (ServiceUsageException ex)40 {41 log.Error("Exception encountered while executing operation", ex);42 }43 // Catch more errors here. . .44 }4546 static void ConfigureLogging()47 {48 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());49 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));50 }51 }52 }
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 .serviceAccountCredentialsBuilder()11 .fromFile("pdfservices-api-credentials.json")12 .build();1314 // Create an ExecutionContext using credentials and create a new operation instance.15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),16 combineFilesOperation = PDFServicesSdk.CombineFiles.Operation.createNew();1718 // Set operation input from a source file.19 const combineSource1 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput1.pdf'),20 combineSource2 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput2.pdf');21 combineFilesOperation.addInput(combineSource1);22 combineFilesOperation.addInput(combineSource2);2324 // Execute the operation and Save the result to the specified location.25 combineFilesOperation.execute(executionContext)26 .then(result => result.saveAsFile('output/combineFilesOutput.pdf'))27 .catch(err => {28 if (err instanceof PDFServicesSdk.Error.ServiceApiError29 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {30 console.log('Exception encountered while executing operation', err);31 } else {32 console.log('Exception encountered while executing operation', err);33 }34 });35 } catch (err) {36 console.log('Exception encountered while executing operation', err);37 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-combinePDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"documentsIn\": [{12 \"documentIn\": {13 \"cpf:location\": \"InputFile0\",14 \"dc:format\": \"application/pdf\"15 }16 }, {17 \"documentIn\": {18 \"cpf:location\": \"InputFile1\",19 \"dc:format\": \"application/pdf\"20 }21 }, {22 \"documentIn\": {23 \"cpf:location\": \"InputFile2\",24 \"dc:format\": \"application/pdf\"25 }26 }]27 },28 \"cpf:engine\": {29 \"repo:assetId\": \"urn:aaid:cpf:Service-916ee91c156b42349a7847a7d564fb13\"30 },31 \"cpf:outputs\": {32 \"documentOut\": {33 \"cpf:location\": \"OutputFile\",34 \"dc:format\": \"application/pdf\"35 }36 }37}"' \38--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"' \39--form 'InputFile1=@"{{Placeholder for input file (absolute path)}}"' \40--form 'InputFile2=@"{{Placeholder for input file (absolute path)}}"'
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.
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.serviceAccountCredentialsBuilder()16 .fromFile("pdfservices-api-credentials.json")17 .build();1819 //Create an ExecutionContext using credentials and create a new operation instance.20 ExecutionContext executionContext = ExecutionContext.create(credentials);21 CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();2223 // Create a FileRef instance from a local file.24 FileRef firstFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput1.pdf");25 PageRanges pageRangesForFirstFile = getPageRangeForFirstFile();26 // Add the first file as input to the operation, along with its page range.27 combineFilesOperation.addInput(firstFileToCombine, pageRangesForFirstFile);2829 // Create a second FileRef instance using a local file.30 FileRef secondFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput2.pdf");31 PageRanges pageRangesForSecondFile = getPageRangeForSecondFile();32 // Add the second file as input to the operation, along with its page range.33 combineFilesOperation.addInput(secondFileToCombine, pageRangesForSecondFile);3435 // Execute the operation.36 FileRef result = combineFilesOperation.execute(executionContext);3738 // Save the result to the specified location.39 result.saveAs("output/combineFilesWithPageOptionsOutput.pdf");4041 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {42 LOGGER.error("Exception encountered while executing operation", ex);43 }44 }4546 private static PageRanges getPageRangeForSecondFile() {47 // Specify which pages of the second file are to be included in the combined file.48 PageRanges pageRangesForSecondFile = new PageRanges();49 // Add all pages including and after page 3.50 pageRangesForSecondFile.addAllFrom(3);51 return pageRangesForSecondFile;52 }5354 private static PageRanges getPageRangeForFirstFile() {55 // Specify which pages of the first file are to be included in the combined file.56 PageRanges pageRangesForFirstFile = new PageRanges();57 // Add page 1.58 pageRangesForFirstFile.addSinglePage(1);59 // Add page 2.60 pageRangesForFirstFile.addSinglePage(2);61 // Add pages 3 to 4.62 pageRangesForFirstFile.addRange(3, 4);63 return pageRangesForFirstFile;64 }65 }
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.ServiceAccountCredentialsBuilder()20 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")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 // Create a FileRef instance from a local file.28 FileRef firstFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput1.pdf");29 PageRanges pageRangesForFirstFile = GetPageRangeForFirstFile();30 // Add the first file as input to the operation, along with its page range.31 combineFilesOperation.AddInput(firstFileToCombine, pageRangesForFirstFile);3233 // Create a second FileRef instance using a local file.34 FileRef secondFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput2.pdf");35 PageRanges pageRangesForSecondFile = GetPageRangeForSecondFile();36 // Add the second file as input to the operation, along with its page range.37 combineFilesOperation.AddInput(secondFileToCombine, pageRangesForSecondFile);3839 // Execute the operation.40 FileRef result = combineFilesOperation.Execute(executionContext);4142 // Save the result to the specified location.43 result.SaveAs(Directory.GetCurrentDirectory() + "/output/combineFilesOutput.pdf");4445 }46 catch (ServiceUsageException ex)47 {48 log.Error("Exception encountered while executing operation", ex);49 }50 // Catch more errors here. . .51 }5253 private static PageRanges GetPageRangeForSecondFile()54 {55 // Specify which pages of the second file are to be included in the combined file.56 PageRanges pageRangesForSecondFile = new PageRanges();57 // Add all pages including and after page 5.58 pageRangesForSecondFile.AddAllFrom(5);59 return pageRangesForSecondFile;60 }6162 private static PageRanges GetPageRangeForFirstFile()63 {64 // Specify which pages of the first file are to be included in the combined file.65 PageRanges pageRangesForFirstFile = new PageRanges();66 // Add page 2.67 pageRangesForFirstFile.AddSinglePage(2);68 // Add page 3.69 pageRangesForFirstFile.AddSinglePage(3);70 // Add pages 5 to 7.71 pageRangesForFirstFile.AddRange(5, 7);72 return pageRangesForFirstFile;73 }7475 static void ConfigureLogging()76 {77 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());78 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));79 }80 }81 }
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 .serviceAccountCredentialsBuilder()31 .fromFile("pdfservices-api-credentials.json")32 .build();3334 // Create an ExecutionContext using credentials and create a new operation instance.35 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),36 combineFilesOperation = PDFServicesSdk.CombineFiles.Operation.createNew();3738 // Create a FileRef instance from a local file.39 const combineSource1 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput1.pdf'),40 pageRangesForFirstFile = getPageRangesForFirstFile();41 // Add the first file as input to the operation, along with its page range.42 combineFilesOperation.addInput(combineSource1, pageRangesForFirstFile);4344 // Create a second FileRef instance using a local file.45 const combineSource2 = PDFServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput2.pdf'),46 pageRangesForSecondFile = getPageRangesForSecondFile();47 // Add the second file as input to the operation, along with its page range.48 combineFilesOperation.addInput(combineSource2, pageRangesForSecondFile);4950 // Execute the operation and Save the result to the specified location.51 combineFilesOperation.execute(executionContext)52 .then(result => result.saveAsFile('output/combineFilesWithPageRangesOutput.pdf'))53 .catch(err => {54 if(err instanceof PDFServicesSdk.Error.ServiceApiError55 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {56 console.log('Exception encountered while executing operation', err);57 } else {58 console.log('Exception encountered while executing operation', err);59 }60 });61 } catch (err) {62 console.log('Exception encountered while executing operation', err);63 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-combinePDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"documentsIn\": [{12 \"pageRanges\": {13 \"cpf:inline\": [{14 \"start\": 1,15 \"end\": 416 }]17 },18 \"documentIn\": {19 \"cpf:location\": \"InputFile0\",20 \"dc:format\": \"application/pdf\"21 }22 }, {23 \"pageRanges\": {24 \"cpf:inline\": [{25 \"start\": 1,26 \"end\": 2527 }]28 },29 \"documentIn\": {30 \"cpf:location\": \"InputFile1\",31 \"dc:format\": \"application/pdf\"32 }33 }, {34 \"pageRanges\": {35 \"cpf:inline\": [{36 \"start\": 137 }, {38 \"end\": 2539 }, {40 \"start\": 1,41 \"end\": 2542 }]43 },44 \"documentIn\": {45 \"cpf:location\": \"InputFile2\",46 \"dc:format\": \"application/pdf\"47 }48 }]49 },50 \"cpf:engine\": {51 \"repo:assetId\": \"urn:aaid:cpf:Service-916ee91c156b42349a7847a7d564fb13\"52 },53 \"cpf:outputs\": {54 \"documentOut\": {55 \"cpf:location\": \"OutputFile\",56 \"dc:format\": \"application/pdf\"57 }58 }59}"' \60--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"' \61--form 'InputFile1=@"{{Placeholder for input file (absolute path)}}"' \62--form 'InputFile2=@"{{Placeholder for input file (absolute path)}}"'