You are here: Home Management eClosing DocMagic Documentation Wiki Convertendo arquivos TIF em PDF
Views

Para converter arquivos de imagem no formato TIF em documentos PDF basta usar o iText. Adicione o arquivo iText-2.1.4.jar no seu projeto e crie uma classe (ou método em uma classe já existente) parecida com a apresentada abaixo:

/*
 * $Id: Tiff2Pdf.java 3373 2008-05-12 16:21:24Z xlv $
 *
 * 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.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
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;

/**
 * Demonstrates how the Tiff to PDF conversion works.
 *
 * @author psoares
 * @author blowagie
 */

public class Tiff2Pdf {

 /**
  * Demonstrates some TIFF functionality.
  *
  * @param args
  *            a list of tiff files to convert
  */
 public void convertTiff2Pdf(String fileName) {
  String pdf_file;
  
  pdf_file = fileName.substring(0, fileName.lastIndexOf('.') + 1) + "pdf";
  Document document = new Document();
  try {
   PdfWriter writer = PdfWriter.getInstance(document,
     new FileOutputStream(pdf_file));
   int pages = 0;
   document.open();
   PdfContentByte cb = writer.getDirectContent();
            RandomAccessFileOrArray ra = null;
            int comps = 0;
            try {
                ra = new RandomAccessFileOrArray(fileName);
                comps = TiffImage.getNumberOfPages(ra);
               
                System.out.println("Processing: " + fileName);
                for (int c = 0; c < comps; ++c) {
                    try {
                        Image img = TiffImage.getTiffImage(ra, c + 1);
                        if (img != null) {
                            System.out.println("page " + (c + 1));
                            if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) {
                                img.scaleToFit(500, 700);
                            }
                            img.setAbsolutePosition(20, 20);
                            document.add(new Paragraph(fileName + " - page " + (c + 1)));
                            cb.addImage(img);
                            document.newPage();
                            ++pages;
                        }
                    }
                    catch (Throwable e) {
                        System.out.println("Exception " + fileName + " page " + (c + 1) + " " + e.getMessage());
                    }
    }
    ra.close();
    document.close();
            }
            catch (Throwable e) {
                System.out.println("Exception in " + fileName + " " + e.getMessage());
            }
  } catch (Throwable e) {
   e.printStackTrace();
  }
 }   
}



powered by Plone