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

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

/*
* 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 java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.core.utils.Collections;
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 SplitProcessor implements DocumentProcessor {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  public static final String  SOURCE_PARAMETER  = "source";

  public static final String  SIZE_PARAMETER    = "size";

  public SplitProcessor() {
    super();
  }

  @Override
  public DocumentOutput process(final DocumentInput input) {
    Assert.notNull(input, "input");
    try {
      Object source = input.getParameter(SplitProcessor.SOURCE_PARAMETER);
      Integer size = input.getParameter(SplitProcessor.SIZE_PARAMETER);

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

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

      PdfReader reader = IText.read(source);
      int pageCount = reader.getNumberOfPages();
      List<InputStream> list = new LinkedList<InputStream>();

      Document document = null;
      ByteArrayOutputStream outputStream = null;
      PdfCopy writer = null;
      int iSize = size.intValue();
      for (int i = 1; i <= pageCount; i++) {
        if ((document == null) || ((i % iSize) == 0)) {
          if (document != null) {
            document.close();
            writer.close();
            list.add(new ByteArrayInputStream(outputStream.toByteArray()));
          }
          document = new Document(reader.getPageSizeWithRotation(1));
          outputStream = new ByteArrayOutputStream();
          writer = new PdfCopy(document, outputStream);
        }
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

      if (document != null) {
        document.close();
        writer.close();
        list.add(new ByteArrayInputStream(outputStream.toByteArray()));
      }

      reader.close();

      return new DocumentOutput(Collections.toArray(list, InputStream.class));
    } 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.SplitProcessor

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.