You can read all details at: Compress, or Optimize PDF Files with Same Quality using Java
Compress PDF Size with Same Quality | Optimize Reduce Minimize or Shrink PDF Size using Java
This file contains 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(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"); |
This file contains 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
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"); |
This file contains 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
// 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); |
This file contains 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
// 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"); |
This file contains 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("Original.pdf"); | |
// Optimize for web | |
pdfDocument.optimize(); | |
// Save output document | |
pdfDocument.save("Optimized_output.pdf"); |
This file contains 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
// 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
Can you show the import pkg for Document and OptimizationOptions class