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 image blob given as a parameter.
036 * @since 8.10
037 */
038@Operation(
039        id = PDFWatermarkImageOperation.ID,
040        category = Constants.CAT_CONVERSION,
041        label = "PDF: Watermark with Image",
042        description = PDFWatermarkImageOperation.DESCRIPTION)
043public class PDFWatermarkImageOperation {
044
045    public static final String ID = "PDF.WatermarkWithImage";
046
047    public static final String DESCRIPTION =
048        "<p>Return a <em>new</em> blob combining the input PDF and the<code> image </code>blob.</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>scale </code>(1.0) : 1.0 is the original size of the picture</li>" +
052        "<li><code>alphaColor</code> (0.5) : 0 is full transparency, 1 is solid</li>" +
053        "<li><code>xPosition </code>(0) : in pixels from left or between 0 (left) and 1 (right) if relativeCoordinates is set to true</li>" +
054        "<li><code>yPosition</code> (0) : in pixels from bottom or between 0 (bottom) and 1 (top) if relativeCoordinates is set to true</li>" +
055        "<li><code>invertX</code> (false) : xPosition starts from the right going left</li>" +
056        "<li><code>invertY</code> (false) : yPosition starts from the top going down</li>" +
057        "<li><code>relativeCoordinates</code> (false)</li>" +
058        "</ul>";
059
060    @Param(name = "image", description="The image blob to use for the watermark")
061    Blob image;
062
063    @Param(name = "properties", description="The watermark properties", required = false)
064    protected Properties properties = new Properties();
065
066    @Context
067    protected PDFTransformationService pdfTransformationService;
068
069    @OperationMethod(collector = BlobCollector.class)
070    public Blob run(Blob inBlob) {
071        return pdfTransformationService.
072                applyImageWatermark(inBlob, image, convertProperties());
073    }
074
075    private WatermarkProperties convertProperties() {
076        WatermarkProperties watermarkProperties = pdfTransformationService.getDefaultProperties();
077        watermarkProperties.updateFromMap(properties);
078        return watermarkProperties;
079    }
080}
081