Edit in GitHubLog an issue

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.

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.deletepages.DeletePDFPages
4
5
6 public class DeletePDFPages {
7
8 // Initialize the logger.
9 private static final Logger LOGGER = LoggerFactory.getLogger(DeletePDFPages.class);
10
11 public static void main(String[] args) {
12 try {
13 // Initial setup, create credentials instance.
14 Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()
15 .withClientId("PDF_SERVICES_CLIENT_ID")
16 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")
17 .build();
18
19 // Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 DeletePagesOperation deletePagesOperation = DeletePagesOperation.createNew();
22
23 // Set operation input from a source file.
24 FileRef source = FileRef.createFromLocalFile("src/main/resources/deletePagesInput.pdf");
25 deletePagesOperation.setInput(source);
26
27 // Delete pages of the document (as specified by PageRanges).
28 PageRanges pageRangeForDeletion = getPageRangeForDeletion();
29 deletePagesOperation.setPageRanges(pageRangeForDeletion);
30
31 // Execute the operation.
32 FileRef result = deletePagesOperation.execute(executionContext);
33
34 // Save the result to the specified location.
35 result.saveAs("output/deletePagesOutput.pdf");
36
37 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
38 LOGGER.error("Exception encountered while executing operation", e);
39 }
40 }
41
42 private static PageRanges getPageRangeForDeletion() {
43 // Specify pages for deletion.
44 PageRanges pageRangeForDeletion = new PageRanges();
45 // Add page 1.
46 pageRangeForDeletion.addSinglePage(1);
47
48 // Add pages 3 to 4.
49 pageRangeForDeletion.addRange(3, 4);
50 return pageRangeForDeletion;
51 }
52 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2023 Adobe. All rights reserved.