ceakki created PDFBOX-3566:
------------------------------
Summary: ClassCastException in JPEGFactory.createFromImage
Key: PDFBOX-3566
URL: https://issues.apache.org/jira/browse/PDFBOX-3566
Project: PDFBox
Issue Type: Bug
Affects Versions: 2.0.3
Environment: Linux OS, Java 1.8, Oracle Java Advanced Imaging (JAI)
installed.
Reporter: ceakki
I was trying to save all the pages from a PDF as images and adding them to a
newly created PDF.
{code}
PDDocument newDoc = new PDDocument();
PDFRenderer pdfRenderer = new PDFRenderer(oldDoc);
floag width = ...
float height = ...
page.setMediaBox(new PDRectangle(width, height));
newDoc.addPage(page);
PDPageContentStream contents = new PDPageContentStream(newDoc, page);
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 200,
ImageType.RGB);
PDImageXObject imageXObject = JPEGFactory.createFromImage(newDoc,
bufferedImage, 0.75);
contents.drawImage(imageXObject, 0, 0, width, height);
contents.close();
...
{code}
Sometimes it's working just fine, sometimes it's trowing a ClassCastException
in JPEGFactory.createFromImage:
{noformat}
java.lang.ClassCastException:
com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriteParam cannot be cast
to javax.imageio.plugins.jpeg.JPEGImageWriteParam
at
org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.encodeImageToJPEGStream(JPEGFactory.java:244)
at
org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createJPEG(JPEGFactory.java:212)
at
org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createFromImage(JPEGFactory.java:175)
at
org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createFromImage(JPEGFactory.java:160)
at
com.mimecast.ttpservice.app.jpati.ConvertPDF.saveDoc(ConvertPDF.java:1390)
{noformat}
This is happening in the following code of above mentioned class:
{code}
imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
...
JPEGImageWriteParam jpegParam =
(JPEGImageWriteParam)imageWriter.getDefaultWriteParam();
{code}
In my case I can get more than one ImageWritter when calling
*ImageIO.getImageWritersBySuffix("jpeg")* as follows:
{noformat}
com.sun.imageio.plugins.jpeg.JPEGImageWriter
com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriter
{noformat}
I suspect that sometimes the *CLibJPEGImageWriter* is returned before
*JPEGImageWriter*, which produces the ClassCastException a bit later.
As I need to keep the JAI installed, is it possible to change the above code to
loop through all ImageWriters found in order to pick the one needed by PdfBox?
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]