Created
November 14, 2023 08:23
-
-
Save conholdate-gists/73257e04fd9a7ffcfe8b2fd5cbd62982 to your computer and use it in GitHub Desktop.
Compress PDF Documents | Reduce PDF Size and Optimize PDF in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open document | |
Document pdfDocument = new Document("Compress.pdf"); | |
// Convert RGB to Grayscale PDF | |
com.aspose.pdf.RgbToDeviceGrayConversionStrategy strategy = new com.aspose.pdf.RgbToDeviceGrayConversionStrategy(); | |
for (int idxPage = 1; idxPage <= pdfDocument.getPages().size(); idxPage++) { | |
com.aspose.pdf.Page page = pdfDocument.getPages().get_Item(idxPage); | |
strategy.convert(page); | |
} | |
// Save the grayscale PDF file | |
pdfDocument.save("Compressed.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open document | |
Document pdfDocument = new Document("Compress.pdf"); | |
// Initialize OptimizationOptions | |
com.aspose.pdf.optimization.OptimizationOptions optimizationOptions = new com.aspose.pdf.optimization.OptimizationOptions(); | |
// Set CompressImages option | |
optimizationOptions.getImageCompressionOptions().setCompressImages(true); | |
// Set ImageQuality option | |
optimizationOptions.getImageCompressionOptions().setImageQuality(50); | |
// Compress PDF document using OptimizationOptions | |
pdfDocument.optimizeResources(optimizationOptions); | |
// Save updated compressed PDF document | |
pdfDocument.save("Compressed.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open document | |
Document pdfDocument = new Document("Compress.pdf"); | |
// Compress PDF document by optimizing resources | |
pdfDocument.optimizeResources(); | |
// Save output document | |
pdfDocument.save("Compressed.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment