Package br.net.woodstock.rockframework.document.pdf.pdfbox

Source Code of br.net.woodstock.rockframework.document.pdf.pdfbox.MergeProcessor

/*
* This file is part of rockframework.
*
* rockframework 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.
*
* rockframework 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/>;.
*/
package br.net.woodstock.rockframework.document.pdf.pdfbox;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.util.PDFMergerUtility;

import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.core.utils.Arrays;
import br.net.woodstock.rockframework.document.DocumentException;
import br.net.woodstock.rockframework.document.DocumentInput;
import br.net.woodstock.rockframework.document.DocumentOutput;
import br.net.woodstock.rockframework.document.DocumentProcessor;
import br.net.woodstock.rockframework.document.pdf.PDFException;

public class MergeProcessor implements DocumentProcessor {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  public static final String  SOURCES_PARAMETER  = "sources";

  public MergeProcessor() {
    super();
  }

  @Override
  @SuppressWarnings("unchecked")
  public DocumentOutput process(final DocumentInput input) {
    Assert.notNull(input, "input");
    try {
      Object sources = input.getParameter(MergeProcessor.SOURCES_PARAMETER);

      Collection<Object> collection = null;

      if (sources == null) {
        throw new DocumentException("Parameter 'sources' + must be set");
      }

      if (Arrays.isArray(sources)) {
        collection = Arrays.toList(sources);
      } else if (sources instanceof Collection) {
        collection = (Collection<Object>) sources;
      }

      PDFMergerUtility merger = new PDFMergerUtility();
      for (Object source : collection) {
        InputStream inputStream = PDFBox.toInputStream(source);
        merger.addSource(inputStream);
      }

      ByteArrayOutputStream bos = new ByteArrayOutputStream();

      merger.setDestinationStream(bos);
      merger.mergeDocuments();

      ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

      return new DocumentOutput(bis);
    } catch (IOException e) {
      throw new PDFException(e);
    } catch (COSVisitorException e) {
      throw new PDFException(e);
    }
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.document.pdf.pdfbox.MergeProcessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.