Edit in GitHubLog an issue

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.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.combinepdf.CombinePDF
public class CombinePDF {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(CombinePDF.class);
public static void main(String[] args) {
try {
// Initial setup, create credentials instance.
Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()
.withClientId("PDF_SERVICES_CLIENT_ID")
.withClientSecret("PDF_SERVICES_CLIENT_SECRET")
.build();
//Create an ExecutionContext using credentials and create a new operation instance.
ExecutionContext executionContext = ExecutionContext.create(credentials);
CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();
// Add operation input from source files.
FileRef combineSource1 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput1.pdf");
FileRef combineSource2 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput2.pdf");
combineFilesOperation.addInput(combineSource1);
combineFilesOperation.addInput(combineSource2);
// Execute the operation.
FileRef result = combineFilesOperation.execute(executionContext);
// Save the result to the specified location.
result.saveAs("output/combineFilesOutput.pdf");
} catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
LOGGER.error("Exception encountered while executing operation", e);
}
}
}

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.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.combinepdf.CombinePDFWithPageRanges
public class CombinePDFWithPageRanges {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(CombinePDFWithPageRanges.class);
public static void main(String[] args) {
try {
// Initial setup, create credentials instance.
Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()
.withClientId("PDF_SERVICES_CLIENT_ID")
.withClientSecret("PDF_SERVICES_CLIENT_SECRET")
.build();
//Create an ExecutionContext using credentials and create a new operation instance.
ExecutionContext executionContext = ExecutionContext.create(credentials);
CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();
// Create a FileRef instance from a local file.
FileRef firstFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput1.pdf");
PageRanges pageRangesForFirstFile = getPageRangeForFirstFile();
// Add the first file as input to the operation, along with its page range.
combineFilesOperation.addInput(firstFileToCombine, pageRangesForFirstFile);
// Create a second FileRef instance using a local file.
FileRef secondFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput2.pdf");
PageRanges pageRangesForSecondFile = getPageRangeForSecondFile();
// Add the second file as input to the operation, along with its page range.
combineFilesOperation.addInput(secondFileToCombine, pageRangesForSecondFile);
// Execute the operation.
FileRef result = combineFilesOperation.execute(executionContext);
// Save the result to the specified location.
result.saveAs("output/combineFilesWithPageOptionsOutput.pdf");
} catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
private static PageRanges getPageRangeForSecondFile() {
// Specify which pages of the second file are to be included in the combined file.
PageRanges pageRangesForSecondFile = new PageRanges();
// Add all pages including and after page 3.
pageRangesForSecondFile.addAllFrom(3);
return pageRangesForSecondFile;
}
private static PageRanges getPageRangeForFirstFile() {
// Specify which pages of the first file are to be included in the combined file.
PageRanges pageRangesForFirstFile = new PageRanges();
// Add page 1.
pageRangesForFirstFile.addSinglePage(1);
// Add page 2.
pageRangesForFirstFile.addSinglePage(2);
// Add pages 3 to 4.
pageRangesForFirstFile.addRange(3, 4);
return pageRangesForFirstFile;
}
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.