Skip to content

Instantly share code, notes, and snippets.

@farhan-raza
Created October 14, 2020 22:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save farhan-raza/6b2f5c95c9bbd2dcb035cc3176ecbd2b to your computer and use it in GitHub Desktop.
Save farhan-raza/6b2f5c95c9bbd2dcb035cc3176ecbd2b to your computer and use it in GitHub Desktop.
Compress PDF Size with Same Quality | Optimize Reduce Minimize or Shrink PDF Size using Java
// Open document
Document pdfDocument = new Document(dataDir + "OptimizeDocument.pdf");
// Iterate through each page and annotation
for (Page page : pdfDocument.getPages())
{
for (Annotation annotation : page.getAnnotations())
{
// Either flatten the annotation
annotation.flatten();
// OR delete the annotation
// page.getAnnotations().delete(annotation);
}
}
// Save optimized PDF document
pdfDocument.save(dataDir + "OptimizeDocument_out.pdf");
Document doc = new Document(dataDir + "Test.pdf");
OptimizationOptions opt = new OptimizationOptions();
// Either
// Unembed all fonts in PDF
opt.setUnembedFonts(true);
//OR
// only keep embedded fonts for used characters
opt.setSubsetFonts(true);
// link duplicate streams
opt.setLinkDuplcateStreams(false);
// Remove unused streams
opt.setRemoveUnusedStreams(false);
// Remove unused objects
opt.setRemoveUnusedObjects(false);
doc.optimizeResources(opt);
// Save the updated file
doc.save(dataDir + "compressingPDF.pdf");
// Load source PDF form
Document doc = new Document(dataDir + "input.pdf");
// Flatten Form fields
if (doc.getForm().getFields().length > 0)
{
for (Field item : doc.getForm().getFields())
{
item.flatten();
}
}
dataDir = dataDir + "FlattenForms_out.pdf";
// Save the updated document
doc.save(dataDir);
// Load input PDF document
Document document = new Document("input.pdf");
// Initialize RgbToDeviceGrayConversionStrategy instance
RgbToDeviceGrayConversionStrategy strategy = new RgbToDeviceGrayConversionStrategy();
for (int idxPage = 1; idxPage <= document.getPages().size(); idxPage++) {
Page page = document.getPages().get_Item(idxPage);
// Convert color space of each page to Greyscale
strategy.convert(page);
}
// Save output PDF document
document.save("output.pdf");
// Open document
Document pdfDocument = new Document("Original.pdf");
// Optimize for web
pdfDocument.optimize();
// Save output document
pdfDocument.save("Optimized_output.pdf");
// Load input document
Document doc = new Document(dataDir + "Test.pdf");
// Initialize OptimizationOptions object
OptimizationOptions opt = new OptimizationOptions();
// Enable image compression
// Set the quality and resolution of images in PDF file
opt.getImageCompressionOptions().setCompressImages(true);
opt.getImageCompressionOptions().setImageQuality(10);
opt.getImageCompressionOptions().setMaxResolution(150);
opt.getImageCompressionOptions().setResizeImages(true);
doc.optimizeResources(opt);
// Save the updated file
doc.save(dataDir + "compressingPDFWithImages_out.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment