Total Pageviews

2016/07/05

[PDFBox] No ImageWriter found for 'tif' format

Problem
I try to convert PDF to TIF file via PDFBox, but as I get error message in the conversion process:
1
2
3
4
四月 28, 2016 3:06:38 下午 org.apache.pdfbox.tools.imageio.ImageIOUtil writeImage
SEVERE: No ImageWriter found for 'tif' format
四月 28, 2016 3:06:38 下午 org.apache.pdfbox.tools.imageio.ImageIOUtil writeImage
SEVERE: Supported formats: BMP bmp jpg JPG wbmp jpeg png JPEG PNG WBMP GIF gif 

My source code looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package albert.practice.file;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import lombok.extern.slf4j.Slf4j;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;

@Slf4j
public class PdfFileToTif {

    private final float dpi = 300f;

    public static void main(String[] args) {
        File pdfFile = new File("D:\\dropbox\\Getting Started.pdf");
        String destination = "D:\\dropbox\\";

        PdfFileToTif test = new PdfFileToTif();
        test.convertPdfToTif(pdfFile, destination);
    }

    public void convertPdfToTif(File pdfFile, String destination) {
        if (!isFileExisted(pdfFile)) {
            throw new RuntimeException("File not found ! (" + pdfFile.getAbsolutePath() + ")");
        }

        String pdfFileName = pdfFile.getName();

        try {
            // load PDF document
            PDDocument document = PDDocument.load(pdfFile);

            // create PDF renderer
            PDFRenderer renderer = new PDFRenderer(document);

            // go through each page of PDF, and generate TIF for each PDF page.
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                // Returns the given page as an RGB image with 300 DPI.
                BufferedImage image = renderer.renderImageWithDPI(i, dpi, ImageType.BINARY);

                // Assign the file name of TIF
                String fileName = pdfFileName + "_" + String.format("%02d", i + 1);
                log.debug("Generating  " + fileName + ".tif to " + destination);

                // Writes a buffered image to a file using the given image format.
                ImageIOUtil.writeImage(image, destination + fileName + ".tif", Math.round(dpi));
                image.flush();
            }
            log.debug("PDF to TIF conversion well done!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 判斷檔案是否存在
     * 
     * @param file
     * @return true - 檔案存在; false - 檔案不存在
     */
    private Boolean isFileExisted(File file) {
        Boolean isExisted = Boolean.FALSE;
        isExisted = (file.exists() && (!file.isDirectory()));
        return isExisted;
    }

}

pom.xml snippet looks like:
1
2
3
4
5
6
<!-- for generating tif file -->
  <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox-tools</artifactId>
      <version>2.0.0</version>
  </dependency>


This error message prints from ImageIOUtil:
https://github.com/apache/pdfbox/blob/04292e421e9531616aa2856b908fbdb05b381af7/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java#L198

According to the JavaDoc in writeImage method...
Writes a buffered image to a file using the given image format. Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the quality parameter for JPG, and dependent of bit count for TIFF (a bitonal image will be compressed with CCITT G4, a color image with LZW). Creating a TIFF image is only supported if the jai_imageio library is in the class path.
https://github.com/apache/pdfbox/blob/04292e421e9531616aa2856b908fbdb05b381af7/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java#L150-L155


How-to
This error message results from jai_imageio library is not in my class path 
Therefore, add jai-imageio-core into dependency will resolve this problem
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!-- for generating tif file -->
  <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox-tools</artifactId>
      <version>2.0.0</version>
  </dependency>
  
  <dependency>
      <groupId>com.github.jai-imageio</groupId>
      <artifactId>jai-imageio-core</artifactId>
      <version>1.3.1</version>
  </dependency>



2 comments:

Fonts Mania said...

Really great work! So if you want to convert your tiff file to other formats; then use this converter;

Simple File Converter

Fonts Mania said...

Because that simple converter can support several formats like these;

Tiff to Tif

Aiff to Wav

Odp to Ppt

Ods to Pdf

Vob to Mp4