All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.pdfservices.operation.pdfjobs.jobs.InsertPagesPDFJob Maven / Gradle / Ivy

Go to download

Adobe PDF Services SDK allows you to access RESTful APIs to create, convert, and manipulate PDFs within your applications. Older versions can be found under groupId: com.adobe.documentservices, artifactId: pdftools-sdk

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright 2024 Adobe
 * All Rights Reserved.
 *
 * NOTICE: Adobe permits you to use, modify, and distribute this file in
 * accordance with the terms of the Adobe license agreement accompanying
 * it. If you have received this file from a source other than Adobe,
 * then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 */

package com.adobe.pdfservices.operation.pdfjobs.jobs;

import com.adobe.pdfservices.operation.PDFServicesJob;
import com.adobe.pdfservices.operation.config.notifier.NotifierConfig;
import com.adobe.pdfservices.operation.exception.ServiceApiException;
import com.adobe.pdfservices.operation.internal.ExecutionContext;
import com.adobe.pdfservices.operation.internal.PDFServicesHelper;
import com.adobe.pdfservices.operation.internal.constants.CustomErrorMessages;
import com.adobe.pdfservices.operation.internal.constants.OperationHeaderInfoEndpointMap;
import com.adobe.pdfservices.operation.internal.dto.request.PlatformApiRequest;
import com.adobe.pdfservices.operation.internal.dto.request.combinepdf.CombinePDFExternalAssetRequest;
import com.adobe.pdfservices.operation.internal.dto.request.combinepdf.CombinePDFInternalAssetRequest;
import com.adobe.pdfservices.operation.internal.http.DefaultRequestHeaders;
import com.adobe.pdfservices.operation.internal.http.HttpResponse;
import com.adobe.pdfservices.operation.internal.params.CombinePDFJobInput;
import com.adobe.pdfservices.operation.internal.util.ObjectUtil;
import com.adobe.pdfservices.operation.io.Asset;
import com.adobe.pdfservices.operation.io.CloudAsset;
import com.adobe.pdfservices.operation.io.ExternalAsset;
import com.adobe.pdfservices.operation.pdfjobs.params.PageRanges;
import com.adobe.pdfservices.operation.pdfjobs.params.combinepdf.CombinePDFParams;
import com.adobe.pdfservices.operation.pdfjobs.params.insertpages.InsertPagesParams;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * A job that can be used to insert pages of multiple PDF files into a base PDF file.
 * 

* For more complex use cases, refer the {@link CombinePDFJob}. *

* Sample Usage: *

{@code
 *             InputStream baseInputStream = new FileInputStream(new File("SOURCE_PATH"));
 *             InputStream firstInputStreamToInsert = new FileInputStream(new File("SOURCE_PATH"));
 *             InputStream secondInputStreamToInsert = new FileInputStream(new File("SOURCE_PATH"));
 *
 *             Credentials credentials = new ServicePrincipalCredentials(
 *                     System.getenv("PDF_SERVICES_CLIENT_ID"),
 *                     System.getenv("PDF_SERVICES_CLIENT_SECRET"));
 *
 *             PDFServices pdfServices = new PDFServices(credentials);
 *
 *             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 = new PageRanges();
 *             pageRanges.addSinglePage(1, 3);
 *             pageRanges.addRange(4);
 *
 *             InsertPagesParams insertPagesParams = InsertPagesParams.insertPagesParamsBuilder(baseAsset)
 *                     .addPagesToInsertAt(firstAssetToInsert, pageRanges, 2)
 *                     .addPagesToInsertAt(secondAssetToInsert, 3)
 *                     .build();
 *
 *             InsertPagesPDFJob insertPagesJob = new InsertPagesPDFJob(insertPagesParams);
 *
 *             String location = pdfServices.submit(insertPagesJob);
 *             PDFServicesResponse pdfServicesResponse = pdfServices.getJobResult(location, InsertPagesResult.class);
 *
 *             Asset resultAsset = pdfServicesResponse.getResult().getAsset();
 *             StreamAsset streamAsset = pdfServices.getContent(resultAsset);
 * }
*/ public class InsertPagesPDFJob extends PDFServicesJob { private Asset outputAsset; private InsertPagesParams insertPagesParams; /** * Constructs a new {@code InsertPagesPDFJob} instance. * * @param insertPagesParams {@link InsertPagesParams} object containing the input files and the page numbers to * insert at; can not be null. */ public InsertPagesPDFJob(InsertPagesParams insertPagesParams) { ObjectUtil.requireNonNull(insertPagesParams, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Insert Pages parameters")); this.insertPagesParams = insertPagesParams; } @Override protected String process(ExecutionContext executionContext) throws ServiceApiException { return this.process(executionContext, null); } @Override protected String process(ExecutionContext executionContext, List notifyConfigList) throws ServiceApiException { this.validate(executionContext); PlatformApiRequest combinePDFRequest = generatePlatformApiRequest(notifyConfigList); String xRequestId = UUID.randomUUID().toString(); HttpResponse response = PDFServicesHelper.submitJob(executionContext, combinePDFRequest, xRequestId, OperationHeaderInfoEndpointMap.INSERT_PAGES); return response.getHeaders().get(DefaultRequestHeaders.LOCATION_HEADER_NAME); } /** * Sets the output asset for the job. * {@code @note} External assets can be set as output only when input is external asset as well * * @param asset {@link Asset} object representing the output asset; can not be null. * @return {@code InsertPagesPDFJob} instance */ public InsertPagesPDFJob setOutput(Asset asset) { ObjectUtil.requireNonNull(asset, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Output asset")); if (this.insertPagesParams.getBaseAsset() instanceof CloudAsset) { throw new IllegalArgumentException(CustomErrorMessages.SET_OUTPUT_VALIDATE); } this.outputAsset = asset; return this; } private static CombinePDFParams getFilesToInsert(Asset baseAsset, Map> assetsToInsert) { List assetList = new ArrayList<>(); List pageRangeList = new ArrayList<>(); int baseFileStartIndex = 1; for (Map.Entry> entry : assetsToInsert.entrySet()) { if (entry.getKey() != 1) { assetList.add(baseAsset); PageRanges pageRanges = new PageRanges(); pageRanges.addRange(baseFileStartIndex, entry.getKey() - 1); pageRangeList.add(pageRanges); baseFileStartIndex = entry.getKey(); } for (CombinePDFJobInput combinePDFJobInput : entry.getValue()) { assetList.add(combinePDFJobInput.getAsset()); pageRangeList.add(combinePDFJobInput.getPageRanges()); } } assetList.add(baseAsset); PageRanges basePageRanges = new PageRanges(); basePageRanges.addAllFrom(baseFileStartIndex); pageRangeList.add(basePageRanges); CombinePDFParams.Builder combinePDFParamsBuilder = CombinePDFParams.combinePDFParamsBuilder(); for (int i = 0; i < assetList.size(); i++) { combinePDFParamsBuilder.addAsset(assetList.get(i), pageRangeList.get(i)); } return combinePDFParamsBuilder.build(); } private PlatformApiRequest generatePlatformApiRequest(List notifyConfigList) { CombinePDFParams combinePDFParams = getFilesToInsert(this.insertPagesParams.getBaseAsset(), this.insertPagesParams.getAssetsToInsert()); PlatformApiRequest combinePDFRequest; if (combinePDFParams.getFilesToCombine().stream().findFirst().get().getAsset() instanceof CloudAsset) { combinePDFRequest = new CombinePDFInternalAssetRequest(combinePDFParams, notifyConfigList); } else { combinePDFRequest = new CombinePDFExternalAssetRequest(combinePDFParams, notifyConfigList).setOutput((ExternalAsset) outputAsset); } return combinePDFRequest; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy