Article updated on

Java de Pdf a Tiff Ejemplo

Para pasar archivos PDF a imágenes TIFF, se puede usar llamando a la aplicación GhostScript  desde java por ejemplo con el comando gs -o output.tiff -sDEVICE=tiffg4 -r600 mydpf.pdf pero prefería una solución completamente Java.

1 - Descarga las Liberías Necesarias.

  1. ICEPdf  una librería para trabajar con PDF desde java que puede pasar los PDF a imágenes. El formato TIFF no funciona por si sólo con esta librería (por lo menos hasta la versión 5.0.5).
  2. Java Advanced Imaging package (jai_core.jar y jai_imageio.jar) Jai 1.1.3 y Jai-imageio 1.1

Descargar todas las liberías del ejemplo aquí

Ejemplo de archivo TIFF aquí

 

2 - Comprueba que puedes Ver Archivos TIFF .

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class LoadAndShow extends JPanel {    
    private static final long serialVersionUID = 1342432423424L;
    private static final String PATH = "/change/to/your/path/test.tif";
    private BufferedImage image;
    private Dimension size = new Dimension();
    public LoadAndShow(BufferedImage image) {
        this.image = image;
        size.setSize(image.getWidth(), image.getHeight());
    }
    protected void paintComponent(Graphics g) {
        int x = (getWidth() - size.width) / 2;
        int y = (getHeight() - size.height) / 2;
        g.drawImage(image, x, y, this);
    }
    public Dimension getPreferredSize() {
        return size;
    }
    public static void main(String[] args) throws IOException {
        BufferedImage image = ImageIO.read(new File(PATH));
        LoadAndShow test = new LoadAndShow(image);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(test));
        f.setSize(400, 400);
        f.setLocation(200, 200);
        f.setVisible(true);
    }
}

 

3 - Convierte tus PDF a TIFFs

import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import org.icepdf.core.exceptions.*;
import org.icepdf.core.pobjects.*;
import org.icepdf.core.util.GraphicsRenderingHints;
public class TestPDF {
    private static final String FILEPATH = "/change/to/your/path/mypdf.pdf";
    public static void main(String[] args) {
          Document document = new Document();
          try {
             document.setFile(FILEPATH);
          } catch (PDFException ex) {
             System.out.println("Error parsing PDF document " + ex);
          } catch (PDFSecurityException ex) {
             System.out.println("Error encryption not supported " + ex);
          } catch (FileNotFoundException ex) {
             System.out.println("Error file not found " + ex);
          } catch (IOException ex) {
             System.out.println("Error IOException " + ex);
          }
          float scale = 1.0f;
          float rotation = 0f;
          for (int i = 0; i < document.getNumberOfPages(); i++) {
             BufferedImage image = (BufferedImage) document.getPageImage(
                 i, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX, rotation, scale);
             RenderedImage rendImage = image;
             try {
                System.out.println(" capturing page " + i);
                File file = new File("imageCapture1_" + i + ".tif");
                ImageIO.write(rendImage, "tiff", file);
             } catch (IOException e) {
                e.printStackTrace();
             }
             image.flush();
          }
          document.dispose();
    }
}

 

Tiff para Fax (ICEPDF Apache Example)

* Copyright 2006-2012 ICEsoft Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.pobjects.PDimension;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.util.GraphicsRenderingHints;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.IIOImage;
import javax.imageio.ImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.DataBuffer;
import java.awt.*;
import java.io.*;
import java.util.Iterator;
/**
* The <code>MultiPageCapture</code> class is an example of how to save page
* captures to disk. A PDF, specified at the command line, is opened, and
* every page in the document is captured as an image and saved into one
* multi-page group 4 fax TIFF graphics file.
*
* @since 4.0
*/
public class MultiPageCapture {
    public static final double FAX_RESOLUTION = 200.0;
    public static final double PRINTER_RESOLUTION = 300.0;
    // This compression type may be wpecific to JAI ImageIO Tools
    public static final String COMPRESSION_TYPE_GROUP4FAX = "CCITT T.6";
    public static void main(String[] args) {
        // Verify that ImageIO can output TIFF
        Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("tiff");
        if (!iterator.hasNext()) {
            System.out.println(
                "ImageIO missing required plug-in to write TIFF files. " +
                "You can download the JAI ImageIO Tools from: " +
                "https://jai-imageio.dev.java.net/");
            return;
        }
        boolean foundCompressionType = false;
        for(String type : iterator.next().getDefaultWriteParam().getCompressionTypes()) {
            if (COMPRESSION_TYPE_GROUP4FAX.equals(type)) {
                foundCompressionType = true;
                break;
            }
        }
        if (!foundCompressionType) {
            System.out.println(
                "TIFF ImageIO plug-in does not support Group 4 Fax " +
                "compression type ("+COMPRESSION_TYPE_GROUP4FAX+")");
            return;
        }
        // Get a file from the command line to open
        String filePath = args[0];
        // open the url
        Document document = new Document();
        try {
            document.setFile(filePath);
        } catch (PDFException ex) {
            System.out.println("Error parsing PDF document " + ex);
        } catch (PDFSecurityException ex) {
            System.out.println("Error encryption not supported " + ex);
        } catch (FileNotFoundException ex) {
            System.out.println("Error file not found " + ex);
        } catch (IOException ex) {
            System.out.println("Error handling PDF document " + ex);
        }
        try {
            // save page caputres to file.
            File file = new File("imageCapture.tif");
            ImageOutputStream ios = ImageIO.createImageOutputStream(file);
            ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
            writer.setOutput(ios);
            // Paint each pages content to an image and write the image to file
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                final double targetDPI = PRINTER_RESOLUTION;
                float scale = 1.0f;
                float rotation = 0f;
                // Given no initial zooming, calculate our natural DPI when
                // printed to standard US Letter paper
                PDimension size = document.getPageDimension(i, rotation, scale);
                double dpi = Math.sqrt((size.getWidth()*size.getWidth()) +
                                       (size.getHeight()*size.getHeight()) ) /
                             Math.sqrt((8.5*8.5)+(11*11));
                // Calculate scale required to achieve at least our target DPI
                if (dpi < (targetDPI-0.1)) {
                    scale = (float) (targetDPI / dpi);
                    size = document.getPageDimension(i, rotation, scale);
                }
                int pageWidth = (int) size.getWidth();
                int pageHeight = (int) size.getHeight();
                int[] cmap = new int[] { 0xFF000000, 0xFFFFFFFF };
                IndexColorModel cm = new IndexColorModel(
                    1, cmap.length, cmap, 0, false, Transparency.BITMASK,
                    DataBuffer.TYPE_BYTE);
                BufferedImage image = new BufferedImage(
                    pageWidth, pageHeight, BufferedImage.TYPE_BYTE_BINARY, cm);
                Graphics g = image.createGraphics();
                document.paintPage(
                    i, g, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX,
                    rotation, scale);
                g.dispose();              
                // capture the page image to file
                IIOImage img = new IIOImage(image, null, null);
                ImageWriteParam param = writer.getDefaultWriteParam();
                param.setCompressionMode(param.MODE_EXPLICIT);
                param.setCompressionType(COMPRESSION_TYPE_GROUP4FAX);
                if (i == 0) {
                    writer.write(null, img, param);
                }
                else {
                    writer.writeInsert(-1, img, param);
                }
                image.flush();
            }            
            ios.flush();
            ios.close();
            writer.dispose();
        }
        catch(IOException e) {
            System.out.println("Error saving file " + e);
            e.printStackTrace();
        }        
        // clean up resources
        document.dispose();
    }
}

 

4 - TIFFs file to PDF (IText)

import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.io.FileChannelRandomAccessSource;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;
public class Test1 {
    public static void main(String[] args) throws Exception {
        RandomAccessFile aFile = new RandomAccessFile("/myfolder/origin.tif", "r");
        FileChannel inChannel = aFile.getChannel();
        FileChannelRandomAccessSource fcra =  new FileChannelRandomAccessSource(inChannel);
        Document document = new Document();
        PdfWriter.getInstance(document,  new FileOutputStream("/myfolder/destination.pdf"));
        document.open();              
        RandomAccessFileOrArray rafa = new RandomAccessFileOrArray(fcra);
        int pages = TiffImage.getNumberOfPages(rafa);
        Image image;
        for (int i = 1; i <= pages; i++) {            
            image = TiffImage.getTiffImage(rafa, i);
            Rectangle pageSize = new Rectangle(image.getWidth(), image.getHeight());
            document.setPageSize(pageSize);
            document.newPage();
            document.add(image);
        }
        document.close();
        aFile.close();            
    }
}

Notas

  • Puede que necesites usar ImageIO.scanForPlugins si no te reconoce el plug-in, normalmente contenedores web o servidores de aplicaciones web.
  • Puede que te lleve bastante tiempo mostrar un TIFF si es muy pesado desde Java.
  • Puede ocurrir un java.lang.NullPointerException si las librerias no están classpath.
  • Puedes convertir a otros muchos formatos sin JAI como por ejemplo JPEG o PNG cambia la línea del ImageIO  ej. ImageIO.write(rendImage, "png", file);