adams.flow.transformer.PDFStamp.java Source code

Java tutorial

Introduction

Here is the source code for adams.flow.transformer.PDFStamp.java

Source

/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * PDFStamp.java
 * Copyright (C) 2015 University of Waikato, Hamilton, New Zealand
 */

package adams.flow.transformer;

import adams.core.QuickInfoHelper;
import adams.core.io.FileUtils;
import adams.core.io.PlaceholderFile;
import adams.flow.core.Token;
import adams.flow.transformer.pdfstamp.AbstractStamper;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;

/**
 <!-- globalinfo-start -->
 * Actor for stamping pages in a PDF with a custom overlay.
 * <br><br>
 <!-- globalinfo-end -->
 *
 <!-- flow-summary-start -->
 * Input&#47;output:<br>
 * - accepts:<br>
 * &nbsp;&nbsp;&nbsp;java.lang.String<br>
 * &nbsp;&nbsp;&nbsp;java.io.File<br>
 * - generates:<br>
 * &nbsp;&nbsp;&nbsp;java.lang.String<br>
 * <br><br>
 <!-- flow-summary-end -->
 *
 <!-- options-start -->
 * <pre>-logging-level &lt;OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST&gt; (property: loggingLevel)
 * &nbsp;&nbsp;&nbsp;The logging level for outputting errors and debugging output.
 * &nbsp;&nbsp;&nbsp;default: WARNING
 * </pre>
 * 
 * <pre>-name &lt;java.lang.String&gt; (property: name)
 * &nbsp;&nbsp;&nbsp;The name of the actor.
 * &nbsp;&nbsp;&nbsp;default: PDFStamp
 * </pre>
 * 
 * <pre>-annotation &lt;adams.core.base.BaseAnnotation&gt; (property: annotations)
 * &nbsp;&nbsp;&nbsp;The annotations to attach to this actor.
 * &nbsp;&nbsp;&nbsp;default: 
 * </pre>
 * 
 * <pre>-skip &lt;boolean&gt; (property: skip)
 * &nbsp;&nbsp;&nbsp;If set to true, transformation is skipped and the input token is just forwarded 
 * &nbsp;&nbsp;&nbsp;as it is.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 * 
 * <pre>-stop-flow-on-error &lt;boolean&gt; (property: stopFlowOnError)
 * &nbsp;&nbsp;&nbsp;If set to true, the flow gets stopped in case this actor encounters an error;
 * &nbsp;&nbsp;&nbsp; useful for critical actors.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 * 
 * <pre>-silent &lt;boolean&gt; (property: silent)
 * &nbsp;&nbsp;&nbsp;If enabled, then no errors are output in the console.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 * 
 * <pre>-stamper &lt;adams.flow.transformer.pdfstamp.AbstractStamper&gt; (property: stamper)
 * &nbsp;&nbsp;&nbsp;The stamping algorithm to apply to the PDF.
 * &nbsp;&nbsp;&nbsp;default: adams.flow.transformer.pdfstamp.PassThrough
 * </pre>
 * 
 * <pre>-output &lt;adams.core.io.PlaceholderFile&gt; (property: output)
 * &nbsp;&nbsp;&nbsp;The PDF file to save the combined PDF to.
 * &nbsp;&nbsp;&nbsp;default: ${CWD}
 * </pre>
 * 
 <!-- options-end -->
 *
 * @author  fracpete (fracpete at waikato dot ac dot nz)
 * @version $Revision$
 */
public class PDFStamp extends AbstractTransformer {

    /** for serialization. */
    private static final long serialVersionUID = 5783362940767103716L;

    /** the stamper to use. */
    protected AbstractStamper m_Stamper;

    /** the output file. */
    protected PlaceholderFile m_Output;

    /**
     * Returns a string describing the object.
     *
     * @return          a description suitable for displaying in the gui
     */
    @Override
    public String globalInfo() {
        return "Actor for stamping pages in a PDF with a custom overlay.";
    }

    /**
     * Adds options to the internal list of options.
     */
    @Override
    public void defineOptions() {
        super.defineOptions();

        m_OptionManager.add("stamper", "stamper", new adams.flow.transformer.pdfstamp.PassThrough());

        m_OptionManager.add("output", "output", new PlaceholderFile("."));
    }

    /**
     * Sets the stamper to use.
     *
     * @param value   the stamper
     */
    public void setStamper(AbstractStamper value) {
        m_Stamper = value;
        reset();
    }

    /**
     * Returns the stamper in use.
     *
     * @return       the stamper
     */
    public AbstractStamper getStamper() {
        return m_Stamper;
    }

    /**
     * Returns the tip text for this property.
     *
     * @return         tip text for this property suitable for
     *             displaying in the GUI or for listing the options.
     */
    public String stamperTipText() {
        return "The stamping algorithm to apply to the PDF.";
    }

    /**
     * Sets the output file.
     *
     * @param value   the file
     */
    public void setOutput(PlaceholderFile value) {
        m_Output = value;
        reset();
    }

    /**
     * Returns the output file.
     *
     * @return       the file
     */
    public PlaceholderFile getOutput() {
        return m_Output;
    }

    /**
     * Returns the tip text for this property.
     *
     * @return         tip text for this property suitable for
     *             displaying in the GUI or for listing the options.
     */
    public String outputTipText() {
        return "The PDF file to save the combined PDF to.";
    }

    /**
     * Returns a quick info about the actor, which will be displayed in the GUI.
     *
     * @return      null if no info available, otherwise short string
     */
    @Override
    public String getQuickInfo() {
        String result;

        result = QuickInfoHelper.toString(this, "stamper", m_Stamper, "stamper: ");
        result += QuickInfoHelper.toString(this, "output", m_Output, ", output: ");

        return result;
    }

    /**
     * Returns the class that the consumer accepts.
     *
     * @return      <!-- flow-accepts-start -->java.lang.String.class, java.io.File.class<!-- flow-accepts-end -->
     */
    public Class[] accepts() {
        return new Class[] { String.class, File.class };
    }

    /**
     * Returns the class of objects that it generates.
     *
     * @return      <!-- flow-generates-start -->java.lang.String.class<!-- flow-generates-end -->
     */
    public Class[] generates() {
        return new Class[] { String.class };
    }

    /**
     * Executes the flow item.
     *
     * @return      null if everything is fine, otherwise error message
     */
    @Override
    protected String doExecute() {
        String result;
        PlaceholderFile file;
        PdfReader reader;
        PdfStamper stamper;
        FileOutputStream fos;

        result = null;

        // get files
        if (m_InputToken.getPayload() instanceof String)
            file = new PlaceholderFile((String) m_InputToken.getPayload());
        else
            file = new PlaceholderFile((File) m_InputToken.getPayload());

        reader = null;
        stamper = null;
        fos = null;
        try {
            reader = new PdfReader(file.getAbsolutePath());
            fos = new FileOutputStream(m_Output.getAbsolutePath());
            stamper = new PdfStamper(reader, fos);
            m_Stamper.stamp(stamper);
        } catch (Exception e) {
            result = handleException("Failed to stamp PDF file: " + file, e);
        } finally {
            try {
                if (stamper != null)
                    stamper.close();
            } catch (Exception e) {
                // ignored
            }
            try {
                if (reader != null)
                    reader.close();
            } catch (Exception e) {
                // ignored
            }
            FileUtils.closeQuietly(fos);
        }

        if (result == null)
            m_OutputToken = new Token(m_Output.getAbsolutePath());

        return result;
    }
}