Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created August 8, 2025 10:46
Show Gist options
  • Save aspose-com-gists/ce5eaa8e2b236d62fffece0e7bbf42e3 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/ce5eaa8e2b236d62fffece0e7bbf42e3 to your computer and use it in GitHub Desktop.
Shrink a PDF Size Online in Seconds or Build Your Own Tool
// Load the PDF document from the specified file path
var document = new Document("input.pdf");
// Create an OptimizationOptions object to define compression settings
var options = new OptimizationOptions
{
// Remove unused objects from the PDF to free up space
RemoveUnusedObjects = true,
// Remove unused streams (extra data not required for display)
RemoveUnusedStreams = true,
// Link duplicate streams so identical resources (e.g., fonts, images) are stored only once
LinkDuplicateStreams = true,
};
// Enable image compression
options.ImageCompressionOptions.CompressImages = true;
// Set image quality to 75% (balances file size and visual clarity)
options.ImageCompressionOptions.ImageQuality = 75;
// Apply the optimization settings to the PDF document
document.OptimizeResources(options);
// Save the shrinked PDF to the specified output path
document.Save("shrinked.pdf");
import com.aspose.pdf.Document;
import com.aspose.pdf.optimization.OptimizationOptions;
public class ShrinkPDF {
public static void main(String[] args) {
// Load the PDF document from the specified file path
Document document = new Document("input.pdf");
// Create an OptimizationOptions object to define compression settings
OptimizationOptions options = new OptimizationOptions();
// Remove unused objects from the PDF to free up space
options.setRemoveUnusedObjects(true);
// Remove unused streams (extra data not required for display)
options.setRemoveUnusedStreams(true);
// Link duplicate streams so identical resources (e.g., fonts, images) are stored only once
options.setLinkDuplicateStreams(true);
// Enable image compression
options.getImageCompressionOptions().setCompressImages(true);
// Set image quality to 75% (balances file size and visual clarity)
options.getImageCompressionOptions().setImageQuality(75);
// Apply the optimization settings to the PDF document
document.optimizeResources(options);
// Save the shrunk (compressed) PDF to the specified output path
document.save("shrinked.pdf");
}
}
import aspose.pdf as ap
# Load the PDF document from the specified file path
document = ap.Document("input.pdf")
# Create an OptimizationOptions object to define compression settings
options = ap.optimization.OptimizationOptions()
# Remove unused objects from the PDF to free up space
options.remove_unused_objects = True
# Remove unused streams (extra data not required for display)
options.remove_unused_streams = True
# Link duplicate streams so identical resources (e.g., fonts, images) are stored only once
options.link_duplicate_streams = True
# Enable image compression
options.image_compression_options.compress_images = True
# Set image quality to 75% (balances file size and visual clarity)
options.image_compression_options.image_quality = 75
# Apply the optimization settings to the PDF document
document.optimize_resources(options)
# Save the shrunk (compressed) PDF to the specified output path
document.save("shrinked.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment