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 *     Michael Vachette
020 */
021package org.nuxeo.ecm.platform.pdf.operations;
022
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
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.automation.core.util.Properties;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.platform.pdf.service.PDFTransformationService;
032import org.nuxeo.ecm.platform.pdf.service.watermark.WatermarkProperties;
033
034/**
035 * Return a new blob combining the input PDF and the watermark text given as a parameter.
036 * @since 8.10
037 */
038@Operation(
039        id = PDFWatermarkTextOperation.ID,
040        category = Constants.CAT_CONVERSION,
041        label = "PDF: Watermark with Text",
042        description = PDFWatermarkTextOperation.DESCRIPTION)
043public class PDFWatermarkTextOperation {
044
045    public static final String ID = "PDF.WatermarkWithText";
046
047    public static final String DESCRIPTION =
048        "<p>Return a <em>new</em> blob combining the input PDF and the <code>text</code> text.</p>" +
049        "<p>Properties must be one or more of the following (the default if the property is not set):</p>" +
050        "<ul>" +
051        "<li><code>fontFamily</code> (Helvetica) </li>" +
052        "<li><code>fontSize</code> (72)</li>" +
053        "<li><code>rotation</code> (0): in&nbsp;counterclockwise degrees</li>" +
054        "<li><code>hex255Color</code> (#000000)</li>" +
055        "<li><code>alphaColor</code> (0.5) : 0 is full transparency, 1 is solid</li>" +
056        "<li><code>xPosition</code> (0) : in pixels from left or between 0 (left) and 1 (right) if relativeCoordinates is set to true</li>" +
057        "<li><code>yPosition</code> (0) : in pixels from bottom or between 0 (bottom) and 1 (top) if relativeCoordinates is set to true</li>" +
058        "<li><code>invertX</code> (false) : xPosition starts from the right going left</li>" +
059        "<li><code>invertY</code> (false) : yPosition starts from the top going down</li>" +
060        "<li><code>relativeCoordinates</code> (false)</li>" +
061        "</ul>";
062
063    @Param(name = "text", description="The text to use for the watermark")
064    protected String text;
065
066    @Param(name = "properties", description="The watermark properties", required = false)
067    protected Properties properties = new Properties();
068
069    @Context
070    protected PDFTransformationService pdfTransformationService;
071
072    @OperationMethod(collector = BlobCollector.class)
073    public Blob run(Blob inBlob) {
074        return pdfTransformationService.
075                applyTextWatermark(inBlob, text, convertProperties());
076    }
077
078    private WatermarkProperties convertProperties() {
079        WatermarkProperties watermarkProperties = pdfTransformationService.getDefaultProperties();
080        watermarkProperties.updateFromMap(properties);
081        return watermarkProperties;
082    }
083
084}
085