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 (InputStream baseInputStream = Files.newInputStream(new File("src/main/resources/baseInput.pdf").toPath());
InputStream firstInputStreamToInsert = Files.newInputStream(new File("src/main/resources/firstFileToInsertInput.pdf").toPath());
InputStream secondInputStreamToInsert = Files.newInputStream(new File("src/main/resources/secondFileToInsertInput.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset baseAsset = pdfServices.upload(baseInputStream, PDFServicesMediaType.PDF.getMediaType());
Asset firstAssetToInsert = pdfServices.upload(firstInputStreamToInsert, PDFServicesMediaType.PDF.getMediaType());
Asset secondAssetToInsert = pdfServices.upload(secondInputStreamToInsert, PDFServicesMediaType.PDF.getMediaType());
PageRanges pageRanges = getPageRangeForFirstFile();
// Create parameters for the job
InsertPagesParams insertPagesParams = InsertPagesParams.insertPagesParamsBuilder(baseAsset)
.addPagesToInsertAt(firstAssetToInsert, pageRanges, 2) // Add the first asset as input to the params, along with its page ranges and base page
.addPagesToInsertAt(secondAssetToInsert, 3) // Add the seccond asset as input to the params, along with base page
.build();
// Creates a new job instance
InsertPagesPDFJob insertPagesJob = new InsertPagesPDFJob(insertPagesParams);
// Submit the job and gets the job result
String location = pdfServices.submit(insertPagesJob);
PDFServicesResponse<InsertPagesResult> pdfServicesResponse = pdfServices.getJobResult(location, InsertPagesResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/insertPagesOutput.pdf").toPath());
LOGGER.info("Saving asset at output/insertPagesOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} 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.