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
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.combinepdf.CombinePDF
4
5 public class CombinePDF {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(CombinePDF.class);
9
10 public static void main(String[] args) {
11 try (InputStream inputStream1 = Files.newInputStream(new File("src/main/resources/combineFilesInput1.pdf").toPath());
12 InputStream inputStream2 = Files.newInputStream(new File("src/main/resources/combineFilesInput2.pdf").toPath())) {
13 // Initial setup, create credentials instance
14 Credentials credentials = new ServicePrincipalCredentials(
15 System.getenv("PDF_SERVICES_CLIENT_ID"),
16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
17
18 // Creates a PDF Services instance
19 PDFServices pdfServices = new PDFServices(credentials);
20
21 // Creates an asset(s) from source file(s) and upload
22 List<StreamAsset> streamAssets = new ArrayList<>();
23 streamAssets.add(new StreamAsset(inputStream1, PDFServicesMediaType.PDF.getMediaType()));
24 streamAssets.add(new StreamAsset(inputStream2, PDFServicesMediaType.PDF.getMediaType()));
25 List<Asset> assets = pdfServices.uploadAssets(streamAssets);
26
27 // Create parameters for the job
28 CombinePDFParams combinePDFParams = CombinePDFParams.combinePDFParamsBuilder()
29 .addAsset(assets.get(0))
30 .addAsset(assets.get(1))
31 .build();
32
33 // Creates a new job instance
34 CombinePDFJob combinePDFJob = new CombinePDFJob(combinePDFParams);
35
36 // Submit the job and gets the job result
37 String location = pdfServices.submit(combinePDFJob);
38 PDFServicesResponse<CombinePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CombinePDFResult.class);
39
40 // Get content from the resulting asset(s)
41 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
42 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
43
44 // Creates an output stream and copy stream asset's content to it
45 Files.createDirectories(Paths.get("output/"));
46 OutputStream outputStream = Files.newOutputStream(new File("output/combineFilesOutput.pdf").toPath());
47 LOGGER.info("Saving asset at output/combineFilesOutput.pdf");
48 IOUtils.copy(streamAsset.getInputStream(), outputStream);
49 outputStream.close();
50 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
51 LOGGER.error("Exception encountered while executing operation", e);
52 }
53 }
54 }
55

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
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.combinepdf.CombinePDFWithPageRanges
4
5 public class CombinePDFWithPageRanges {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(CombinePDFWithPageRanges.class);
9
10 public static void main(String[] args) {
11
12 try (InputStream inputStream1 = Files.newInputStream(new File("src/main/resources/combineFileWithPageRangeInput1.pdf").toPath());
13 InputStream inputStream2 = Files.newInputStream(new File("src/main/resources/combineFileWithPageRangeInput2.pdf").toPath())) {
14 // Initial setup, create credentials instance
15 Credentials credentials = new ServicePrincipalCredentials(
16 System.getenv("PDF_SERVICES_CLIENT_ID"),
17 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
18
19 // Creates a PDF Services instance
20 PDFServices pdfServices = new PDFServices(credentials);
21
22 // Creates an asset(s) from source file(s) and upload
23 List<StreamAsset> streamAssets = new ArrayList<>();
24 streamAssets.add(new StreamAsset(inputStream1, PDFServicesMediaType.PDF.getMediaType()));
25 streamAssets.add(new StreamAsset(inputStream2, PDFServicesMediaType.PDF.getMediaType()));
26 List<Asset> assets = pdfServices.uploadAssets(streamAssets);
27
28 PageRanges pageRangesForFirstFile = getPageRangeForFirstFile();
29 PageRanges pageRangesForSecondFile = getPageRangeForSecondFile();
30
31 // Create parameters for the job
32 CombinePDFParams combinePDFParams = CombinePDFParams.combinePDFParamsBuilder()
33 .addAsset(assets.get(0), pageRangesForFirstFile) // Add the first asset as input to the params, along with its page ranges
34 .addAsset(assets.get(1), pageRangesForSecondFile) // Add the second asset as input to the params, along with its page ranges
35 .build();
36
37 // Creates a new job instance
38 CombinePDFJob combinePDFJob = new CombinePDFJob(combinePDFParams);
39
40 // Submit the job and gets the job result
41 String location = pdfServices.submit(combinePDFJob);
42 PDFServicesResponse<CombinePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CombinePDFResult.class);
43
44 // Get content from the resulting asset(s)
45 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
46 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
47
48 // Creates an output stream and copy stream asset's content to it
49 Files.createDirectories(Paths.get("output/"));
50 OutputStream outputStream = Files.newOutputStream(new File("output/combineFilesWithPageOptionsOutput.pdf").toPath());
51 LOGGER.info("Saving asset at output/combineFilesWithPageOptionsOutput.pdf");
52 IOUtils.copy(streamAsset.getInputStream(), outputStream);
53 outputStream.close();
54 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
55 LOGGER.error("Exception encountered while executing operation", ex);
56 }
57 }
58
59 private static PageRanges getPageRangeForSecondFile() {
60 // Specify which pages of the second file are to be included in the combined file
61 PageRanges pageRangesForSecondFile = new PageRanges();
62 // Add all pages including and after page 3
63 pageRangesForSecondFile.addAllFrom(3);
64 return pageRangesForSecondFile;
65 }
66
67 private static PageRanges getPageRangeForFirstFile() {
68 // Specify which pages of the first file are to be included in the combined file
69 PageRanges pageRangesForFirstFile = new PageRanges();
70 // Add page 1
71 pageRangesForFirstFile.addSinglePage(1);
72 // Add page 2
73 pageRangesForFirstFile.addSinglePage(2);
74 // Add pages 3 to 4
75 pageRangesForFirstFile.addRange(3, 4);
76 return pageRangesForFirstFile;
77 }
78 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.