Edit in GitHubLog an issue

Delete Pages

Delete Pages in a PDF#

The delete pages operation selectively removes pages from a PDF file.

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.serviceAccountCredentialsBuilder()
15 .fromFile("pdfservices-api-credentials.json")
16 .build();
17
18 // Create an ExecutionContext using credentials and create a new operation instance.
19 ExecutionContext executionContext = ExecutionContext.create(credentials);
20 DeletePagesOperation deletePagesOperation = DeletePagesOperation.createNew();
21
22 // Set operation input from a source file.
23 FileRef source = FileRef.createFromLocalFile("src/main/resources/deletePagesInput.pdf");
24 deletePagesOperation.setInput(source);
25
26 // Delete pages of the document (as specified by PageRanges).
27 PageRanges pageRangeForDeletion = getPageRangeForDeletion();
28 deletePagesOperation.setPageRanges(pageRangeForDeletion);
29
30 // Execute the operation.
31 FileRef result = deletePagesOperation.execute(executionContext);
32
33 // Save the result to the specified location.
34 result.saveAs("output/deletePagesOutput.pdf");
35
36 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
37 LOGGER.error("Exception encountered while executing operation", e);
38 }
39 }
40
41 private static PageRanges getPageRangeForDeletion() {
42 // Specify pages for deletion.
43 PageRanges pageRangeForDeletion = new PageRanges();
44 // Add page 1.
45 pageRangeForDeletion.addSinglePage(1);
46
47 // Add pages 3 to 4.
48 pageRangeForDeletion.addRange(3, 4);
49 return pageRangeForDeletion;
50 }
51 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.