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 (InputStream inputStream1 = Files.newInputStream(new File("src/main/resources/combineFilesInput1.pdf").toPath());
InputStream inputStream2 = Files.newInputStream(new File("src/main/resources/combineFilesInput2.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
List<StreamAsset> streamAssets = new ArrayList<>();
streamAssets.add(new StreamAsset(inputStream1, PDFServicesMediaType.PDF.getMediaType()));
streamAssets.add(new StreamAsset(inputStream2, PDFServicesMediaType.PDF.getMediaType()));
List<Asset> assets = pdfServices.uploadAssets(streamAssets);
// Create parameters for the job
CombinePDFParams combinePDFParams = CombinePDFParams.combinePDFParamsBuilder()
.addAsset(assets.get(0))
.addAsset(assets.get(1))
.build();
// Creates a new job instance
CombinePDFJob combinePDFJob = new CombinePDFJob(combinePDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(combinePDFJob);
PDFServicesResponse<CombinePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CombinePDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/combineFilesOutput.pdf").toPath());
LOGGER.info("Saving asset at output/combineFilesOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} 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 (InputStream inputStream1 = Files.newInputStream(new File("src/main/resources/combineFileWithPageRangeInput1.pdf").toPath());
InputStream inputStream2 = Files.newInputStream(new File("src/main/resources/combineFileWithPageRangeInput2.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
List<StreamAsset> streamAssets = new ArrayList<>();
streamAssets.add(new StreamAsset(inputStream1, PDFServicesMediaType.PDF.getMediaType()));
streamAssets.add(new StreamAsset(inputStream2, PDFServicesMediaType.PDF.getMediaType()));
List<Asset> assets = pdfServices.uploadAssets(streamAssets);
PageRanges pageRangesForFirstFile = getPageRangeForFirstFile();
PageRanges pageRangesForSecondFile = getPageRangeForSecondFile();
// Create parameters for the job
CombinePDFParams combinePDFParams = CombinePDFParams.combinePDFParamsBuilder()
.addAsset(assets.get(0), pageRangesForFirstFile) // Add the first asset as input to the params, along with its page ranges
.addAsset(assets.get(1), pageRangesForSecondFile) // Add the second asset as input to the params, along with its page ranges
.build();
// Creates a new job instance
CombinePDFJob combinePDFJob = new CombinePDFJob(combinePDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(combinePDFJob);
PDFServicesResponse<CombinePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CombinePDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/combineFilesWithPageOptionsOutput.pdf").toPath());
LOGGER.info("Saving asset at output/combineFilesWithPageOptionsOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} 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.