We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Converting PDF to TIFF in Java using Aspose.PDF fails with ClassCastException

Sample.pdf (18.5 KB)
Aspose Team,

We use the Aspose PDF java package to convert pdf to tiff, and got the following error with some PDF files.

java.lang.ClassCastException: com.aspose.pdf.internal.imaging.fileformats.jpeg.JpegImage cannot be cast to com.aspose.pdf.internal.ms.System.l5f
at com.aspose.pdf.internal.l10p.le.lb(Unknown Source)
at com.aspose.pdf.internal.l10p.le.lI(Unknown Source)
at com.aspose.pdf.devices.TiffDevice.processInternal(Unknown Source)
at com.aspose.pdf.devices.DocumentDevice.processInternal(Unknown Source)
at com.aspose.pdf.devices.DocumentDevice.process(Unknown Source)
at TestPdf2Tiff.pdfToTiff(TestPdf2Tiff.java:59)
at TestPdf2Tiff.main(TestPdf2Tiff.java:25)

Following is the sample code that reproduces the problem and attached is a sample file. Note that we add some stamps to the pdf before converting it to tiff. So we added the code for adding stamps here. We noticed that the same error happens if the method for adding stamps (addStamps) is skipped.

The operating system is Ubuntu 18.04. Java version is 1.8. Aspose PDF java package is 20.8.

import com.aspose.pdf.;
import com.aspose.pdf.devices.
;
import com.aspose.pdf.facades.FormattedText;
import com.aspose.pdf.facades.PdfFileEditor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class TestPdf2Tiff {

public enum TiffFormat {
    Color, BlackAndWhite, Grayscale
}
public static final int DEFAULT_DPI_RESOLUTION = 300;  // dots per inch resolution

public static void main(String[] args) {
    try {
        System.out.println("Start");
        String pdfPath = "/home/ubuntu/testdir_pdf2tiff/Sample.pdf";
        String tiffPath = "/home/ubuntu/testdir_pdf2tiff/Sample.tiff";
        addStamps(pdfPath);
        pdfToTiff(pdfPath, tiffPath, TiffFormat.Color);
        System.out.println("Done");
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
}

private static void pdfToTiff(String inputFilePath, String outputFilePath, TiffFormat format) throws IOException {
    System.out.println("Convert pdf to tiff...");

    int compressingType = CompressionType.LZW;  // Default to Color
    int colorDepth = ColorDepth.Default;
    if (format == TiffFormat.BlackAndWhite) {
        compressingType = CompressionType.CCITT4; // Black and White
        colorDepth = ColorDepth.Format8bpp;
    } else if (format == TiffFormat.Grayscale) { // Grayscale
        colorDepth = ColorDepth.Format1bpp;
    }

    OutputStream imageStream = null;
    Document pdfDocument = null;
    try {
        pdfDocument = new Document(inputFilePath);
        imageStream = new FileOutputStream(outputFilePath);

        Resolution resolution = new Resolution(DEFAULT_DPI_RESOLUTION);   // abstract resolution that seems to correlate to dots per inch (DPI)

        TiffSettings tiffSettings = new TiffSettings();
        tiffSettings.setCompression(compressingType);
        tiffSettings.setDepth(colorDepth);
        tiffSettings.setSkipBlankPages(false);

        TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
        tiffDevice.process(pdfDocument, imageStream);
        pdfDocument.close();
    } finally {
        if (pdfDocument != null) {
            pdfDocument.close();
        }
        if (imageStream != null) {
            imageStream.close();
        }
    }

    System.out.println("Converting pdf to tiff done");
}

private static void addStamps(String path) throws Exception {
    System.out.println("Add stamps...");
    String ucStr = "Test";
    String llStr = "1";
    String lrStr = "YAX.PC.00000198";
    String fontFamily = "DejaVu Sans";
    Font font = FontRepository.findFont(fontFamily);;
    int fontSize = 10;

    // Stamps
    List<TextStamp> stampsToAdd = new ArrayList<>();
    // Uper Center
    stampsToAdd.add(convertToStamp(ucStr, HorizontalAlignment.Center, VerticalAlignment.Top, font, fontSize));
    // Lower Left
    stampsToAdd.add(convertToStamp(llStr, HorizontalAlignment.Left, VerticalAlignment.Bottom, font, fontSize));
    // Lower Right
    stampsToAdd.add(convertToStamp(lrStr, HorizontalAlignment.Right, VerticalAlignment.Bottom, font, fontSize));

    // Resize the page before adding the stamps
    int leftMargin = 5;
    int rightMargin = 5;
    int topMargin = 11;
    int bottomMargin = 11;
    PdfFileEditor.ContentsResizeParameters parameters = new PdfFileEditor.ContentsResizeParameters(
            PdfFileEditor.ContentsResizeValue.units(leftMargin),
            null,
            PdfFileEditor.ContentsResizeValue.units(rightMargin),
            PdfFileEditor.ContentsResizeValue.units(topMargin),
            null,
            PdfFileEditor.ContentsResizeValue.units(bottomMargin)
    );

    PdfFileEditor editor = new PdfFileEditor();
    Document document = new Document(path);
    editor.resizeContents(document, parameters);

    for (Page page : document.getPages()) {
        for(TextStamp stamp : stampsToAdd){
            page.addStamp(stamp);
        }
    }
    document.save();
    System.out.println("Adding stamps done");
}

private static TextStamp convertToStamp(String text, int horizontalAlignment, int verticalAlignment,
                                        Font font, int fontSize) {
    FormattedText formattedText = new FormattedText();
    formattedText.addNewLineText(text);
    TextStamp textStamp = new TextStamp(formattedText);
    textStamp.setWordWrap(true);
    textStamp.setHorizontalAlignment(horizontalAlignment);
    textStamp.setVerticalAlignment(verticalAlignment);
    textStamp.getTextState().setFont(font);
    textStamp.getTextState().setFontSize(fontSize);
    textStamp.setTopMargin(0);
    return textStamp;
}

}

@xyang

We tested the scenario using Aspose.PDF for Java 20.9 and were unable to notice any issue in our environment. We have attached generated output for your reference as well. Would you kindly try to use latest version of the API and in case issue still persists, please let us know.
sample.pdf.zip (182.2 KB)

I ran the test with Aspose.PDF for Java 20.9 and the result is as expected.

Thank You!

@xyang

It is good to know that your issue has been resolved. Please keep using our API and in case you face any issue, please feel free to let us know.