Edit in GitHubLog an issue

Combine PDF Files

Combine Files#

This sample combines up to 20 PDF files into a single PDF file.

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 {
12 // Initial setup, create credentials instance.
13 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
14 .fromFile("pdfservices-api-credentials.json")
15 .build();
16
17 //Create an ExecutionContext using credentials and create a new operation instance.
18 ExecutionContext executionContext = ExecutionContext.create(credentials);
19 CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();
20
21 // 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);
26
27 // Execute the operation.
28 FileRef result = combineFilesOperation.execute(executionContext);
29
30 // Save the result to the specified location.
31 result.saveAs("output/combineFilesOutput.pdf");
32
33 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
34 LOGGER.error("Exception encountered while executing operation", e);
35 }
36 }
37 }
38

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.

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 {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json")
17 .build();
18
19 //Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();
22
23 // 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);
28
29 // 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);
34
35 // Execute the operation.
36 FileRef result = combineFilesOperation.execute(executionContext);
37
38 // Save the result to the specified location.
39 result.saveAs("output/combineFilesWithPageOptionsOutput.pdf");
40
41 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
42 LOGGER.error("Exception encountered while executing operation", ex);
43 }
44 }
45
46 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 }
53
54 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 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.