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
// 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.insertpages.InsertPDFPages
public class InsertPDFPages {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(InsertPDFPages.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);
InsertPagesOperation insertPagesOperation = InsertPagesOperation.createNew();
// Set operation base input from a source file.
FileRef baseSourceFile = FileRef.createFromLocalFile("src/main/resources/baseInput.pdf");
insertPagesOperation.setBaseInput(baseSourceFile);
// Create a FileRef instance using a local file.
FileRef firstFileToInsert = FileRef.createFromLocalFile("src/main/resources/firstFileToInsertInput.pdf");
PageRanges pageRanges = getPageRangeForFirstFile();
// Adds the pages (specified by the page ranges) of the input PDF file to be inserted at
// the specified page of the base PDF file.
insertPagesOperation.addPagesToInsertAt(firstFileToInsert, pageRanges, 2);
// Create a FileRef instance using a local file.
FileRef secondFileToInsert = FileRef.createFromLocalFile("src/main/resources/secondFileToInsertInput.pdf");
// Adds all the pages of the input PDF file to be inserted at the specified page of the
// base PDF file.
insertPagesOperation.addPagesToInsertAt(secondFileToInsert, 3);
// Execute the operation.
FileRef result = insertPagesOperation.execute(executionContext);
// Save the result to the specified location.
result.saveAs("output/insertPagesOutput.pdf");
} catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
LOGGER.error("Exception encountered while executing operation", e);
}
}
private static PageRanges getPageRangeForFirstFile() {
// Specify which pages of the first file are to be inserted in the base 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.