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

Source Code of br.net.woodstock.rockframework.document.pdf.itextpdf.CutProcessor

/*
* 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.itextpdf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;

public class CutProcessor implements DocumentProcessor {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  public static final String  SOURCE_PARAMETER  = "source";

  public static final String  START_PARAMETER    = "start";

  public static final String  END_PARAMETER    = "end";

  public CutProcessor() {
    super();
  }

  @Override
  public DocumentOutput process(final DocumentInput input) {
    Assert.notNull(input, "input");
    try {
      Object source = input.getParameter(CutProcessor.SOURCE_PARAMETER);
      Integer start = input.getParameter(CutProcessor.START_PARAMETER);
      Integer end = input.getParameter(CutProcessor.END_PARAMETER);

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

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

      if (end == null) {
        end = Integer.valueOf(0);
      }

      PdfReader reader = IText.read(source);

      Document document = new Document(reader.getPageSizeWithRotation(1));
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      PdfCopy writer = new PdfCopy(document, outputStream);
      int pageCount = reader.getNumberOfPages();

      if (start.intValue() > pageCount) {
        throw new DocumentException("Start page is greater than page count");
      }

      int endPage = end.intValue();
      if ((endPage < 1) || (endPage > pageCount)) {
        endPage = pageCount;
      }

      document.open();

      for (int i = start.intValue(); i <= endPage; i++) {
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

      document.close();
      writer.close();
      reader.close();

      return new DocumentOutput(new ByteArrayInputStream(outputStream.toByteArray()));
    } catch (IOException e) {
      throw new PDFException(e);
    } catch (DocumentException e) {
      throw new PDFException(e);
    }
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.document.pdf.itextpdf.CutProcessor

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.