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