Insert Pages
Insert Pages in PDF
The insert operation inserts additional pages from different PDFs into an existing PDF.
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.insertpages.InsertPDFPages45 public class InsertPDFPages {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(InsertPDFPages.class);910 public static void main(String[] args) {11 try {12 // Initial setup, create credentials instance.13 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()14 .fromFile("pdfservices-api-credentials.json")15 .build();1617 // Create an ExecutionContext using credentials and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 InsertPagesOperation insertPagesOperation = InsertPagesOperation.createNew();2021 // Set operation base input from a source file.22 FileRef baseSourceFile = FileRef.createFromLocalFile("src/main/resources/baseInput.pdf");23 insertPagesOperation.setBaseInput(baseSourceFile);2425 // Create a FileRef instance using a local file.26 FileRef firstFileToInsert = FileRef.createFromLocalFile("src/main/resources/firstFileToInsertInput.pdf");27 PageRanges pageRanges = getPageRangeForFirstFile();2829 // Adds the pages (specified by the page ranges) of the input PDF file to be inserted at30 // the specified page of the base PDF file.31 insertPagesOperation.addPagesToInsertAt(firstFileToInsert, pageRanges, 2);3233 // Create a FileRef instance using a local file.34 FileRef secondFileToInsert = FileRef.createFromLocalFile("src/main/resources/secondFileToInsertInput.pdf");3536 // Adds all the pages of the input PDF file to be inserted at the specified page of the37 // base PDF file.38 insertPagesOperation.addPagesToInsertAt(secondFileToInsert, 3);3940 // Execute the operation.41 FileRef result = insertPagesOperation.execute(executionContext);4243 // Save the result to the specified location.44 result.saveAs("output/insertPagesOutput.pdf");4546 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {47 LOGGER.error("Exception encountered while executing operation", e);48 }49 }5051 private static PageRanges getPageRangeForFirstFile() {52 // Specify which pages of the first file are to be inserted in the base 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 InsertPDFPages/4// dotnet run InsertPDFPages.csproj56 namespace InsertPDFPages7 {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 InsertPagesOperation insertPagesOperation = InsertPagesOperation.CreateNew();2728 // Set operation base input from a source file.29 FileRef baseSourceFile = FileRef.CreateFromLocalFile(@"baseInput.pdf");30 insertPagesOperation.SetBaseInput(baseSourceFile);3132 // Create a FileRef instance using a local file.33 FileRef firstFileToInsert = FileRef.CreateFromLocalFile(@"firstFileToInsertInput.pdf");34 PageRanges pageRanges = GetPageRangeForFirstFile();3536 // Adds the pages (specified by the page ranges) of the input PDF file to be inserted at37 // the specified page of the base PDF file.38 insertPagesOperation.AddPagesToInsertAt(firstFileToInsert, pageRanges, 2);3940 // Create a FileRef instance using a local file.41 FileRef secondFileToInsert = FileRef.CreateFromLocalFile(@"secondFileToInsertInput.pdf");4243 // Adds all the pages of the input PDF file to be inserted at the specified page of the44 // base PDF file.45 insertPagesOperation.AddPagesToInsertAt(secondFileToInsert, 3);4647 // Execute the operation.48 FileRef result = insertPagesOperation.Execute(executionContext);4950 // Save the result to the specified location.51 result.SaveAs(Directory.GetCurrentDirectory() + "/output/insertPagesOutput.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 which pages of the first file are to be inserted in the base 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/insertpages/insert-pdf-pages.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 const getPageRangesForFirstFile = () => {8 // Specify which pages of the first file are to be inserted in the base 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 insertPagesOperation = PDFServicesSdk.InsertPages.Operation.createNew();2930 // Set operation base input from a source file.31 const baseInputFile = PDFServicesSdk.FileRef.createFromLocalFile('resources/baseInput.pdf');32 insertPagesOperation.setBaseInput(baseInputFile);3334 // Create a FileRef instance using a local file.35 const firstFileToInsert = PDFServicesSdk.FileRef.createFromLocalFile('resources/firstFileToInsertInput.pdf'),36 pageRanges = getPageRangesForFirstFile();3738 // Adds the pages (specified by the page ranges) of the input PDF file to be inserted at39 // the specified page of the base PDF file.40 insertPagesOperation.addPagesToInsertAt(2, firstFileToInsert, pageRanges);4142 // Create a FileRef instance using a local file.43 const secondFileToInsert = PDFServicesSdk.FileRef.createFromLocalFile('resources/secondFileToInsertInput.pdf');4445 // Adds all the pages of the input PDF file to be inserted at the specified page of the46 // base PDF file.47 insertPagesOperation.addPagesToInsertAt(3, secondFileToInsert);4849 // Execute the operation and Save the result to the specified location.50 insertPagesOperation.execute(executionContext)51 .then(result => result.saveAsFile('output/insertPagesOutput.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://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)}}"'