Delete Pages
Delete one or more pages from a document
Rest API
See our public API Reference for Delete Pages
Delete Pages in a PDF
The delete pages operation selectively removes pages from a PDF file.
Please refer the API usage guide to understand how to use our APIs.
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.deletepages.DeletePDFPages456 public class DeletePDFPages {78 // Initialize the logger.9 private static final Logger LOGGER = LoggerFactory.getLogger(DeletePDFPages.class);1011 public static void main(String[] args) {12 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/deletePagesInput.pdf").toPath())) {13 // Initial setup, create credentials instance14 Credentials credentials = new ServicePrincipalCredentials(15 System.getenv("PDF_SERVICES_CLIENT_ID"),16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));1718 // Creates a PDF Services instance19 PDFServices pdfServices = new PDFServices(credentials);2021 // Creates an asset(s) from source file(s) and upload22 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());2324 // Delete pages of the document (as specified by PageRanges).25 PageRanges pageRangeForDeletion = getPageRangeForDeletion();2627 // Create parameters for the job28 DeletePagesParams deletePagesParams = new DeletePagesParams(pageRangeForDeletion);2930 // Creates a new job instance31 DeletePagesJob deletePagesJob = new DeletePagesJob(asset, deletePagesParams);3233 // Submit the job and gets the job result34 String location = pdfServices.submit(deletePagesJob);35 PDFServicesResponse<DeletePagesResult> pdfServicesResponse = pdfServices.getJobResult(location, DeletePagesResult.class);3637 // Get content from the resulting asset(s)38 Asset resultAsset = pdfServicesResponse.getResult().getAsset();39 StreamAsset streamAsset = pdfServices.getContent(resultAsset);4041 // Creates an output stream and copy stream asset's content to it42 Files.createDirectories(Paths.get("output/"));43 OutputStream outputStream = Files.newOutputStream(new File("output/deletePagesOutput.pdf").toPath());44 LOGGER.info("Saving asset at output/deletePagesOutput.pdf");45 IOUtils.copy(streamAsset.getInputStream(), outputStream);46 outputStream.close();47 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {48 LOGGER.error("Exception encountered while executing operation", e);49 }50 }5152 private static PageRanges getPageRangeForDeletion() {53 // Specify pages for deletion54 PageRanges pageRangeForDeletion = new PageRanges();55 // Add page 156 pageRangeForDeletion.addSinglePage(1);5758 // Add pages 3 to 459 pageRangeForDeletion.addRange(3, 4);60 return pageRangeForDeletion;61 }62 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd DeletePDFPages/4// dotnet run DeletePDFPages.csproj56 namespace DeletePDFPages7 {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.ServicePrincipalCredentialsBuilder()19 .WithClientId("PDF_SERVICES_CLIENT_ID")20 .WithClientSecret("PDF_SERVICES_CLIENT_SECRET")21 .Build();2223 // Create an ExecutionContext using credentials.24 ExecutionContext executionContext = ExecutionContext.Create(credentials);2526 // Create a new operation instance27 DeletePagesOperation deletePagesOperation = DeletePagesOperation.CreateNew();2829 // Set operation input from a source file.30 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"deletePagesInput.pdf");31 deletePagesOperation.SetInput(sourceFileRef);3233 // Delete pages of the document (as specified by PageRanges).34 PageRanges pageRangeForDeletion = GetPageRangeForDeletion();35 deletePagesOperation.SetPageRanges(pageRangeForDeletion);3637 // Execute the operation.38 FileRef result = deletePagesOperation.Execute(executionContext);3940 // Save the result to the specified location.41 result.SaveAs(Directory.GetCurrentDirectory() + "/output/deletePagesOutput.pdf");42 }43 catch (ServiceUsageException ex)44 {45 log.Error("Exception encountered while executing operation", ex);46 }47 // Catch more errors here . . .48 }4950 private static PageRanges GetPageRangeForDeletion()51 {52 // Specify pages for deletion.53 PageRanges pageRangeForDeletion = new PageRanges();54 // Add page 1.55 pageRangeForDeletion.AddSinglePage(1);5657 // Add pages 3 to 4.58 pageRangeForDeletion.AddRange(3, 4);59 return pageRangeForDeletion;60 }6162 static void ConfigureLogging()63 {64 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());65 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));66 }67 }68 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/replacepages/replace-pdf-pages.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 const getPageRangesForDeletion = () => {8 // Specify pages for deletion.9 const pageRangesForDeletion = new PDFServicesSdk.PageRanges();10 // Add page 1.11 pageRangesForDeletion.addSinglePage(1);1213 // Add pages 3 to 4.14 pageRangesForDeletion.addPageRange(3, 4);15 return pageRangesForDeletion;16 };1718 try {19 // Initial setup, create credentials instance.20 const credentials = PDFServicesSdk.Credentials21 .servicePrincipalCredentialsBuilder()22 .withClientId("PDF_SERVICES_CLIENT_ID")23 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")24 .build();2526 // Create an ExecutionContext using credentials and create a new operation instance.27 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),28 deletePagesOperation = PDFServicesSdk.DeletePages.Operation.createNew();2930 // Set operation input from a source file.31 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/deletePagesInput.pdf');32 deletePagesOperation.setInput(input);3334 // Delete pages of the document (as specified by PageRanges).35 const pageRangesForDeletion = getPageRangesForDeletion();36 deletePagesOperation.setPageRanges(pageRangesForDeletion);3738 // Execute the operation and Save the result to the specified location.39 deletePagesOperation.execute(executionContext)40 .then(result => result.saveAsFile('output/deletePagesOutput.pdf'))41 .catch(err => {42 if (err instanceof PDFServicesSdk.Error.ServiceApiError43 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {44 console.log('Exception encountered while executing operation', err);45 } else {46 console.log('Exception encountered while executing operation', err);47 }48 });49 } catch (err) {50 console.log('Exception encountered while executing operation', err);51 }
Copied to your clipboard1// Please refer our Rest API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Page-Manipulation34curl --location --request POST 'https://pdf-services.adobe.io/operation/pagemanipulation' \5--header 'x-api-key: {{Placeholder for client_id}}' \6--header 'Content-Type: application/json' \7--header 'Authorization: Bearer {{Placeholder for token}}' \8--data-raw '{9 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718",10 "pageActions": [11 {12 "delete": {13 "pageRanges": [14 {15 "start": 1,16 "end": 217 }18 ]19 }20 }21 ]22}'2324// Legacy API can be found here25// https://documentcloud.adobe.com/document-services/index.html#post-pageManipulation