Jordi Castilla Jordi Castilla - 1 year ago 389
Java Question

Convert from Itext PDF byte array to multipage TIFF file

I have a pdf file (obtained from a

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");

STEP 2: convert from PDF multipaged to

List<Image> images = null;

Ghostscript.getInstance(); // create gs instance

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

SimpleRenderer renderer = new SimpleRenderer();


images = renderer.render(lDocument);
catch (RendererException | DocumentException e)

Step 3: Now I iterate over
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);

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();

TIFFEncodeParam params = new TIFFEncodeParam();
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++) {



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

Answer Source

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);


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download