Aspose java pdf replace image increase the image size in x3!

Hi
when replacing an image in pdf the image size increases by ~3 times.
i checked and this happens even when replacing the image with the exact same image without any changes, like in the code below:

private void replaceImageWithItself(String inputPath){
    Document doc = new Document(inputPath);
    for (Page page: doc.getPages()) {
        XImageCollection xImageCollection = page.getResources().getImages();
        for (int i = 1; i <= xImageCollection.size(); i++) {
            byte[] bytes;
            ByteArrayOutputStream imageOutStream = new ByteArrayOutputStream();
            XImage image = xImageCollection.get_Item(i);
            bytes = imageOutStream.toByteArray();
            xImageCollection.replace(i, new ByteArrayInputStream(bytes));
        }
    }
    doc.save();
}

how can I replace or add an image without to change the quality or increase the size?

@yecht

Would you please share your sample document and sample image for our reference? We will test the scenario in our environment and address it accordingly.

qr_codes_ilpdf.pdf (144.6 KB)

File is attached.
P.S. This seems to happen for all files

Thanks!

@yecht

We tested your code snippet with 25.1 version of the API and faced below exception:

class com.aspose.pdf.internal.ms.System.lh: Invalid image stream (stream)

Are you sure this code is working in your environment with the latest version of the API?

sorry I missed one line
use this:

private void replaceImageWithItself(String inputPath){
    Document doc = new Document(inputPath);
    for (Page page: doc.getPages()) {
        XImageCollection xImageCollection = page.getResources().getImages();
        for (int i = 1; i <= xImageCollection.size(); i++) {
            byte[] bytes;
            ByteArrayOutputStream imageOutStream = new ByteArrayOutputStream();
            XImage image = xImageCollection.get_Item(i);
            image.save(imageOutStream);
            bytes = imageOutStream.toByteArray();
            xImageCollection.replace(i, new ByteArrayInputStream(bytes));
        }
    }
    doc.save(inputPath + "_out.pdf");
}

@yecht

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-44766

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.