Delete Pages
Delete Pages in a PDF
The delete pages operation selectively removes pages from a PDF 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.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 {13 // Initial setup, create credentials instance.14 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()15 .fromFile("pdfservices-api-credentials.json")16 .build();1718 // Create an ExecutionContext using credentials and create a new operation instance.19 ExecutionContext executionContext = ExecutionContext.create(credentials);20 DeletePagesOperation deletePagesOperation = DeletePagesOperation.createNew();2122 // Set operation input from a source file.23 FileRef source = FileRef.createFromLocalFile("src/main/resources/deletePagesInput.pdf");24 deletePagesOperation.setInput(source);2526 // Delete pages of the document (as specified by PageRanges).27 PageRanges pageRangeForDeletion = getPageRangeForDeletion();28 deletePagesOperation.setPageRanges(pageRangeForDeletion);2930 // Execute the operation.31 FileRef result = deletePagesOperation.execute(executionContext);3233 // Save the result to the specified location.34 result.saveAs("output/deletePagesOutput.pdf");3536 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {37 LOGGER.error("Exception encountered while executing operation", e);38 }39 }4041 private static PageRanges getPageRangeForDeletion() {42 // Specify pages for deletion.43 PageRanges pageRangeForDeletion = new PageRanges();44 // Add page 1.45 pageRangeForDeletion.addSinglePage(1);4647 // Add pages 3 to 4.48 pageRangeForDeletion.addRange(3, 4);49 return pageRangeForDeletion;50 }51 }
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.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 DeletePagesOperation deletePagesOperation = DeletePagesOperation.CreateNew();2728 // Set operation input from a source file.29 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"deletePagesInput.pdf");30 deletePagesOperation.SetInput(sourceFileRef);3132 // Delete pages of the document (as specified by PageRanges).33 PageRanges pageRangeForDeletion = GetPageRangeForDeletion();34 deletePagesOperation.SetPageRanges(pageRangeForDeletion);3536 // Execute the operation.37 FileRef result = deletePagesOperation.Execute(executionContext);3839 // Save the result to the specified location.40 result.SaveAs(Directory.GetCurrentDirectory() + "/output/deletePagesOutput.pdf");41 }42 catch (ServiceUsageException ex)43 {44 log.Error("Exception encountered while executing operation", ex);45 }46 // Catch more errors here . . .47 }4849 private static PageRanges GetPageRangeForDeletion()50 {51 // Specify pages for deletion.52 PageRanges pageRangeForDeletion = new PageRanges();53 // Add page 1.54 pageRangeForDeletion.AddSinglePage(1);5556 // Add pages 3 to 4.57 pageRangeForDeletion.AddRange(3, 4);58 return pageRangeForDeletion;59 }6061 static void ConfigureLogging()62 {63 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());64 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));65 }66 }67 }
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 .serviceAccountCredentialsBuilder()22 .fromFile("pdfservices-api-credentials.json")23 .build();2425 // Create an ExecutionContext using credentials and create a new operation instance.26 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),27 deletePagesOperation = PDFServicesSdk.DeletePages.Operation.createNew();2829 // Set operation input from a source file.30 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/deletePagesInput.pdf');31 deletePagesOperation.setInput(input);3233 // Delete pages of the document (as specified by PageRanges).34 const pageRangesForDeletion = getPageRangesForDeletion();35 deletePagesOperation.setPageRanges(pageRangesForDeletion);3637 // Execute the operation and Save the result to the specified location.38 deletePagesOperation.execute(executionContext)39 .then(result => result.saveAsFile('output/deletePagesOutput.pdf'))40 .catch(err => {41 if (err instanceof PDFServicesSdk.Error.ServiceApiError42 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {43 console.log('Exception encountered while executing operation', err);44 } else {45 console.log('Exception encountered while executing operation', err);46 }47 });48 } catch (err) {49 console.log('Exception encountered while executing operation', err);50 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-pageManipulation34curl --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 \"pageActions\": [{14 \"pageAction\": {15 \"delete\": {16 \"pageRanges\": [{17 \"start\": 1,18 \"end\": 119 }]20 }21 }22 }]23 }24 },25 \"documentIn\": {26 \"cpf:location\": \"InputFile0\",27 \"dc:format\": \"application/pdf\"28 }29 },30 \"cpf:engine\": {31 \"repo:assetId\": \"urn:aaid:cpf:Service-4735fcf3cf924b25879e6fcf7aa84ad4\"32 },33 \"cpf:outputs\": {34 \"documentOut\": {35 \"cpf:location\": \"cid:multipartLabelOut\",36 \"dc:format\": \"application/pdf\"37 }38 }39}"' \40--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'