Edit in GitHubLog an issue

Split PDF

Split PDF by number of pages

This operation splits a PDF into multiple smaller documents. Simply use the page count to specify the maximum number of pages of each 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.splitpdf.SplitPDFByNumberOfPages
4
5 public class SplitPDFByNumberOfPages {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFByNumberOfPages.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 SplitPDFOperation splitPDFOperation = SplitPDFOperation.createNew();
20
21 // Set operation input from a source file.
22 FileRef source = FileRef.createFromLocalFile("src/main/resources/splitPDFInput.pdf");
23 splitPDFOperation.setInput(source);
24
25 // Set the maximum number of pages each of the output files can have.
26 splitPDFOperation.setPageCount(2);
27
28 // Execute the operation.
29 List result = splitPDFOperation.execute(executionContext);
30
31 // Save the result to the specified location.
32 int index = 0;
33 for (FileRef fileRef : result) {
34 fileRef.saveAs("output/SplitPDFByNumberOfPagesOutput_" + index + ".pdf");
35 index++;
36 }
37
38 } catch (IOException| ServiceApiException | SdkException | ServiceUsageException e) {
39 LOGGER.error("Exception encountered while executing operation", e);
40 }
41 }
42
43 }

Split PDF by page ranges

As an alternative to creating smaller PDFs with a set number of pages, you can split PDFs into multiple smaller documents by specifying page ranges where each page range corresponds to a single 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.splitpdf.SplitPDFByPageRanges
4
5 public class SplitPDFByPageRanges {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFByPageRanges.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 SplitPDFOperation splitPDFOperation = SplitPDFOperation.createNew();
20
21 // Set operation input from a source file.
22 FileRef source = FileRef.createFromLocalFile("src/main/resources/splitPDFInput.pdf");
23 splitPDFOperation.setInput(source);
24
25 // Set the page ranges where each page range corresponds to a single output file.
26 PageRanges pageRanges = getPageRanges();
27 splitPDFOperation.setPageRanges(pageRanges);
28
29 // Execute the operation.
30 List result = splitPDFOperation.execute(executionContext);
31
32 // Save the result to the specified location.
33 int index = 0;
34 for (FileRef fileRef : result) {
35 fileRef.saveAs("output/SplitPDFByPageRangesOutput_" + index + ".pdf");
36 index++;
37 }
38
39 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
40 LOGGER.error("Exception encountered while executing operation", e);
41 }
42 }
43
44 private static PageRanges getPageRanges() {
45 // Specify page ranges.
46 PageRanges pageRanges = new PageRanges();
47 // Add page 1.
48 pageRanges.addSinglePage(1);
49
50 // Add pages 3 to 4.
51 pageRanges.addRange(3, 4);
52 return pageRanges;
53 }
54
55 }
56

Split PDF into number of files

As an alternative to creating smaller PDFs by specifying a set number of pages or a page range, you can split PDFs by file count. In this case, the operation creates the specified number of files with each containing an identical number of pages (if possible).

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.splitpdf.SplitPDFIntoNumberOfFiles
4
5 public class SplitPDFIntoNumberOfFiles {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFIntoNumberOfFiles.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 SplitPDFOperation splitPDFOperation = SplitPDFOperation.createNew();
20
21 // Set operation input from a source file.
22 FileRef source = FileRef.createFromLocalFile("src/main/resources/splitPDFInput.pdf");
23 splitPDFOperation.setInput(source);
24
25 // Set the number of documents to split the input PDF file into.
26 splitPDFOperation.setFileCount(2);
27
28 // Execute the operation.
29 List result = splitPDFOperation.execute(executionContext);
30
31 // Save the result to the specified location.
32 int index = 0;
33 for (FileRef fileRef : result) {
34 fileRef.saveAs("output/SplitPDFIntoNumberOfFilesOutput_" + index + ".pdf");
35 index++;
36 }
37
38 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
39 LOGGER.error("Exception encountered while executing operation", e);
40 }
41 }
42
43 }
44
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.