Replace Pages
Replace Pages in PDF
The replace pages operation replaces pages in a PDF with pages from other PDF files.
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://documentcloud.adobe.com/document-services/index.html#post-combinePDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"documentsIn\": [{12 \"pageRanges\": {13 \"cpf:inline\": [{14 \"start\": 1,15 \"end\": 416 }]17 },18 \"documentIn\": {19 \"cpf:location\": \"InputFile0\",20 \"dc:format\": \"application/pdf\"21 }22 }, {23 \"pageRanges\": {24 \"cpf:inline\": [{25 \"start\": 1,26 \"end\": 2527 }]28 },29 \"documentIn\": {30 \"cpf:location\": \"InputFile1\",31 \"dc:format\": \"application/pdf\"32 }33 }, {34 \"pageRanges\": {35 \"cpf:inline\": [{36 \"start\": 137 }, {38 \"end\": 2539 }, {40 \"start\": 1,41 \"end\": 2542 }]43 },44 \"documentIn\": {45 \"cpf:location\": \"InputFile2\",46 \"dc:format\": \"application/pdf\"47 }48 }]49 },50 \"cpf:engine\": {51 \"repo:assetId\": \"urn:aaid:cpf:Service-916ee91c156b42349a7847a7d564fb13\"52 },53 \"cpf:outputs\": {54 \"documentOut\": {55 \"cpf:location\": \"OutputFile\",56 \"dc:format\": \"application/pdf\"57 }58 }59}"' \60--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"' \61--form 'InputFile1=@"{{Placeholder for input file (absolute path)}}"' \62--form 'InputFile2=@"{{Placeholder for input file (absolute path)}}"'