We have a requirement to reduce the size of a PDF file in my project.
Currently, we are using the paid version 22.11 of Aspose.PDF using java However, we are unable to compress the PDF size with this current version of Aspose jar.
Could you please provide guidance on how to achieve this requirement using Aspose.PDF?
Code snippet:
Document doc = new Document(file);
OptimizationOptions optimizationOptions = new OptimizationOptions();
//document.optimize();
// document.optimizeResources();
optimizationOptions.getImageCompressionOptions().setCompressImages(true);
// Set ImageQuality option
optimizationOptions.getImageCompressionOptions().setImageQuality(10);
// Set ResizeImage option
optimizationOptions.getImageCompressionOptions().setResizeImages(true);
// Set MaxResolution option
optimizationOptions.getImageCompressionOptions().setMaxResolution(10);
doc.optimizeResources(optimizationOptions);
doc.save(“AsposePDF.pdf”);
@narayana.venkatas
To reduce the size of a PDF file using Aspose.PDF for Java, you can utilize the OptimizationOptions
class to configure various optimization settings. Based on your provided code snippet, it looks like you are on the right track. Here’s a detailed guide on how to effectively compress your PDF file:
-
Load the PDF Document: Start by loading your PDF document using the Document
class.
-
Set Optimization Options: Create an instance of OptimizationOptions
and configure it to compress images, set image quality, resize images, and set maximum resolution.
-
Optimize Resources: Use the optimizeResources
method of the Document
class to apply the optimization options.
-
Save the Optimized Document: Finally, save the optimized document to a new file.
Here’s an example code snippet that demonstrates these steps:
import com.aspose.pdf.Document;
import com.aspose.pdf.optimization.OptimizationOptions;
public class PDFCompressor {
public static void main(String[] args) {
// Load the PDF document
Document doc = new Document("input.pdf");
// Create optimization options
OptimizationOptions optimizationOptions = new OptimizationOptions();
// Enable image compression
optimizationOptions.getImageCompressionOptions().setCompressImages(true);
// Set image quality (1-100, where lower means more compression)
optimizationOptions.getImageCompressionOptions().setImageQuality(10);
// Enable image resizing
optimizationOptions.getImageCompressionOptions().setResizeImages(true);
// Set maximum resolution for images
optimizationOptions.getImageCompressionOptions().setMaxResolution(10);
// Optimize the document resources
doc.optimizeResources(optimizationOptions);
// Save the optimized document
doc.save("OptimizedOutput.pdf");
}
}
Additional Tips:
-
Remove Unused Objects: You can also set options to remove unused objects and subset fonts, which can further reduce the file size. For example:
optimizationOptions.setRemoveUnusedObjects(true);
optimizationOptions.setSubsetFonts(true);
-
Optimize for Web: If your goal is to optimize the PDF for web viewing, you can use the optimize()
method directly on the document, which applies a set of default optimizations.
By following these steps and utilizing the provided code, you should be able to effectively reduce the size of your PDF files using Aspose.PDF for Java.
If you encounter any issues or need further assistance, feel free to ask!
Sources:
[1]: OptimizePDFFileSize.java
[2]: OptimizePDFDocumentForWeb.java
Thank you for the information.
However, we have already attempted this option, and it did not significantly reduce the PDF file size. It only decreased the size by about 2-3%. According to Aspose, we should expect a reduction of around 40%.
Could you please let us know , if other option
@narayana.venkatas
Would you please share your sample document with us as well so that we can test the scenario in our environment and address it accordingly?