Chandler E.
Chandler E.

Reputation: 113

Convert CCITT Group 3 1-Dimensional TIFF to PDF using iText in Java

I am experiencing an EOF Exception as follows when attempting to read tiff files using iText 5.5.10

ExceptionConverter: java.io.EOFException
at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:249)
at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:241)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:209)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:314)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:302)
at com.itextpdf.text.Image.getInstance(Image.java:428)
at com.itextpdf.text.Image.getInstance(Image.java:374)
at TiffToPdf.main(TiffToPdf.java:137)

The code I am using is:

    byte[] data = null;
    Image img = null;
    try {
        data = Files.readAllBytes(Paths.get("tiff.tif"));
        img = Image.getInstance(data, true);
    } 
    catch (Exception e) {
       e.printStackTrace();
    }

I have tried skipping the Image step and using the TiffImage class explicitly but I experience the same error.

    byte[] data = null;
    Image img = null;
    try {
        data = Files.readAllBytes(Paths.get("tiff.tif"));
        RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
        RandomAccessSource fileBytes = factory.createSource(data);
        RandomAccessFileOrArray s = new RandomAccessFileOrArray(fileBytes);
        img = TiffImage.getTiffImage(s, true, 1, true);
    } 
    catch (Exception e) {
       e.printStackTrace();
    }

I noticed that there are 2 classes within iText called TIFFFaxDecompressor and TIFFFaxDecoder but I haven't been able to find any resources online on how to use them.

Upvotes: 0

Views: 671

Answers (1)

Sasi Kathimanda
Sasi Kathimanda

Reputation: 1806

with your given tiff image, the following code does worked for me i.e., converted to pdf successfully.

    byte[] data = null;
    com.itextpdf.text.Image img = null;
    try {
        //System.out.println(Paths.get("src/main/resources/tiff.tif"));
        data = Files.readAllBytes(Paths.get("src/main/resources/file.tif"));
        RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
        RandomAccessSource fileBytes = factory.createSource(data);
        RandomAccessFileOrArray s = new RandomAccessFileOrArray(fileBytes);

        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("src/main/resources/destination.pdf"));
        document.open();
        int pages = TiffImage.getNumberOfPages(s);
        Image image;
        for (int i = 1; i <= pages; i++) {
            image = TiffImage.getTiffImage(s, i);
            Rectangle pageSize = new Rectangle(image.getWidth(),
                    image.getHeight());
            document.setPageSize(pageSize);
            document.newPage();
            document.add(image);
        }
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 1

Related Questions