用户对扫描件或者带有图片的PDF文件进行优化压缩时,可通过对图片dpi参数进行控制,从而决定最终文件的大小。示例代码如下:
public static void main(String[] args) throws PDFException {
// Initialize library
int error_code = Library.initialize(sn, key);
if (error_code != e_ErrSuccess) {
System.out.println("Library Initialize Error: " + error_code);
return;
}
createResultFolder(output_path);
String input_file = "XXXXX.pdf";
System.out.println("Optimized Start : Image Compression.");
try {
PDFDoc doc = new PDFDoc(input_file);
error_code = doc.load(null);
if (error_code != e_ErrSuccess) {
System.out.println("The Doc [" + input_file + " Error: " + error_code);
return;
}
PauseUtil pause = new PauseUtil();
OptimizerSettings settings = new OptimizerSettings();
ImageSettings imageSettings = new ImageSettings();
imageSettings.setQuality(1);
//可自定义控制
imageSettings.setImageDPI(100);
imageSettings.setImageDPILimit(100);
settings.setColorImageSettings(imageSettings);
settings.setOptimizerOptions(OptimizerSettings.e_OptimizerCompressImages);
Progressive progressive = Optimizer.optimize(doc, settings, pause);
int state = Progressive.e_ToBeContinued;
while (state == Progressive.e_ToBeContinued) {
state = progressive.resume();
int rate = progressive.getRateOfProgress();
System.out.println("Optimize progress percent: " + rate + "%");
}
if(state == Progressive.e_Finished)
{
doc.saveAs( "Compression12.pdf", e_SaveFlagRemoveRedundantObjects);
}
} catch (PDFException e) {
e.printStackTrace();
return;
}
System.out.println("Optimized Finish : Image Compression.");
Library.release();
}