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

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

/*
* 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.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
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.ImageType;
import br.net.woodstock.rockframework.document.pdf.PDFException;

public class ToImageProcessor implements DocumentProcessor {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  public static final String  SOURCE_PARAMETER  = "source";

  public static final String  TYPE_PARAMETER    = "type";

  public ToImageProcessor() {
    super();
  }

  @SuppressWarnings("unchecked")
  @Override
  public DocumentOutput process(final DocumentInput input) {
    Assert.notNull(input, "input");
    try {
      Object source = input.getParameter(ToImageProcessor.SOURCE_PARAMETER);
      Object type = input.getParameter(ToImageProcessor.TYPE_PARAMETER);

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

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

      String strType = null;

      if (type instanceof ImageType) {
        strType = ((ImageType) type).getName();
      } else {
        strType = type.toString();
      }

      PDDocument document = PDFBox.load(source);
      List<PDPage> pages = document.getDocumentCatalog().getAllPages();
      int pageCount = pages.size();
      int index = 0;
      InputStream[] array = new InputStream[pageCount];
      for (PDPage page : pages) {
        BufferedImage image = page.convertToImage();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, strType, outputStream);
        InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        array[index++] = inputStream;
      }
      return new DocumentOutput(array);
    } catch (IOException e) {
      throw new PDFException(e);
    }
  }

}
TOP

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

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.