Convert from Itext PDF byte array to multipage TIFF file

Jordi Castilla picture Jordi Castilla · Jan 5, 2015 · Viewed 7.7k times · Source

I have a pdf file (obtained from a byte[] generated by iText) I need to send to a signature hardware.

Due some incompatibility with the java printer driver I can't send the PDF directly, so i need to convert it to images before. I've succeed converting each PDF page to a jpg file, but customer does not like solution cause signatures are not in all the document, only in individual pages.

As I've not found any free library, I decided to make it in four steps:

STEP1: generate PDF with itext and persist it.

FileOutputStream fos = new FileOutputStream("tempFile.pdf");
fos.write(myByteArray);
fos.close();
fos.flush();

STEP 2: convert from PDF multipaged to List<java.awt.Image>

List<Image> images = null;

Ghostscript.getInstance(); // create gs instance

PDFDocument lDocument = new PDFDocument();
lDocument.load(new File("tempFile.pdf"));

SimpleRenderer renderer = new SimpleRenderer();

renderer.setResolution(300);

try 
{
    images = renderer.render(lDocument);
} 
catch (RendererException | DocumentException e) 
{
    e.printStackTrace();
}

Step 3: Now I iterate over List<java.awt.Image> to convert to an individual TIFF's.

    int filename = 1;

    TIFFEncodeParam params = new TIFFEncodeParam();

    Iterator<Image> imageIterator = images.iterator();

    while (imageIterator.hasNext()) {
        BufferedImage  image = (BufferedImage) imageIterator.next();

        FileOutputStream os = new FileOutputStream(/*outputDir + */ filename + ".tif");

        JAI.create("encode", image , os, "TIFF", params);

        filename ++;
    }

STEP 4: create multipaged TIFF from various individual TIFF files

BufferedImage image[] = new BufferedImage[paginas];
    for (int i = 0; i < paginas; i++) {
        SeekableStream ss = new FileSeekableStream((i + 1) + ".tif");
        ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);
        PlanarImage pi = new NullOpImage(decoder.decodeAsRenderedImage(0),null,null,OpImage.OP_IO_BOUND);
        image[i] = pi.getAsBufferedImage();
        ss.close();
    }

    TIFFEncodeParam params = new TIFFEncodeParam();
    params.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
    OutputStream out = new FileOutputStream(nombre +".tif");
    ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
    List <BufferedImage>list = new ArrayList<BufferedImage>(image.length);

    for (int i = 1; i < image.length; i++) {
        list.add(image[i]);
    }

    params.setExtraImages(list.iterator());
    encoder.encode(image[0]);
    out.close();

    System.out.println("Done.");

DONE. Hope that helps for someone else with same problem.

Answer

Kiki picture Kiki · Jan 5, 2015

I had same issue a while ago. I got lot of help from here: Multiple page tif

Allso check: JAI (Java Advance Image)

Here is the conde snippet to convert pdf pages to png images (using org.apache.pdfbox library):

    PDDocument document = null;
    document = PDDocument.load(pdf1);

    int pageNum = document.getNumberOfPages();

    PDFImageWriter writer = new PDFImageWriter();
    String filename = pdf1.getPath() + "-";
    filename = filename.replace(".pdf", "");
    writer.writeImage(document, "png", "", 1, Integer.MAX_VALUE, filename);

    document.close();

And after that i converted each PNG image to TIFF and then from multiple TIFF images to single multi paged TIFF.