Package com.lowagie.examples.objects.images.tiff

Source Code of com.lowagie.examples.objects.images.tiff.OddEven

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code 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.
*
* itext-questions@lists.sourceforge.net
*/

package com.lowagie.examples.objects.images.tiff;

import java.io.FileOutputStream;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.LwgImage;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;

/**
* Combines 2 tiffs, one with odd, another with even pages into 1 combined PDF
* (inspired by a tiffmesh example at http://sourceforge.net/projects/tifftools/).
*
* @author blowagie
*/

public class OddEven {

  /**
   * Combines 2 tiff-files into 1 PDF (similar to tiffmesh).
   *
   * @param args
   *            [0] the file with the odd pages
   *            [1] the file with the even pages
   *            [2] the resulting file
   */
  public static void main(String[] args) {
    if (args.length < 3) {
      System.err.println("OddEven needs 3 Arguments.");
      System.out.println("Usage: com.lowagie.examples.objects.images.tiff.OddEven odd_file.tif even_file.tif combined_file.pdf");
      System.exit(1);
    }
    try {
      RandomAccessFileOrArray odd = new RandomAccessFileOrArray(args[0]);
      RandomAccessFileOrArray even = new RandomAccessFileOrArray(args[1]);
      LwgImage img = TiffImage.getTiffImage(odd, 1);
      LwgDocument document = new LwgDocument(new LwgRectangle(img.getScaledWidth(),
          img.getScaledHeight()));
      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream(args[2]));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      int count = Math.max(TiffImage.getNumberOfPages(odd), TiffImage
          .getNumberOfPages(even));
      System.out.println("Combining 2 TIFF files into 1 PDF");
      for (int c = 0; c < count; ++c) {
        try {
          LwgImage imgOdd = TiffImage.getTiffImage(odd, c + 1);
          LwgImage imgEven = TiffImage.getTiffImage(even, count - c);
          document.setPageSize(new LwgRectangle(imgOdd.getScaledWidth(),
              imgOdd.getScaledHeight()));
          document.newPage();
          imgOdd.setAbsolutePosition(0, 0);
          cb.addImage(imgOdd);
          document.setPageSize(new LwgRectangle(imgEven.getScaledWidth(),
              imgEven.getScaledHeight()));
          document.newPage();
          imgEven.setAbsolutePosition(0, 0);
          cb.addImage(imgEven);

        } catch (Throwable e) {
          System.out.println("Exception page " + (c + 1) + " "
              + e.getMessage());
        }
      }
      odd.close();
      even.close();
      document.close();
    } catch (Throwable e) {
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of com.lowagie.examples.objects.images.tiff.OddEven

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.