- Code: Select all
LicenseProvider.setLicenseKey("<License_Key>");
PdfCompressor compressor = new PdfCompressor("<Input_File_Path>");
TextCompressionOptions textCompression = compressor.getOptions().getTextCompressionOptions();
textCompression.setCompressFonts(true);
ImageCompressionOptions imageCompression = compressor.getOptions().getImageCompressionOptions();
imageCompression.setImageQuality(ImageQuality.Low);
compressor.compressToFile("<Output_Path>");
When I run it, I get this error for certain PDFs (It might have to do with PDFs having images). I have attached one such PDF that was causing this. It's working fine for most other PDFs.
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec
at com.spire.pdf.packages.sprkzo.spr┨▁(Unknown Source)
at com.spire.pdf.packages.sprkzo.spr▊▁(Unknown Source)
at com.spire.pdf.packages.spryxo.spr㈮⃞▁(Unknown Source)
at com.spire.pdf.packages.sprduo.spr┍╿(Unknown Source)
at com.spire.pdf.packages.sprszo.spr╸▁(Unknown Source)
at com.spire.pdf.packages.sprszo.spr⅔▁(Unknown Source)
at com.spire.pdf.conversion.compression.PdfCompressor.compressToFile(Unknown Source)
at com.search.snippets.SnippetsApplication.main(SnippetsApplication.java:79)
Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 8 more
I haven't been able to handle this with exception handling as well. It still fails and halts the entire program (which is basically just this code looped over various PDFs).
Some details which you might need -
Windows 10 Enterprise - 64-bit (Region - US)
SpirePDF for Java Version - 10.7.3
Running it on Java 8 with SpringBoot (JDK 1.8.0_402).
We did notice that this code was working fine on JDK version 1.8.0_261. So this just might be an issue related to the JDK used.
For our use case we would be running the code on a Databricks Compute (which has some version of Java 8 JDK) and we can't modify the exact JDK version there. So we require the code to be able to run on different minor versions of Java 8.
Please let me know if you need anything else.
Thanks for your support.
-Hrusikesh