Replace Pages
Replace one or more pages with another page in an existing document
Rest API
See our public API Reference for Replace Pages
Replace Pages in PDF
The replace pages operation replaces pages in a PDF with pages from other PDF files.
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.replacepages.ReplacePDFPages45 public class ReplacePDFPages {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(ReplacePDFPages.class);910 public static void main(String[] args) {1112 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();1819 // Create an ExecutionContext using credentials and create a new operation instance.20 ExecutionContext executionContext = ExecutionContext.create(credentials);21 ReplacePagesOperation replacePagesOperation = ReplacePagesOperation.createNew();2223 // Set operation base input from a source file.24 FileRef baseSourceFile = FileRef.createFromLocalFile("src/main/resources/baseInput.pdf");25 replacePagesOperation.setBaseInput(baseSourceFile);2627 // Create a FileRef instance using a local file.28 FileRef firstInputFile = FileRef.createFromLocalFile("src/main/resources/replacePagesInput1.pdf");29 PageRanges pageRanges = getPageRangeForFirstFile();3031 // Adds the pages (specified by the page ranges) of the input PDF file for replacing the32 // page of the base PDF file.33 replacePagesOperation.addPagesForReplace(firstInputFile, pageRanges, 1);343536 // Create a FileRef instance using a local file.37 FileRef secondInputFile = FileRef.createFromLocalFile("src/main/resources/replacePagesInput2.pdf");3839 // Adds all the pages of the input PDF file for replacing the page of the base PDF file.40 replacePagesOperation.addPagesForReplace(secondInputFile, 3);4142 // Execute the operation43 FileRef result = replacePagesOperation.execute(executionContext);4445 // Save the result at the specified location46 result.saveAs("output/replacePagesOutput.pdf");47 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {48 LOGGER.error("Exception encountered while executing operation", e);49 }50 }5152 private static PageRanges getPageRangeForFirstFile() {53 // Specify pages of the first file for replacing the page of base PDF file.54 PageRanges pageRanges = new PageRanges();55 // Add pages 1 to 3.56 pageRanges.addRange(1, 3);5758 // Add page 4.59 pageRanges.addSinglePage(4);6061 return pageRanges;62 }63 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd ReplacePDFPages/4// dotnet run ReplacePDFPages.csproj56 namespace ReplacePDFPages7 {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 ReplacePagesOperation replacePagesOperation = ReplacePagesOperation.CreateNew();2829 // Set operation base input from a source file.30 FileRef baseSourceFile = FileRef.CreateFromLocalFile(@"baseInput.pdf");31 replacePagesOperation.SetBaseInput(baseSourceFile);3233 // Create a FileRef instance using a local file.34 FileRef firstInputFile = FileRef.CreateFromLocalFile(@"replacePagesInput1.pdf");35 PageRanges pageRanges = GetPageRangeForFirstFile();3637 // Adds the pages (specified by the page ranges) of the input PDF file for replacing the38 // page of the base PDF file.39 replacePagesOperation.AddPagesForReplace(firstInputFile, pageRanges, 1);4041 // Create a FileRef instance using a local file.42 FileRef secondInputFile = FileRef.CreateFromLocalFile(@"replacePagesInput2.pdf");4344 // Adds all the pages of the input PDF file for replacing the page of the base PDF file.45 replacePagesOperation.AddPagesForReplace(secondInputFile, 3);4647 // Execute the operation.48 FileRef result = replacePagesOperation.Execute(executionContext);4950 // Save the result to the specified location.51 result.SaveAs(Directory.GetCurrentDirectory() + "/output/replacePagesOutput.pdf");52 }53 catch (ServiceUsageException ex)54 {55 log.Error("Exception encountered while executing operation", ex);56 // Catch more errors here . . .57 }5859 private static PageRanges GetPageRangeForFirstFile()60 {61 // Specify pages of the first file for replacing the page of base PDF file.62 PageRanges pageRanges = new PageRanges();63 // Add pages 1 to 3.64 pageRanges.AddRange(1, 3);6566 // Add page 4.67 pageRanges.AddSinglePage(4);6869 return pageRanges;70 }7172 static void ConfigureLogging()73 {74 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());75 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));76 }77 }78 }
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 getPageRangesForFirstFile = () => {8 // Specify pages of the first file for replacing the page of base PDF file.9 const pageRangesForFirstFile = new PDFServicesSdk.PageRanges();10 // Add pages 1 to 3.11 pageRangesForFirstFile.addPageRange(1, 3);1213 // Add page 4.14 pageRangesForFirstFile.addSinglePage(4);1516 return pageRangesForFirstFile;17 };1819 try {20 // Initial setup, create credentials instance.21 const credentials = PDFServicesSdk.Credentials22 .servicePrincipalCredentialsBuilder()23 .withClientId("PDF_SERVICES_CLIENT_ID")24 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")25 .build();2627 // Create an ExecutionContext using credentials and create a new operation instance.28 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),29 replacePagesOperation = PDFServicesSdk.ReplacePages.Operation.createNew();3031 // Set operation base input from a source file.32 const baseInputFile = PDFServicesSdk.FileRef.createFromLocalFile('resources/baseInput.pdf');33 replacePagesOperation.setBaseInput(baseInputFile);3435 // Create a FileRef instance using a local file.36 const firstInputFile = PDFServicesSdk.FileRef.createFromLocalFile('resources/replacePagesInput1.pdf'),37 pageRanges = getPageRangesForFirstFile();3839 // Adds the pages (specified by the page ranges) of the input PDF file for replacing the40 // page of the base PDF file.41 replacePagesOperation.addPagesForReplace(1, firstInputFile, pageRanges);4243 // Create a FileRef instance using a local file.44 const secondInputFile = PDFServicesSdk.FileRef.createFromLocalFile('resources/replacePagesInput2.pdf');4546 // Adds all the pages of the input PDF file for replacing the page of the base PDF file.47 replacePagesOperation.addPagesForReplace(3, secondInputFile);4849 // Execute the operation and Save the result to the specified location.50 replacePagesOperation.execute(executionContext)51 .then(result => result.saveAsFile('output/replacePagesOutput.pdf'))52 .catch(err => {53 if (err instanceof PDFServicesSdk.Error.ServiceApiError54 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {55 console.log('Exception encountered while executing operation', err);56 } else {57 console.log('Exception encountered while executing operation', err);58 }59 });60 } catch (err) {61 console.log('Exception encountered while executing operation', err);62 }
Copied to your clipboard1// Please refer our Rest API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Combine-PDF34curl --location --request POST 'https://pdf-services.adobe.io/operation/combinepdf' \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 "assets": [10 {11 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718",12 "pageRanges": [13 {14 "start": 1,15 "end": 116 }17 ]18 },19 {20 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718",21 "pageRanges": [22 {23 "start": 224 }25 ]26 },27 {28 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718",29 "pageRanges": [30 {31 "start": 332 }33 ]34 }35 ]36}'37// Legacy API can be found here38// https://documentcloud.adobe.com/document-services/index.html#post-combinePDF