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
// 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.splitpdf.SplitPDFByNumberOfPages
public class SplitPDFByNumberOfPages {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFByNumberOfPages.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/splitPDFInput.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
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
// Create parameters for the job
SplitPDFParams splitPDFParams = new SplitPDFParams();
// Sets the maximum number of pages each of the output files can have
splitPDFParams.setPageCount(2);
// Creates a new job instance
SplitPDFJob splitPDFJob = new SplitPDFJob(asset, splitPDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(splitPDFJob);
PDFServicesResponse<SplitPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, SplitPDFResult.class);
// Get content from the resulting asset(s)
List<Asset> resultAssets = pdfServicesResponse.getResult().getAssets();
Files.createDirectories(Paths.get("output/"));
int index = 0;
for (Asset resultAsset : resultAssets) {
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
OutputStream outputStream = Files.newOutputStream(new File("output/SplitPDFByNumberOfPagesOutput_" + index + ".pdf").toPath());
LOGGER.info("Saving asset at output/SplitPDFByNumberOfPagesOutput_" + index + ".pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
index++;
}
} catch (IOException| ServiceApiException | SDKException | ServiceUsageException e) {
LOGGER.error("Exception encountered while executing operation", e);
}
}
}

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
// 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.splitpdf.SplitPDFByPageRanges
public class SplitPDFByPageRanges {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFByPageRanges.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/splitPDFInput.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
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
// Specify page ranges to split PDF
PageRanges pageRanges = getPageRanges();
// Create parameters for the job
SplitPDFParams splitPDFParams = new SplitPDFParams();
// Set the page ranges where each page range corresponds to a single output file
splitPDFParams.setPageRanges(pageRanges);
// Creates a new job instance
SplitPDFJob splitPDFJob = new SplitPDFJob(asset, splitPDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(splitPDFJob);
PDFServicesResponse<SplitPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, SplitPDFResult.class);
// Get content from the resulting asset(s)
List<Asset> resultAssets = pdfServicesResponse.getResult().getAssets();
Files.createDirectories(Paths.get("output/"));
int index = 0;
for (Asset resultAsset : resultAssets) {
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
OutputStream outputStream = Files.newOutputStream(new File("output/SplitPDFByPageRangesOutput_" + index + ".pdf").toPath());
LOGGER.info("Saving asset at output/SplitPDFByPageRangesOutput_" + index + ".pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
index++;
}
} catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
LOGGER.error("Exception encountered while executing operation", e);
}
}
private static PageRanges getPageRanges() {
// Specify page ranges
PageRanges pageRanges = new PageRanges();
// Add page 1
pageRanges.addSinglePage(1);
// Add pages 3 to 4
pageRanges.addRange(3, 4);
return pageRanges;
}
}

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
// 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.splitpdf.SplitPDFIntoNumberOfFiles
public class SplitPDFIntoNumberOfFiles {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFIntoNumberOfFiles.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/splitPDFInput.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
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
// Create parameters for the job
SplitPDFParams splitPDFParams = new SplitPDFParams();
// Sets the number of documents to split the input PDF file into
splitPDFParams.setFileCount(2);
// Creates a new job instance
SplitPDFJob splitPDFJob = new SplitPDFJob(asset, splitPDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(splitPDFJob);
PDFServicesResponse<SplitPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, SplitPDFResult.class);
// Get content from the resulting asset(s)
List<Asset> resultAssets = pdfServicesResponse.getResult().getAssets();
Files.createDirectories(Paths.get("output/"));
int index = 0;
for (Asset resultAsset : resultAssets) {
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
OutputStream outputStream = Files.newOutputStream(new File("output/SplitPDFIntoNumberOfFilesOutput_" + index + ".pdf").toPath());
LOGGER.info("Saving asset at output/SplitPDFIntoNumberOfFilesOutput_" + index + ".pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
index++;
}
} catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
LOGGER.error("Exception encountered while executing operation", e);
}
}
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.