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.
Java
.NET
Node JS
REST API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.splitpdf.SplitPDFByNumberOfPages45 public class SplitPDFByNumberOfPages {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFByNumberOfPages.class);910 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();1617 // Create an ExecutionContext using credentials and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 SplitPDFOperation splitPDFOperation = SplitPDFOperation.createNew();2021 // Set operation input from a source file.22 FileRef source = FileRef.createFromLocalFile("src/main/resources/splitPDFInput.pdf");23 splitPDFOperation.setInput(source);2425 // Set the maximum number of pages each of the output files can have.26 splitPDFOperation.setPageCount(2);2728 // Execute the operation.29 List result = splitPDFOperation.execute(executionContext);3031 // 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 }3738 } catch (IOException| ServiceApiException | SdkException | ServiceUsageException e) {39 LOGGER.error("Exception encountered while executing operation", e);40 }41 }4243 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd SplitPDFByNumberOfPages/4// dotnet run SplitPDFByNumberOfPages.csproj56 namespace SplitPDFByNumberOfPages7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);2425 // Create a new operation instance26 SplitPDFOperation splitPDFOperation = SplitPDFOperation.CreateNew();2728 // Set operation input from a source file.29 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"splitPDFInput.pdf");30 splitPDFOperation.SetInput(sourceFileRef);3132 // Set the maximum number of pages each of the output files can have.33 splitPDFOperation.SetPageCount(2);3435 // Execute the operation.36 List result = splitPDFOperation.Execute(executionContext);3738 // Save the result to the specified location.39 int index = 0;40 foreach (FileRef fileRef in result)41 {42 fileRef.SaveAs(Directory.GetCurrentDirectory() + "/output/SplitPDFByNumberOfPagesOutput_" + index + ".pdf");43 index++;44 }4546 }47 catch (ServiceUsageException ex)48 {49 log.Error("Exception encountered while executing operation", ex);50 }51 // Catch more errors here . . .52 }5354 static void ConfigureLogging()55 {56 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());57 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));58 }59 }60 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/splitpdf/split-pdf-by-number-of-pages.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .serviceAccountCredentialsBuilder()11 .fromFile("pdfservices-api-credentials.json")12 .build();1314 // Create an ExecutionContext using credentials15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials);1617 // Create a new operation instance.18 const splitPDFOperation = PDFServicesSdk.SplitPDF.Operation.createNew(),19 input = PDFServicesSdk.FileRef.createFromLocalFile(20 'resources/splitPDFInput.pdf',21 PDFServicesSdk.SplitPDF.SupportedSourceFormat.pdf22 );23 // Set operation input from a source file.24 splitPDFOperation.setInput(input);2526 // Set the maximum number of pages each of the output files can have.27 splitPDFOperation.setPageCount(2);2829 // Execute the operation and Save the result to the specified location.30 splitPDFOperation.execute(executionContext)31 .then(result => {32 let saveFilesPromises = [];33 for(let i = 0; i < result.length; i++){34 saveFilesPromises.push(result[i].saveAsFile(`output/SplitPDFByNumberOfPagesOutput_${i}.pdf`));35 }36 return Promise.all(saveFilesPromises);37 })38 .catch(err => {39 if(err instanceof PDFServicesSdk.Error.ServiceApiError40 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {41 console.log('Exception encountered while executing operation', err);42 } else {43 console.log('Exception encountered while executing operation', err);44 }45 });46 } catch (err) {47 console.log('Exception encountered while executing operation', err);48 }49
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-splitPDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"params\": {12 \"cpf:inline\": {13 \"pageCount\": 414 }15 },16 \"documentIn\": {17 \"cpf:location\": \"InputFile0\",18 \"dc:format\": \"application/pdf\"19 }20 },21 \"cpf:engine\": {22 \"repo:assetId\": \"urn:aaid:cpf:Service-d99c7660cba14e5c98f9023221dab40f\"23 },24 \"cpf:outputs\": {25 \"documentOutList\": {26 \"cpf:location\": \"multipartLabelOut\",27 \"dc:format\": \"text/directory\"28 }29 }30}"' \31--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'
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.
Java
.NET
Node JS
REST API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.splitpdf.SplitPDFByPageRanges45 public class SplitPDFByPageRanges {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFByPageRanges.class);910 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();1617 // Create an ExecutionContext using credentials and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 SplitPDFOperation splitPDFOperation = SplitPDFOperation.createNew();2021 // Set operation input from a source file.22 FileRef source = FileRef.createFromLocalFile("src/main/resources/splitPDFInput.pdf");23 splitPDFOperation.setInput(source);2425 // Set the page ranges where each page range corresponds to a single output file.26 PageRanges pageRanges = getPageRanges();27 splitPDFOperation.setPageRanges(pageRanges);2829 // Execute the operation.30 List result = splitPDFOperation.execute(executionContext);3132 // 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 }3839 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {40 LOGGER.error("Exception encountered while executing operation", e);41 }42 }4344 private static PageRanges getPageRanges() {45 // Specify page ranges.46 PageRanges pageRanges = new PageRanges();47 // Add page 1.48 pageRanges.addSinglePage(1);4950 // Add pages 3 to 4.51 pageRanges.addRange(3, 4);52 return pageRanges;53 }5455 }56
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd SplitPDFByPageRanges/4// dotnet run SplitPDFByPageRanges.csproj56 namespace SplitPDFByPageRanges7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);2425 // Create a new operation instance26 SplitPDFOperation splitPDFOperation = SplitPDFOperation.CreateNew();2728 // Set operation input from a source file.29 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"splitPDFInput.pdf");30 splitPDFOperation.SetInput(sourceFileRef);3132 // Set the page ranges where each page range corresponds to a single output file.33 PageRanges pageRanges = GetPageRanges();34 splitPDFOperation.SetPageRanges(pageRanges);3536 // Execute the operation.37 List result = splitPDFOperation.Execute(executionContext);3839 // Save the result to the specified location.40 int index = 0;41 foreach (FileRef fileRef in result)42 {43 fileRef.SaveAs(Directory.GetCurrentDirectory() + "/output/SplitPDFByPageRangesOutput_" + index + ".pdf");44 index++;45 }4647 }48 catch (ServiceUsageException ex)49 {50 log.Error("Exception encountered while executing operation", ex);51 }52 // Catch more errors here . . .53 }5455 static void ConfigureLogging()56 {57 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());58 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));59 }6061 private static PageRanges GetPageRanges()62 {63 // Specify page ranges.64 PageRanges pageRanges = new PageRanges();65 // Add page 1.66 pageRanges.AddSinglePage(1);6768 // Add pages 3 to 4.69 pageRanges.AddRange(3, 4);70 return pageRanges;71 }72 }73 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/splitpdf/split-pdf-by-page-ranges.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 const getPageRanges = () => {8 // Specify pages ranges.9 const pageRanges = new PDFServicesSdk.PageRanges();10 // Add page 1.11 pageRanges.addSinglePage(1);1213 // Add pages 3 to 4.14 pageRanges.addPageRange(3, 4);15 return pageRanges;16 };17 try {18 // Initial setup, create credentials instance.19 const credentials = PDFServicesSdk.Credentials20 .serviceAccountCredentialsBuilder()21 .fromFile("pdfservices-api-credentials.json")22 .build();2324 // Create an ExecutionContext using credentials25 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials);2627 // Create a new operation instance.28 const splitPDFOperation = PDFServicesSdk.SplitPDF.Operation.createNew(),29 input = PDFServicesSdk.FileRef.createFromLocalFile(30 'resources/splitPDFInput.pdf',31 PDFServicesSdk.SplitPDF.SupportedSourceFormat.pdf32 );33 // Set operation input from a source file.34 splitPDFOperation.setInput(input);3536 // Set the page ranges where each page range corresponds to a single output file.37 const pageRanges = getPageRanges();38 splitPDFOperation.setPageRanges(pageRanges);3940 // Execute the operation and Save the result to the specified location.41 splitPDFOperation.execute(executionContext)42 .then(result => {43 let saveFilesPromises = [];44 for(let i = 0; i < result.length; i++){45 saveFilesPromises.push(result[i].saveAsFile(`output/SplitPDFByPageRangesOutput_${i}.pdf`));46 }47 return Promise.all(saveFilesPromises);48 })49 .catch(err => {50 if(err instanceof PDFServicesSdk.Error.ServiceApiError51 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {52 console.log('Exception encountered while executing operation', err);53 } else {54 console.log('Exception encountered while executing operation', err);55 }56 });57 } catch (err) {58 console.log('Exception encountered while executing operation', err);59 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-splitPDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"params\": {12 \"cpf:inline\": {13 \"pageRanges\": [{14 \"start\": 2,15 \"end\": 416 }, {17 \"start\": 7,18 \"end\": 919 }]20 }21 },22 \"documentIn\": {23 \"cpf:location\": \"InputFile0\",24 \"dc:format\": \"application/pdf\"25 }26 },27 \"cpf:engine\": {28 \"repo:assetId\": \"urn:aaid:cpf:Service-d99c7660cba14e5c98f9023221dab40f\"29 },30 \"cpf:outputs\": {31 \"documentOutList\": {32 \"cpf:location\": \"multipartLabelOut\",33 \"dc:format\": \"text/directory\"34 }35 }36}"' \37--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'
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).
Java
.NET
Node JS
REST API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.splitpdf.SplitPDFIntoNumberOfFiles45 public class SplitPDFIntoNumberOfFiles {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(SplitPDFIntoNumberOfFiles.class);910 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();1617 // Create an ExecutionContext using credentials and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 SplitPDFOperation splitPDFOperation = SplitPDFOperation.createNew();2021 // Set operation input from a source file.22 FileRef source = FileRef.createFromLocalFile("src/main/resources/splitPDFInput.pdf");23 splitPDFOperation.setInput(source);2425 // Set the number of documents to split the input PDF file into.26 splitPDFOperation.setFileCount(2);2728 // Execute the operation.29 List result = splitPDFOperation.execute(executionContext);3031 // 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 }3738 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {39 LOGGER.error("Exception encountered while executing operation", e);40 }41 }4243 }44
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd SplitPDFIntoNumberOfFiles/4// dotnet run SplitPDFIntoNumberOfFiles.csproj56 namespace SplitPDFIntoNumberOfFiles7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);2425 // Create a new operation instance26 SplitPDFOperation splitPDFOperation = SplitPDFOperation.CreateNew();2728 // Set operation input from a source file.29 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"splitPDFInput.pdf");30 splitPDFOperation.SetInput(sourceFileRef);3132 // Set the number of documents to split the input PDF file into.33 splitPDFOperation.SetFileCount(2);3435 // Execute the operation.36 List result = splitPDFOperation.Execute(executionContext);3738 // Save the result to the specified location.39 int index = 0;40 foreach (FileRef fileRef in result)41 {42 fileRef.SaveAs(Directory.GetCurrentDirectory() + "/output/SplitPDFIntoNumberOfFilesOutput_" + index + ".pdf");43 index++;44 }4546 }47 catch (ServiceUsageException ex)48 {49 log.Error("Exception encountered while executing operation", ex);50 }51 // Catch more errors here . . .52 }5354 static void ConfigureLogging()55 {56 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());57 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));58 }59 }60 }61
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/splitpdf/split-pdf-into-number-of-files.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .serviceAccountCredentialsBuilder()11 .fromFile("pdfservices-api-credentials.json")12 .build();1314 // Create an ExecutionContext using credentials15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials);1617 // Create a new operation instance.18 const splitPDFOperation = PDFServicesSdk.SplitPDF.Operation.createNew(),19 input = PDFServicesSdk.FileRef.createFromLocalFile(20 'resources/splitPDFInput.pdf',21 PDFServicesSdk.SplitPDF.SupportedSourceFormat.pdf22 );23 // Set operation input from a source file.24 splitPDFOperation.setInput(input);2526 // Set the number of documents to split the input PDF file into.27 splitPDFOperation.setFileCount(2);2829 // Execute the operation and Save the result to the specified location.30 splitPDFOperation.execute(executionContext)31 .then(result => {32 let saveFilesPromises = [];33 for(let i = 0; i < result.length; i++){34 saveFilesPromises.push(result[i].saveAsFile(`output/SplitPDFIntoNumberOfFilesOutput_${i}.pdf`));35 }36 return Promise.all(saveFilesPromises);37 })38 .catch(err => {39 if(err instanceof PDFServicesSdk.Error.ServiceApiError40 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {41 console.log('Exception encountered while executing operation', err);42 } else {43 console.log('Exception encountered while executing operation', err);44 }45 });46 } catch (err) {47 console.log('Exception encountered while executing operation', err);48 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-splitPDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"params\": {12 \"cpf:inline\": {13 \"fileCount\": 214 }15 },16 \"documentIn\": {17 \"cpf:location\": \"InputFile0\",18 \"dc:format\": \"application/pdf\"19 }20 },21 \"cpf:engine\": {22 \"repo:assetId\": \"urn:aaid:cpf:Service-d99c7660cba14e5c98f9023221dab40f\"23 },24 \"cpf:outputs\": {25 \"documentOutList\": {26 \"cpf:location\": \"multipartLabelOut\",27 \"dc:format\": \"text/directory\"28 }29 }30}"' \31--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'