Not a JPEG file: starts with 0xff 0xff (Words 11.7.0)

Hello Aspose Team,
I’m getting an error when trying to convert a jpg file to pdf. The file seems to be ok, I can display it in Windows 7, edit it, save it as new… you can find it attached.
Aspose Words for Java : 11.7.0 (Aspose.Words.jdk16.jar)
JAI (from you): jai_codec-1.1.3-alpha.jar, jai_imageio-1.1.jar, jai-core-1.1.3-alpha.jar
The code looks like your example:

private String convertImageToPdf(String source, String target) throws Exception {
    Document doc = new Document();
    ImageInputStream iis = null;
    ImageReader reader = null;
    try {
        DocumentBuilder builder = new DocumentBuilder(doc);
        iis = ImageIO.createImageInputStream(new File(source));
        reader = ImageIO.getImageReaders(iis).next();
        reader.setInput(iis, false);
        int framesCount = reader.getNumImages(true);
        for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) {
            if (frameIdx != 0)
                builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
            BufferedImage image = reader.read(frameIdx);

The exception is thrown at the last line of code, BufferedImage…

javax.imageio.IIOException: Not a JPEG file: starts with 0xff 0xff
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:529)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:523)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:925)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:909)
at javax.imageio.ImageReader.read(ImageReader.java:934)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.invoke(AbstractJAXWSMethodInvoker.java:178)
at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:64)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:75)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
at java.util.concurrent.FutureTask.run(FutureTask.java:149)
at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:107)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:122)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:211)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:213)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:193)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:129)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:187)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:110)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:166)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:864)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:816)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)

Hope you can help! Thank you in advance.
Kind regards
Peter

Hi
Peter,

Thanks for your inquiry.

I have tested the scenario and have managed to reproduce the same exception on my side. By looking at the stack trace, it doesn’t seem to have anything to do with Aspose.Words for Java API. I would suggest you please ask your question in some JAI community e.g. here:
http://www.java.net/forums/javadesktop/java-desktop-technologies/jai-imageio

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi Awais,
thank you for your reply.
After looking into the JAI community link I noticed that it looks pretty “dead”. Unfortunately I think I will not receive a bug fix for this… :confused: are you looking to not use JAI components in the future? JAI bugs will affect Aspose Quality in terms of imaging files.
Kind regards
Peter

Hi Peter,

Thanks for the additional information. Yes, you’re right; most of our Java products depend upon the JAI package from Sun in order to process images. I am afraid, this dependency will not change in any upcoming version. I have logged a new issue in our bug tracking system to address this problem. The issue ID is WORDSJAVA-619. Your request has been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best Regards,

Hi Peter,
Please try another DocumentBuilder.insertImage() overload:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertImage("Path\\to\\notajpg.jpg");
doc.save("Path\\to\\out.pdf");

In this case Aspose.Words itself will choose a codec (from JAI or from ImageIO) and will tweak an options needed to successfully load this particular image.
Regards,

Hey Konstantin,
thank you very much for the code snippet.
It does not lead to the mentioned error which is very nice, unfortunately a landscape JPG gets into a PDF in portrait format, this leads that only half of the picture is visible in the PDF. While calling insertImage(…) I can pass width and height, but I don’t have those values.
Do you have any hint how to process landscape JPG’s with this code snippet without “loosing” some of the picture? Should I try to turn the PDF to landscape (preferrebly) or fit the landscape JPG into portrait PDF?
Thank you in advance!
Kind regards
Peter

Hi Peter,
For better understanding you can think that Aspose.Words is a ‘word processor for robots’. I.e. your robot (your program) can do with document content and formatting all the things that you doing inside word processor for humans (like MS Word and others).
So, it’s up to you what page type and size, margins, image scale, etc. will be better for your document. Here is a small sample with your image:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Tweak some Page settings.
PageSetup pageSetup = builder.getPageSetup();
pageSetup.setOrientation(Orientation.LANDSCAPE);
pageSetup.setLeftMargin(20);
pageSetup.setRightMargin(10);
pageSetup.setTopMargin(10);
pageSetup.setBottomMargin(10);
Shape shape = builder.insertImage("Path\\to\\notajpg.jpg");
double shapeWidth = shape.getWidth();
double shapeHeight = shape.getHeight();
// Check whether the image fits the page.
double visibleWidth = pageSetup.getPageWidth() - pageSetup.getLeftMargin() - pageSetup.getRightMargin();
double visibleHeight = pageSetup.getPageHeight() - pageSetup.getTopMargin() - pageSetup.getBottomMargin();
if (shapeWidth > visibleWidth || shapeHeight > visibleHeight)
{
    double widthScale = shapeWidth / visibleWidth;
    double heightScale = shapeHeight / visibleHeight;
    double scale = Math.max(widthScale, heightScale);
    shape.setWidth(shapeWidth / scale);
    shape.setHeight(shapeHeight / scale);
}
doc.save("Path\\to\\out.pdf");

Please check Programmers Guide https://docs.aspose.com/words/java/using-documentbuilder-to-modify-a-document/ and Aspose.Words API to find new ideas about document formatting.
Best Regards,

Hello Konstantin,
thank you very much for the code snippet. I’ve implemented it and it works like a charm, my code looks like:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup pageSetup = builder.getPageSetup();
Shape shape = builder.insertImage(source);
// is landscape?
double shapeWidth = shape.getWidth();
double shapeHeight = shape.getHeight();
if (shapeWidth > shapeHeight)
{
    pageSetup.setOrientation(Orientation.LANDSCAPE);
}
pageSetup.setLeftMargin(20);
pageSetup.setRightMargin(10);
pageSetup.setTopMargin(10);
pageSetup.setBottomMargin(10);
// check visibility and scale down if necessary
double visibleWidth = pageSetup.getPageWidth() - pageSetup.getLeftMargin() - pageSetup.getRightMargin();
double visibleHeight = pageSetup.getPageHeight() - pageSetup.getTopMargin() - pageSetup.getBottomMargin();
if (shapeWidth > visibleWidth || shapeHeight > visibleHeight)
{
    double widthScale = shapeWidth / visibleWidth;
    double heightScale = shapeHeight / visibleHeight;
    double scale = Math.max(widthScale, heightScale);
    shape.setWidth(shapeWidth / scale);
    shape.setHeight(shapeHeight / scale);
}
doc.save(target);

I set the PDF to landscape when I detect that the image has more width than height.
Have a good day!
Kind regards
Peter

Hi
Peter,

Thanks for your feedback. It is perfect that you managed to achieve what you were looking for. Please let us know any time you have any further queries.

Best Regards,

The issues you have found earlier (filed as WORDSJAVA-619) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.