How to Compress Images in PPT/PDF in Java without Losing the Quality?

Is there a way to compress the images in PPT/PDF without losing the quality in java? We have resized the image to make it fit in the frame. and already doing this using new width and new height.

@AnkitaCisco,
Thank you for your question.

To better understand the problem you have encountered, please share the following data and information:

  • input and output files (you can zip the files and upload them here)
  • code example that reproduces the problem with the image quality
  • OS version where the code was executed
  • JDK version in your app
  • Aspose.Slides version you used

Then we will do our best to assist you.

Any details or further information can help us or try to guide you on this topic,
Do you need to start from scratch or you have already code?

Below is the function written to add a picture to the picture placeholder by resizing it. I need help with compressing the image size. With the default image sizes the we are not able to attach the ppt to the mail. Need assistance with compressing the image and not reducing the image quality.

// Function to add images in the placeholder
    private boolean setImage(PPTFile PPTObj, IPictureFrame pic, HashMap<String, Iterator<Object>> keyMapper, int slideIdx, int timeout, ArrayList<IShape> toDelete){
        //For adding Images in Image Placeholders using Aspose API
        String key = pic.getName();

        HttpURLConnection conn = null;
        log.info("Adding image to shape " + key);
        try {
            String url = (String) keyMapper.get(key).next();
            URL imageURL = new URL(url);
            log.info("Adding image from the URL " + url);

            conn = (HttpURLConnection) imageURL.openConnection();
            if(conn != null){
                (conn).setConnectTimeout(timeout*1000);
                (conn).setRequestMethod("HEAD");
                conn.setReadTimeout(timeout*1000);
            }
            conn.getInputStream();
            long size = conn.getContentLengthLong();

            if(size != -1 && size < imageSize*1024*1024){
                IPPImage img = PPTObj.PPT.getImages().addImage(ImageIO.read(imageURL));
                float X = 0, Y = 0;
                float width_ratio = pic.getWidth()/img.getWidth();
                float height_ratio = pic.getHeight()/img.getHeight();

                float new_width = img.getWidth()*height_ratio;
                float new_height = img.getHeight()*width_ratio;
                IPictureFrame picNew;
                    X = pic.getX();
                    Y = pic.getY();
                picNew = PPTObj.PPT.getSlides().get_Item(slideIdx).getShapes().addPictureFrame(ShapeType.Rectangle, X, Y, pic.getWidth(), pic.getHeight(), img);

                if(new_width >= pic.getWidth()){
                    picNew.getPictureFormat().setCropLeft((new_width - pic.getWidth())/(2*new_width)*100);
                    picNew.getPictureFormat().setCropRight((new_width - pic.getWidth())/(2*new_width)*100);
                }else{
                    picNew.getPictureFormat().setCropTop((new_height - pic.getHeight())/(2*new_height)*100);
                    picNew.getPictureFormat().setCropBottom((new_height - pic.getHeight())/(2*new_height)*100);
                }
                picNew.setName(pic.getName());
                pic.removePlaceholder();
                toDelete.add(pic);
            }

@AnkitaCisco,
Unfortunately, Aspose.Slides does not have such a feature for image compression/optimization. You can try using Aspose.Imaging for this.