Edit in GitHubLog an issue

Split PDF

Split a PDF document into multiple smaller documents by simply specifying either the number of files, pages per file, or page ranges.

Rest API

See our public API Reference for 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.

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.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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/splitPDFInput.pdf").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
22
23 // Create parameters for the job
24 SplitPDFParams splitPDFParams = new SplitPDFParams();
25 // Sets the maximum number of pages each of the output files can have
26 splitPDFParams.setPageCount(2);
27
28 // Creates a new job instance
29 SplitPDFJob splitPDFJob = new SplitPDFJob(asset, splitPDFParams);
30
31 // Submit the job and gets the job result
32 String location = pdfServices.submit(splitPDFJob);
33 PDFServicesResponse<SplitPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, SplitPDFResult.class);
34
35 // Get content from the resulting asset(s)
36 List<Asset> resultAssets = pdfServicesResponse.getResult().getAssets();
37
38 Files.createDirectories(Paths.get("output/"));
39 int index = 0;
40 for (Asset resultAsset : resultAssets) {
41 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
42
43 // Creates an output stream and copy stream asset's content to it
44 OutputStream outputStream = Files.newOutputStream(new File("output/SplitPDFByNumberOfPagesOutput_" + index + ".pdf").toPath());
45 LOGGER.info("Saving asset at output/SplitPDFByNumberOfPagesOutput_" + index + ".pdf");
46 IOUtils.copy(streamAsset.getInputStream(), outputStream);
47 outputStream.close();
48 index++;
49 }
50 } catch (IOException| ServiceApiException | SDKException | ServiceUsageException e) {
51 LOGGER.error("Exception encountered while executing operation", e);
52 }
53 }
54
55 }

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.

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.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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/splitPDFInput.pdf").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
22
23 // Specify page ranges to split PDF
24 PageRanges pageRanges = getPageRanges();
25
26 // Create parameters for the job
27 SplitPDFParams splitPDFParams = new SplitPDFParams();
28 // Set the page ranges where each page range corresponds to a single output file
29 splitPDFParams.setPageRanges(pageRanges);
30
31 // Creates a new job instance
32 SplitPDFJob splitPDFJob = new SplitPDFJob(asset, splitPDFParams);
33
34 // Submit the job and gets the job result
35 String location = pdfServices.submit(splitPDFJob);
36 PDFServicesResponse<SplitPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, SplitPDFResult.class);
37
38 // Get content from the resulting asset(s)
39 List<Asset> resultAssets = pdfServicesResponse.getResult().getAssets();
40
41 Files.createDirectories(Paths.get("output/"));
42 int index = 0;
43 for (Asset resultAsset : resultAssets) {
44 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
45
46 // Creates an output stream and copy stream asset's content to it
47 OutputStream outputStream = Files.newOutputStream(new File("output/SplitPDFByPageRangesOutput_" + index + ".pdf").toPath());
48 LOGGER.info("Saving asset at output/SplitPDFByPageRangesOutput_" + index + ".pdf");
49 IOUtils.copy(streamAsset.getInputStream(), outputStream);
50 outputStream.close();
51 index++;
52 }
53 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
54 LOGGER.error("Exception encountered while executing operation", e);
55 }
56 }
57
58 private static PageRanges getPageRanges() {
59 // Specify page ranges
60 PageRanges pageRanges = new PageRanges();
61 // Add page 1
62 pageRanges.addSinglePage(1);
63
64 // Add pages 3 to 4
65 pageRanges.addRange(3, 4);
66 return pageRanges;
67 }
68
69 }
70

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).

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.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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/splitPDFInput.pdf").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
22
23 // Create parameters for the job
24 SplitPDFParams splitPDFParams = new SplitPDFParams();
25 // Sets the number of documents to split the input PDF file into
26 splitPDFParams.setFileCount(2);
27
28 // Creates a new job instance
29 SplitPDFJob splitPDFJob = new SplitPDFJob(asset, splitPDFParams);
30
31 // Submit the job and gets the job result
32 String location = pdfServices.submit(splitPDFJob);
33 PDFServicesResponse<SplitPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, SplitPDFResult.class);
34
35 // Get content from the resulting asset(s)
36 List<Asset> resultAssets = pdfServicesResponse.getResult().getAssets();
37
38 Files.createDirectories(Paths.get("output/"));
39 int index = 0;
40 for (Asset resultAsset : resultAssets) {
41 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
42
43 // Creates an output stream and copy stream asset's content to it
44 OutputStream outputStream = Files.newOutputStream(new File("output/SplitPDFIntoNumberOfFilesOutput_" + index + ".pdf").toPath());
45 LOGGER.info("Saving asset at output/SplitPDFIntoNumberOfFilesOutput_" + index + ".pdf");
46 IOUtils.copy(streamAsset.getInputStream(), outputStream);
47 outputStream.close();
48 index++;
49 }
50 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
51 LOGGER.error("Exception encountered while executing operation", e);
52 }
53 }
54
55 }
56
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.