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.serviceAccountCredentialsBuilder()15 .fromFile("pdfservices-api-credentials.json")16 .build();1718 // Create an ExecutionContext using credentials and create a new operation instance.19 ExecutionContext executionContext = ExecutionContext.create(credentials);20 ReplacePagesOperation replacePagesOperation = ReplacePagesOperation.createNew();2122 // Set operation base input from a source file.23 FileRef baseSourceFile = FileRef.createFromLocalFile("src/main/resources/baseInput.pdf");24 replacePagesOperation.setBaseInput(baseSourceFile);2526 // Create a FileRef instance using a local file.27 FileRef firstInputFile = FileRef.createFromLocalFile("src/main/resources/replacePagesInput1.pdf");28 PageRanges pageRanges = getPageRangeForFirstFile();2930 // Adds the pages (specified by the page ranges) of the input PDF file for replacing the31 // page of the base PDF file.32 replacePagesOperation.addPagesForReplace(firstInputFile, pageRanges, 1);333435 // Create a FileRef instance using a local file.36 FileRef secondInputFile = FileRef.createFromLocalFile("src/main/resources/replacePagesInput2.pdf");3738 // Adds all the pages of the input PDF file for replacing the page of the base PDF file.39 replacePagesOperation.addPagesForReplace(secondInputFile, 3);4041 // Execute the operation42 FileRef result = replacePagesOperation.execute(executionContext);4344 // Save the result at the specified location45 result.saveAs("output/replacePagesOutput.pdf");46 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {47 LOGGER.error("Exception encountered while executing operation", e);48 }49 }5051 private static PageRanges getPageRangeForFirstFile() {52 // Specify pages of the first file for replacing the page of base PDF file.53 PageRanges pageRanges = new PageRanges();54 // Add pages 1 to 3.55 pageRanges.addRange(1, 3);5657 // Add page 4.58 pageRanges.addSinglePage(4);5960 return pageRanges;61 }62 }
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.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);2425 // Create a new operation instance26 ReplacePagesOperation replacePagesOperation = ReplacePagesOperation.CreateNew();2728 // Set operation base input from a source file.29 FileRef baseSourceFile = FileRef.CreateFromLocalFile(@"baseInput.pdf");30 replacePagesOperation.SetBaseInput(baseSourceFile);3132 // Create a FileRef instance using a local file.33 FileRef firstInputFile = FileRef.CreateFromLocalFile(@"replacePagesInput1.pdf");34 PageRanges pageRanges = GetPageRangeForFirstFile();3536 // Adds the pages (specified by the page ranges) of the input PDF file for replacing the37 // page of the base PDF file.38 replacePagesOperation.AddPagesForReplace(firstInputFile, pageRanges, 1);3940 // Create a FileRef instance using a local file.41 FileRef secondInputFile = FileRef.CreateFromLocalFile(@"replacePagesInput2.pdf");4243 // Adds all the pages of the input PDF file for replacing the page of the base PDF file.44 replacePagesOperation.AddPagesForReplace(secondInputFile, 3);4546 // Execute the operation.47 FileRef result = replacePagesOperation.Execute(executionContext);4849 // Save the result to the specified location.50 result.SaveAs(Directory.GetCurrentDirectory() + "/output/replacePagesOutput.pdf");51 }52 catch (ServiceUsageException ex)53 {54 log.Error("Exception encountered while executing operation", ex);55 // Catch more errors here . . .56 }5758 private static PageRanges GetPageRangeForFirstFile()59 {60 // Specify pages of the first file for replacing the page of base PDF file.61 PageRanges pageRanges = new PageRanges();62 // Add pages 1 to 3.63 pageRanges.AddRange(1, 3);6465 // Add page 4.66 pageRanges.AddSinglePage(4);6768 return pageRanges;69 }7071 static void ConfigureLogging()72 {73 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());74 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));75 }76 }77 }
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 .serviceAccountCredentialsBuilder()23 .fromFile("pdfservices-api-credentials.json")24 .build();2526 // Create an ExecutionContext using credentials and create a new operation instance.27 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),28 replacePagesOperation = PDFServicesSdk.ReplacePages.Operation.createNew();2930 // Set operation base input from a source file.31 const baseInputFile = PDFServicesSdk.FileRef.createFromLocalFile('resources/baseInput.pdf');32 replacePagesOperation.setBaseInput(baseInputFile);3334 // Create a FileRef instance using a local file.35 const firstInputFile = PDFServicesSdk.FileRef.createFromLocalFile('resources/replacePagesInput1.pdf'),36 pageRanges = getPageRangesForFirstFile();3738 // Adds the pages (specified by the page ranges) of the input PDF file for replacing the39 // page of the base PDF file.40 replacePagesOperation.addPagesForReplace(1, firstInputFile, pageRanges);4142 // Create a FileRef instance using a local file.43 const secondInputFile = PDFServicesSdk.FileRef.createFromLocalFile('resources/replacePagesInput2.pdf');4445 // Adds all the pages of the input PDF file for replacing the page of the base PDF file.46 replacePagesOperation.addPagesForReplace(3, secondInputFile);4748 // Execute the operation and Save the result to the specified location.49 replacePagesOperation.execute(executionContext)50 .then(result => result.saveAsFile('output/replacePagesOutput.pdf'))51 .catch(err => {52 if (err instanceof PDFServicesSdk.Error.ServiceApiError53 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {54 console.log('Exception encountered while executing operation', err);55 } else {56 console.log('Exception encountered while executing operation', err);57 }58 });59 } catch (err) {60 console.log('Exception encountered while executing operation', err);61 }
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