4

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.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Wouldn't it make more sense to sign the PDF as is and convert only for printing? – mkl Jan 05 '15 at 17:13
  • From what line do you get the exception? `ImageIO.read`? It seems that your PDFBox-exported JPEGs are either not found, or not readable. Does this happen for the first file in the loop, or at some (random?) point later? If so, try attaching a sample file that fails to be read. :-) – Harald K Jan 06 '15 at 09:30
  • @mkl the device to sign is viewed by java / system as a printer. – Jordi Castilla Jan 07 '15 at 08:15
  • @haraldK im getting the error in line `RenderedImage image = ImageIO.read(new File(file + ".jpg"));` – Jordi Castilla Jan 07 '15 at 08:15
  • 1
    @JordiCastilla *the device to sign is viewed by java / system as a printer* - hmmm, feels like a design that sounded good at the start but eventually turns out to be more a burden than a boon. Sigh, sorry. – mkl Jan 07 '15 at 08:53
  • @haraldK ... solved, I edited question to help future OP – Jordi Castilla Jan 07 '15 at 09:33
  • [Here](http://stackoverflow.com/questions/31973354/converting-pdf-to-multipage-tiff-group-4/32702373#32702373) you can find a simpler approach to convert PDF to multipage TIFF directly without the middle man. – dragon66 Jan 12 '16 at 04:20

1 Answers1

5

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.

Kiki
  • 2,243
  • 5
  • 30
  • 43
  • Thanks! That helped, now I'm able to convert from various TIFF files to one multipaged TIFF... problem is now convert from PDF to TIFF... – Jordi Castilla Jan 05 '15 at 14:25
  • I used itext to convert pdf pages to png and from png to tiff. Look at updated answer. – Kiki Jan 05 '15 at 14:39
  • conversion from PDF to JPG/PNG is not a problem, the problem is the PNG/JPG/PDF to TIFF, cause TIFF seems to need additional librarys or so... did you succeed to convert PNG to TIFF in itext/pdfbox??? – Jordi Castilla Jan 05 '15 at 15:01
  • 1
    I converted png to tiff using JAI (a free library from Sun). Here is an example: https://blog.idrsolutions.com/2009/06/saving-java-images-as-tifs-wth-jai/ – Kiki Jan 05 '15 at 15:49
  • Accepted, your answer helped enough... :D – Jordi Castilla Jan 07 '15 at 09:32
  • see more recent code here: https://stackoverflow.com/questions/23326562/convert-pdf-files-to-images-with-pdfbox – Pipo May 16 '22 at 14:42