Edit in GitHubLog an issue

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.

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.replacepages.ReplacePDFPages
4
5 public class ReplacePDFPages {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(ReplacePDFPages.class);
9
10 public static void main(String[] args) {
11
12 try (InputStream baseInputStream = Files.newInputStream(new File("src/main/resources/baseInput.pdf").toPath());
13 InputStream inputStream1 = Files.newInputStream(new File("src/main/resources/replacePagesInput1.pdf").toPath());
14 InputStream inputStream2 = Files.newInputStream(new File("src/main/resources/replacePagesInput2.pdf").toPath())) {
15 // Initial setup, create credentials instance
16 Credentials credentials = new ServicePrincipalCredentials(
17 System.getenv("PDF_SERVICES_CLIENT_ID"),
18 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
19
20 // Creates a PDF Services instance
21 PDFServices pdfServices = new PDFServices(credentials);
22
23 // Creates an asset(s) from source file(s) and upload
24 Asset baseAsset = pdfServices.upload(baseInputStream, PDFServicesMediaType.PDF.getMediaType());
25 Asset asset1 = pdfServices.upload(inputStream1, PDFServicesMediaType.PDF.getMediaType());
26 Asset asset2 = pdfServices.upload(inputStream2, PDFServicesMediaType.PDF.getMediaType());
27
28 PageRanges pageRanges = getPageRangeForFirstFile();
29
30 // Create parameters for the job
31 ReplacePagesParams replacePagesParams = ReplacePagesParams.replacePagesParamsBuilder(baseAsset)
32 .addPagesForReplace(asset1, pageRanges, 1) // Add the first asset as input to the params, along with its page ranges and base page
33 .addPagesForReplace(asset2, 3) // Add the second asset as input to the params, along with base page
34 .build();
35
36 // Creates a new job instance
37 ReplacePagesPDFJob replacePagesPDFJob = new ReplacePagesPDFJob(replacePagesParams);
38
39 // Submit the job and gets the job result
40 String location = pdfServices.submit(replacePagesPDFJob);
41 PDFServicesResponse<ReplacePagesResult> pdfServicesResponse = pdfServices.getJobResult(location, ReplacePagesResult.class);
42
43 // Get content from the resulting asset(s)
44 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
45 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
46
47 // Creates an output stream and copy stream asset's content to it
48 Files.createDirectories(Paths.get("output/"));
49 OutputStream outputStream = Files.newOutputStream(new File("output/replacePagesOutput.pdf").toPath());
50 LOGGER.info("Saving asset at output/replacePagesOutput.pdf");
51 IOUtils.copy(streamAsset.getInputStream(), outputStream);
52 outputStream.close();
53 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
54 LOGGER.error("Exception encountered while executing operation", e);
55 }
56 }
57
58 private static PageRanges getPageRangeForFirstFile() {
59 // Specify pages of the first file for replacing the page of base PDF file
60 PageRanges pageRanges = new PageRanges();
61 // Add pages 1 to 3
62 pageRanges.addRange(1, 3);
63
64 // Add page 4
65 pageRanges.addSinglePage(4);
66
67 return pageRanges;
68 }
69 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.