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
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.replacepages.ReplacePDFPages
public class ReplacePDFPages {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(ReplacePDFPages.class);
public static void main(String[] args) {
try {
// Initial setup, create credentials instance.
Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()
.withClientId("PDF_SERVICES_CLIENT_ID")
.withClientSecret("PDF_SERVICES_CLIENT_SECRET")
.build();
// Create an ExecutionContext using credentials and create a new operation instance.
ExecutionContext executionContext = ExecutionContext.create(credentials);
ReplacePagesOperation replacePagesOperation = ReplacePagesOperation.createNew();
// Set operation base input from a source file.
FileRef baseSourceFile = FileRef.createFromLocalFile("src/main/resources/baseInput.pdf");
replacePagesOperation.setBaseInput(baseSourceFile);
// Create a FileRef instance using a local file.
FileRef firstInputFile = FileRef.createFromLocalFile("src/main/resources/replacePagesInput1.pdf");
PageRanges pageRanges = getPageRangeForFirstFile();
// Adds the pages (specified by the page ranges) of the input PDF file for replacing the
// page of the base PDF file.
replacePagesOperation.addPagesForReplace(firstInputFile, pageRanges, 1);
// Create a FileRef instance using a local file.
FileRef secondInputFile = FileRef.createFromLocalFile("src/main/resources/replacePagesInput2.pdf");
// Adds all the pages of the input PDF file for replacing the page of the base PDF file.
replacePagesOperation.addPagesForReplace(secondInputFile, 3);
// Execute the operation
FileRef result = replacePagesOperation.execute(executionContext);
// Save the result at the specified location
result.saveAs("output/replacePagesOutput.pdf");
} catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
LOGGER.error("Exception encountered while executing operation", e);
}
}
private static PageRanges getPageRangeForFirstFile() {
// Specify pages of the first file for replacing the page of base PDF file.
PageRanges pageRanges = new PageRanges();
// Add pages 1 to 3.
pageRanges.addRange(1, 3);
// Add page 4.
pageRanges.addSinglePage(4);
return pageRanges;
}
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.