001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thibaud Arguillere
018 *     Miguel Nixo
019 */
020package org.nuxeo.ecm.platform.pdf.operations;
021
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.platform.pdf.PDFPageExtractor;
031
032/**
033 * Extract pages from <code>startPage</code> to <code>endPage</code> (inclusive) from the input object. If a Blob is
034 * used as input, the <code>xpath</code> parameter is not used. <code>title</code>, <code>subject</code> and
035 * <code>author</code> are optional.
036 * <p>
037 * If the PDF is encrypted, a password is required.
038 *
039 * @since 8.10
040 */
041@Operation(id = PDFExtractPagesOperation.ID, category = Constants.CAT_CONVERSION, label = "PDF: Extract Pages",
042    description = "Extract pages from <code>startPage</code> to <code>endPage</code> (inclusive) from the input " +
043        "object. If a Blob is used as input, the <code>xpath</xpath> parameter is not used. <code>title</code>, " +
044        "<code>subject</code> and <code>author</code> are optional. If the PDF is encrypted, a password is required.")
045public class PDFExtractPagesOperation {
046
047    public static final String ID = "PDF.ExtractPages";
048
049    @Context
050    protected CoreSession session;
051
052    @Param(name = "startPage")
053    protected long startPage;
054
055    @Param(name = "endPage")
056    protected long endPage;
057
058    @Param(name = "fileName", required = false)
059    protected String fileName = "";
060
061    @Param(name = "pdfTitle", required = false)
062    protected String pdfTitle = "";
063
064    @Param(name = "pdfSubject", required = false)
065    protected String pdfSubject;
066
067    @Param(name = "pdfAuthor", required = false)
068    protected String pdfAuthor = "";
069
070    @Param(name = "xpath", required = false, values = { "file:content" })
071    protected String xpath = "";
072
073    @Param(name = "password", required = false)
074    protected String password = null;
075
076    @OperationMethod
077    public Blob run(Blob inBlob) {
078        PDFPageExtractor pe = new PDFPageExtractor(inBlob);
079        pe.setPassword(password);
080        return pe.extract((int) startPage, (int) endPage, fileName, pdfTitle, pdfSubject, pdfAuthor);
081    }
082
083    @OperationMethod
084    public Blob run(DocumentModel inDoc) {
085        PDFPageExtractor pe = new PDFPageExtractor(inDoc, xpath);
086        pe.setPassword(password);
087        return pe.extract((int) startPage, (int) endPage, fileName, pdfTitle, pdfSubject, pdfAuthor);
088    }
089
090}