Error on compressing pdf containg images with aspose-pdf 21.11 for java

We used Aspose - PDF 21.11for Java for PDF compression, but some pictures could not be compressed. The error message is as follows.

in.pdf.AsposePdfProcess.lambda$main$1(AsposePdfProcess.java:32)
java.lang.Thread.run(Thread.java:748)
at com.aspose.pdf.XImage.saveInternal(Unknown Source)
at com.aspose.pdf.XImage.lI(Unknown Source)
at com.aspose.pdf.InternalHelper.lI(Unknown Source)
at com.aspose.pdf.internal.l11if.lt.lI(Unknown Source)
at com.aspose.pdf.internal.l11if.lt.lI(Unknown Source)
at com.aspose.pdf.internal.l11if.lt.lI(Unknown Source)
at com.aspose.pdf.internal.l11if.lb.lI(Unknown Source)
at com.aspose.pdf.ADocument.optimizeResources(Unknown Source)
at com.aspose.pdf.Document.optimizeResources(Unknown Source)
at jin.pdf.AsposePdfProcess.pdfCompress(AsposePdfProcess.java:72)
at jin.pdf.AsposePdfProcess.lambda$main$1(AsposePdfProcess.java:32)
at java.lang.Thread.run(Thread.java:748)
Caused by: class com.aspose.pdf.internal.ms.System.lh: Argument width[-1] or height[-1] cannot be less or equal to zero
com.aspose.pdf.internal.l81n.lj.lI(Unknown Source)
com.aspose.pdf.internal.l56u.lf.lI(Unknown Source)

the sample files is as follows.http://ishare-cms-file.oss-cn-beijing.aliyuncs.com/goods/9154933.pdf

@samuelziyou

To ensure a timely and accurate response, please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Thanks for your cooperation.

openjdk version “1.8.0_212”

package jin.pdf;

import com.aspose.pdf.Annotation;
import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.optimization.ImageCompressionVersion;
import com.aspose.pdf.optimization.ImageEncoding;
import com.aspose.pdf.optimization.OptimizationOptions;
import jin.office.AsposeTotalClient;

import java.util.concurrent.atomic.AtomicInteger;

public class AsposePdfProcess2 {

public void loadLicense() {
    com.aspose.words.License aposeLic = new com.aspose.words.License();
    try {
        aposeLic.setLicense(AsposeTotalClient.class.getClassLoader()
                .getResourceAsStream("Aspose.Total.Java.lic"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public static void main(String[] args) {
    AsposePdfProcess2 pdfProcess = new AsposePdfProcess2();
    pdfProcess.loadLicense();
    String fileName = "9154933.pdf";
    pdfProcess.pdfCompress(fileName,2);
    System.out.println("end--------------------");
}

public void pdfCompress(String fileName, int level){
    try {
        System.out.println("compress"+"\t" +fileName+"\t" +level);
        long l = System.currentTimeMillis();

        String compressStr = "重度";
        int quality = 10;
        int compressionLevel = ImageCompressionVersion.Standard;
        if(level == 1){
            compressStr = "轻度";
            quality = 80;
            compressionLevel = ImageCompressionVersion.Fast;
        }else if(level == 2){
            compressStr = "中度";
            quality = 50;
            compressionLevel = ImageCompressionVersion.Mixed;
        }

        String inputFile = "/home/jin/Desktop/pdfCompress/111/" + fileName;
        String outputFile = "/home/jin/Desktop/pdfCompress/111/aspose_compress_" + level + "_" + fileName;

        Document pdfDocument = new Document(inputFile);
        pdfDocument.optimize();

        // Optimize PDF document using OptimizationOptions
        pdfDocument.optimizeResources(setOptions(quality, compressionLevel));

        FlatteningAnnotations(pdfDocument);

        pdfDocument.save(outputFile);

        System.out.println("time:" + (System.currentTimeMillis() - l)/1000 +"\t"+ fileName +"\t" + compressStr );
    }catch (Exception e){
        e.printStackTrace();
    }
}

public OptimizationOptions setOptions(Integer quality, Integer imageCompressionVersion) {
    // Initialize OptimizationOptions
    OptimizationOptions optimizationOptions = new OptimizationOptions();

    // Set CompressImages option
    optimizationOptions.getImageCompressionOptions().setCompressImages(true);

    // Set ImageQuality option
    optimizationOptions.getImageCompressionOptions().setImageQuality(quality == null ? 50 : quality);

    // Set Image Compression Version to fast
    optimizationOptions.getImageCompressionOptions().setVersion(imageCompressionVersion);// ImageCompressionVersion.Fast);

    //RemovingUnusedObjects
    optimizationOptions.setRemoveUnusedObjects(true);

    //RemovingUnusedStream
    optimizationOptions.setRemoveUnusedStreams(true);

    //LinkingDuplicateStream
    optimizationOptions.setAllowReusePageContent(true);

    //FlateDecode 压缩
    optimizationOptions.getImageCompressionOptions().setEncoding(ImageEncoding.Flate);

    return optimizationOptions;

}

public static void FlatteningAnnotations(Document pdfDocument) {
    for (Page page : pdfDocument.getPages()) {
        for (Annotation annotation : page.getAnnotations())
            annotation.flatten();
    }
}

}

@samuelziyou

We have logged this problem in our issue tracking system as PDFJAVA-41332. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.