0

I am converting pdf files to tif format. For doing this I am using pdfbox-tools
of org.apache.pdfbox having version 2.0.3. But it is requiring to modify the in-built class TIFFUtil.java and MetaUtil.java to public. Do we have any other way by which instead of touching the classes in the jar this could be done ?

static void saveAsMultipageTIFF1(ArrayList<BufferedImage> bimTab, String filename, int dpi) throws IOException {
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tiff");
    ImageWriter imageWriter = writers.next();

    ImageOutputStream ios = ImageIO.createImageOutputStream(new File(filename));
    imageWriter.setOutput(ios);
    imageWriter.prepareWriteSequence(null);
    for (BufferedImage image : bimTab) {
        ImageWriteParam param = imageWriter.getDefaultWriteParam();
        IIOMetadata metadata = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        TIFFUtil.setCompressionType(param, image);
        TIFFUtil.updateMetadata(metadata, image, dpi);
        imageWriter.writeToSequence(new IIOImage(image, null, metadata), param);
    }
    imageWriter.endWriteSequence();
    imageWriter.dispose();
    ios.flush();
    ios.close();
}
  • 1
    Current version is 2.0.6. What prevents you from copying the source code of the two classes and modify them for your needs? – Tilman Hausherr Jul 04 '17 at 17:17
  • yes we can use 2.0.6. But I wanted to know whether we have any other way instead of explicitly using the classes from the jar ? – Deepa Nagaliker Jul 05 '17 at 07:40
  • 1
    No, unless you copy the source of the jar. This is freely available. Btw that code in your question looked kindof familiar to me... ah yes, it comes from here: https://stackoverflow.com/a/31974376/535646 – Tilman Hausherr Jul 05 '17 at 08:12

1 Answers1

0

You can use the below to convert pdf to tiff files.

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 PDFToTIFF {
private static final String FILEPATH = "<Your PDF File path>/yourPDF.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 = 3.5f;
      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("Image_" + i + ".tif");
            ImageIO.write(rendImage, "tiff", file);
         } catch (IOException e) {
            e.printStackTrace();
         }
         image.flush();
      }
      document.dispose();
}
}

If you want to set compression and change endianness , then you can one of the ImageIO extended plugin (TwelveMonkeys ImageIO) to do that.

Vinoth
  • 63
  • 4
  • 23