Edit in GitHubLog an issue

Insert Pages

Insert one or more pages into an existing document

REST API

See our public API Reference for Insert Pages

Insert Pages in PDF

The insert operation inserts additional pages from different PDFs into an existing PDF.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.insertpages.InsertPDFPages
4
5 public class InsertPDFPages {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(InsertPDFPages.class);
9
10 public static void main(String[] args) {
11 try (InputStream baseInputStream = Files.newInputStream(new File("src/main/resources/baseInput.pdf").toPath());
12 InputStream firstInputStreamToInsert = Files.newInputStream(new File("src/main/resources/firstFileToInsertInput.pdf").toPath());
13 InputStream secondInputStreamToInsert = Files.newInputStream(new File("src/main/resources/secondFileToInsertInput.pdf").toPath())) {
14 // Initial setup, create credentials instance
15 Credentials credentials = new ServicePrincipalCredentials(
16 System.getenv("PDF_SERVICES_CLIENT_ID"),
17 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
18
19 // Creates a PDF Services instance
20 PDFServices pdfServices = new PDFServices(credentials);
21
22 // Creates an asset(s) from source file(s) and upload
23 Asset baseAsset = pdfServices.upload(baseInputStream, PDFServicesMediaType.PDF.getMediaType());
24 Asset firstAssetToInsert = pdfServices.upload(firstInputStreamToInsert, PDFServicesMediaType.PDF.getMediaType());
25 Asset secondAssetToInsert = pdfServices.upload(secondInputStreamToInsert, PDFServicesMediaType.PDF.getMediaType());
26
27 PageRanges pageRanges = getPageRangeForFirstFile();
28
29 // Create parameters for the job
30 InsertPagesParams insertPagesParams = InsertPagesParams.insertPagesParamsBuilder(baseAsset)
31 .addPagesToInsertAt(firstAssetToInsert, pageRanges, 2) // Add the first asset as input to the params, along with its page ranges and base page
32 .addPagesToInsertAt(secondAssetToInsert, 3) // Add the seccond asset as input to the params, along with base page
33 .build();
34
35 // Creates a new job instance
36 InsertPagesPDFJob insertPagesJob = new InsertPagesPDFJob(insertPagesParams);
37
38 // Submit the job and gets the job result
39 String location = pdfServices.submit(insertPagesJob);
40 PDFServicesResponse<InsertPagesResult> pdfServicesResponse = pdfServices.getJobResult(location, InsertPagesResult.class);
41
42 // Get content from the resulting asset(s)
43 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
44 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
45
46 // Creates an output stream and copy stream asset's content to it
47 Files.createDirectories(Paths.get("output/"));
48 OutputStream outputStream = Files.newOutputStream(new File("output/insertPagesOutput.pdf").toPath());
49 LOGGER.info("Saving asset at output/insertPagesOutput.pdf");
50 IOUtils.copy(streamAsset.getInputStream(), outputStream);
51 outputStream.close();
52 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
53 LOGGER.error("Exception encountered while executing operation", e);
54 }
55 }
56
57 private static PageRanges getPageRangeForFirstFile() {
58 // Specify which pages of the first file are to be inserted in the base file
59 PageRanges pageRanges = new PageRanges();
60 // Add pages 1 to 3
61 pageRanges.addRange(1, 3);
62
63 // Add page 4.
64 pageRanges.addSinglePage(4);
65
66 return pageRanges;
67 }
68 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.