Free Support Forum - aspose.com

Aspose.Page image formats are not supported, even though declared

When trying to save to formats other than PNG (declares support for BMP & JPEG), the resulting file is still always PNG. Even though the support for other formats is declared in API (https://apireference.aspose.com/page/java/com.aspose.eps.device/ImageDevice#ImageDevice-java.awt.Dimension-com.aspose.eps.ImageFormat-) and examples (https://github.com/aspose-page/Aspose.Page-for-Java/blob/master/Examples/src/main/java/com/aspose/page/conversion/PostScriptToImage.java#L37).

@astafev

You need to change the imageFormat and specify image format in ImageDevice as well. Make sure both image formats in the code snippet are same. If you still face any issue, please share sample PS/EPS file with us. We will test the scenario in our environment and address it accordingly.

Any ps files will do. For example input.ps from https://github.com/aspose-page/Aspose.Page-for-Java/tree/master/Examples/src/main/resources.

Also didn’t get your comment about changing imageFormat in few places. There’s a piece of commented code in the official examples I gave a link to with ImageFormat in ImageDevice. Don’t see any other place where I can specify imageFormat, could you please clarify?

@astafev

Please check following complete code snippet to generate a BMP file from PS using Aspose.Page for Java:

try {
            com.aspose.eps.ImageFormat imageFormat = ImageFormat.BMP;
            FileInputStream psStream = new FileInputStream(dataDir + "input.ps");
            com.aspose.eps.PsDocument document = new com.aspose.eps.PsDocument(psStream);
            boolean suppressErrors = true;
            com.aspose.eps.device.ImageSaveOptions options = new com.aspose.eps.device.ImageSaveOptions();
            com.aspose.eps.device.ImageDevice device = new com.aspose.eps.device.ImageDevice(new java.awt.Dimension(595, 842), ImageFormat.BMP);

            try {
                document.save(device, options);
            } finally {
                psStream.close();
            }

            byte[][] imagesBytes = device.getImagesBytes();

            int i = 0;

            for (byte[] imageBytes : imagesBytes) {
                String imagePath = dataDir + "PSToImage" + i + "." + imageFormat.toString().toLowerCase();
                FileOutputStream fs = new FileOutputStream(imagePath);

                try {
                    fs.write(imageBytes, 0, imageBytes.length);
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                } finally {
                    fs.close();
                }
                i++;
            }

            //Review errors
            if (suppressErrors) {
                for (Exception ex : options.getExceptions()) {
                    System.out.println(ex.getMessage());
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

@asad.ali yep. I was wrong, only JPEG format is not working. If you change the imageFormat to ImageFormat.JPEG, it will generate a png file.

@astafev

We used following code snippet to generate a JPEG image and obtained attached image file.

try {
            com.aspose.eps.ImageFormat imageFormat = ImageFormat.JPEG;
            FileInputStream psStream = new FileInputStream(dataDir + "input.ps");
            com.aspose.eps.PsDocument document = new com.aspose.eps.PsDocument(psStream);
            boolean suppressErrors = true;
            com.aspose.eps.device.ImageSaveOptions options = new com.aspose.eps.device.ImageSaveOptions();
            com.aspose.eps.device.ImageDevice device = new com.aspose.eps.device.ImageDevice(new java.awt.Dimension(595, 842), ImageFormat.JPEG);

            try {
                document.save(device, options);
            } finally {
                psStream.close();
            }

            byte[][] imagesBytes = device.getImagesBytes();

            int i = 0;

            for (byte[] imageBytes : imagesBytes) {
                String imagePath = dataDir + "PSToImage" + i + "." + imageFormat.toString().toLowerCase();
                FileOutputStream fs = new FileOutputStream(imagePath);

                try {
                    fs.write(imageBytes, 0, imageBytes.length);
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                } finally {
                    fs.close();
                }
                i++;
            }

            //Review errors
            if (suppressErrors) {
                for (Exception ex : options.getExceptions()) {
                    System.out.println(ex.getMessage());
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

PSToImage0.jpeg (51.3 KB)