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/deletepages/delete-pdf-pages.js45const {6 ServicePrincipalCredentials,7 PDFServices,8 MimeType,9 DeletePagesParams,10 PageRanges,11 DeletePagesJob,12 DeletePagesResult,13 SDKError,14 ServiceUsageError,15 ServiceApiError16} = require("@adobe/pdfservices-node-sdk");17const fs = require("fs");1819(async () => {20 let readStream;21 try {22 // Initial setup, create credentials instance23 const credentials = new ServicePrincipalCredentials({24 clientId: process.env.PDF_SERVICES_CLIENT_ID,25 clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET26 });2728 // Creates a PDF Services instance29 const pdfServices = new PDFServices({credentials});3031 // Creates an asset(s) from source file(s) and upload32 readStream = fs.createReadStream("./deletePagesInput.pdf");33 const inputAsset = await pdfServices.upload({34 readStream,35 mimeType: MimeType.PDF36 });3738 // Delete pages of the document (as specified by PageRanges).39 const pageRangeForDeletion = getPageRangesForDeletion();4041 // Create parameters for the job42 const params = new DeletePagesParams({43 pageRanges: pageRangeForDeletion44 });4546 // Creates a new job instance47 const job = new DeletePagesJob({inputAsset, params});4849 // Submit the job and get the job result50 const pollingURL = await pdfServices.submit({job});51 const pdfServicesResponse = await pdfServices.getJobResult({52 pollingURL,53 resultType: DeletePagesResult54 });5556 // Get content from the resulting asset(s)57 const resultAsset = pdfServicesResponse.result.asset;58 const streamAsset = await pdfServices.getContent({asset: resultAsset});5960 // Creates a write stream and copy stream asset's content to it61 const outputFilePath = "./deletePagesOutput.pdf";62 console.log(`Saving asset at ${outputFilePath}`);6364 const writeStream = fs.createWriteStream(outputFilePath);65 streamAsset.readStream.pipe(writeStream);66 } catch (err) {67 if (err instanceof SDKError || err instanceof ServiceUsageError || err instanceof ServiceApiError) {68 console.log("Exception encountered while executing operation", err);69 } else {70 console.log("Exception encountered while executing operation", err);71 }72 } finally {73 readStream?.destroy();74 }75})();7677const getPageRangesForDeletion = () => {78 // Specify pages for deletion.79 const pageRangesForDeletion = new PageRanges();80 // Add page 1.81 pageRangesForDeletion.addSinglePage(1);82 // Add pages 3 to 4.83 pageRangesForDeletion.addRange(3, 4);84 return pageRangesForDeletion;85};
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}'