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 java.io.IOException;
023import org.apache.pdfbox.exceptions.COSVisitorException;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.platform.pdf.PDFPageNumbering;
031import org.nuxeo.ecm.platform.pdf.PDFPageNumbering.PAGE_NUMBER_POSITION;
032
033/**
034 * Add page numbers to the PDF.
035 * <p>
036 * If the PDF is encrypted, a password is required.
037 *
038 * @since 8.10
039 */
040@Operation(id = PDFAddPageNumbersOperation.ID, category = Constants.CAT_CONVERSION, label = "PDF: Add Page Numbers",
041    description = "Add the page numbers to the PDF, using the misc parameters. If the PDF is encrypted, a password " +
042        "is required.")
043public class PDFAddPageNumbersOperation {
044
045    public static final String ID = "PDF.AddPageNumbers";
046
047    @Param(name = "startAtPage", required = false, values = { "1" })
048    private long startAtPage = 1;
049
050    @Param(name = "startAtNumber", required = false, values = { "1" })
051    private long startAtNumber = 1;
052
053    @Param(name = "position", required = false, widget = Constants.W_OPTION, values = {
054        "Bottom right", "Bottom center", "Bottom left", "Top right", "Top center", "Top left" })
055    private String position = "Bottom right";
056
057    @Param(name = "fontName", required = false, values = { "Helvetica" })
058    private String fontName = "Helvetica";
059
060    @Param(name = "fontSize", required = false, values = { "16" })
061    private long fontSize = 16;
062
063    @Param(name = "hex255Color", required = false, values = { "0xffffff" })
064    private String hex255Color = "0xffffff";
065
066    @Param(name = "password", required = false)
067    protected String password = null;
068
069    @OperationMethod(collector = BlobCollector.class)
070    public Blob run(Blob inBlob) throws IOException, COSVisitorException {
071        PAGE_NUMBER_POSITION pos;
072        switch (position.toLowerCase()) {
073        case "bottom center":
074            pos = PAGE_NUMBER_POSITION.BOTTOM_CENTER;
075            break;
076        case "bottom left":
077            pos = PAGE_NUMBER_POSITION.BOTTOM_LEFT;
078            break;
079        case "top right":
080            pos = PAGE_NUMBER_POSITION.TOP_RIGHT;
081            break;
082        case "top center":
083            pos = PAGE_NUMBER_POSITION.TOP_CENTER;
084            break;
085        case "top left":
086            pos = PAGE_NUMBER_POSITION.TOP_LEFT;
087            break;
088        default:
089            pos = PAGE_NUMBER_POSITION.BOTTOM_RIGHT;
090            break;
091        }
092        PDFPageNumbering pn = new PDFPageNumbering(inBlob);
093        pn.setPassword(password);
094        Blob result = pn.addPageNumbers((int) startAtPage, (int) startAtNumber, fontName, fontSize, hex255Color, pos);
095        result.setFilename(inBlob.getFilename());
096        return result;
097    }
098
099}