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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/deletePagesInput.pdf").toPath())) {
13 // Initial setup, create credentials instance
14 Credentials credentials = new ServicePrincipalCredentials(
15 System.getenv("PDF_SERVICES_CLIENT_ID"),
16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
17
18 // Creates a PDF Services instance
19 PDFServices pdfServices = new PDFServices(credentials);
20
21 // Creates an asset(s) from source file(s) and upload
22 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
23
24 // Delete pages of the document (as specified by PageRanges).
25 PageRanges pageRangeForDeletion = getPageRangeForDeletion();
26
27 // Create parameters for the job
28 DeletePagesParams deletePagesParams = new DeletePagesParams(pageRangeForDeletion);
29
30 // Creates a new job instance
31 DeletePagesJob deletePagesJob = new DeletePagesJob(asset, deletePagesParams);
32
33 // Submit the job and gets the job result
34 String location = pdfServices.submit(deletePagesJob);
35 PDFServicesResponse<DeletePagesResult> pdfServicesResponse = pdfServices.getJobResult(location, DeletePagesResult.class);
36
37 // Get content from the resulting asset(s)
38 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
39 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
40
41 // Creates an output stream and copy stream asset's content to it
42 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 }
51
52 private static PageRanges getPageRangeForDeletion() {
53 // Specify pages for deletion
54 PageRanges pageRangeForDeletion = new PageRanges();
55 // Add page 1
56 pageRangeForDeletion.addSinglePage(1);
57
58 // Add pages 3 to 4
59 pageRangeForDeletion.addRange(3, 4);
60 return pageRangeForDeletion;
61 }
62 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.