
最適化の対象フィルター最適化を行う画像の対象とするフィルターを指定します。
PDFの内部で画像はフィルター処理された上で保管されています。最適化する画像の対象とするフィルターのそれぞれについて対象にする、しないを指定して2.2.1 カラー画像最適化オプションの取得・指定と同様にして最適化します。
PtlParamOptimizeImage.APIsetValidFilter(int validFilter): 最適化を行う画像の対象とするFilterを設定
validFilterは次表のint型定数の論理和で指定します。
| 定数フィールド | 説明 |
|---|---|
| PtlParamOptimizeImage.FILTER_NONE | フィルターなし |
| PtlParamOptimizeImage.FILTER_ASCII85Decode | ASCII85Decode |
| PtlParamOptimizeImage.FILTER_ASCIIHexDecode | ASCIIHexDecode |
| PtlParamOptimizeImage.FILTER_CCITTFaxDecode | CCITTFaxDecode |
| PtlParamOptimizeImage.FILTER_DCTDecode | DCTDecode |
| PtlParamOptimizeImage.FILTER_FlateDecode | FlateDecode |
| PtlParamOptimizeImage.FILTER_JBIG2Decode | JBIG2Decode |
| PtlParamOptimizeImage.FILTER_JPXDecode | JPXDecode |
| PtlParamOptimizeImage.FILTER_LZWDecode | LZWDecode |
| PtlParamOptimizeImage.FILTER_RunLengthDecode | RunLengthDecode |
| PtlParamOptimizeImage.FILTER_ALL | 上記全フィルター |
package cookbook;
import jp.co.antenna.ptl.*;
public class OptimizeImageSetValidFilter {
// そのクラスのusageを表示する関数
private static void printUsage() {
System.out.println("usage: java OptimizeImageSetValidFilter in-pdf-file" +
" out-pdf-file");
System.out.println("ASCII85Decode ASCIIHexDecode CCITTFaxDecode DCTDecode");
System.out.println("FlateDecode JBIG2Decode JPXDecode LZWDecode RunLengthDecode");
System.out.println("各フィルター 0と1でそれぞれの有効を切替え:");
System.out.println("0:有効にしない 1:有効にする");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 11) {
printUsage();
return;
}
// コマンドライン引数の取得
boolean[] validFilter = new boolean[9];
try{
for(int i = 0; i < 9; i++){
validFilter[i] =
readBoolArgs(args[i + 2], "各フィルターの数値 は 0か1で指定してください。");
}
}
catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
printUsage(); // usageメッセージの表示
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument()) {
// PDFファイルをロードします。
doc.load(inputFile);
// 画像の最適化を行います。
optimizeDocSetValidFilter(doc, validFilter);
// ファイルに保存します。
doc.save(outputFile);
}
catch (PtlException pex) {
System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() +
"\n " + pex.getErrorMessage());
}
catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
catch (Error ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally {
System.out.println("-- 完了 --");
}
}
public static void optimizeDocSetValidFilter(PtlPDFDocument doc,
boolean[] validFilter)
throws PtlException{
try(PtlParamOptimize paramOptimize = new PtlParamOptimize();
// 画像最適化パラメーターの取得
// カラー画像最適化パラメーターの取得
PtlParamOptimizeImage paramOptimizeImage =
paramOptimize.getParamOptimizeImage();
PtlParamOptimizeImageColor paramOptimizeImageColor =
paramOptimizeImage.getParamOptimizeImageColor()) {
// 最適化する画像の対象Filterを設定
// それぞれの論理和でON・OFFを指定する。
int filterFlag = PtlParamOptimizeImage.FILTER_NONE;
if(validFilter[0]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_ASCII85Decode;
}
if(validFilter[1]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_ASCIIHexDecode;
}
if(validFilter[2]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_CCITTFaxDecode;
}
if(validFilter[3]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_DCTDecode;
}
if(validFilter[4]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_FlateDecode;
}
if(validFilter[5]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_JBIG2Decode;
}
if(validFilter[6]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_JPXDecode;
}
if(validFilter[7]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_LZWDecode;
}
if(validFilter[8]){
filterFlag = filterFlag | PtlParamOptimizeImage.FILTER_RunLengthDecode;
}
paramOptimizeImage.setValidFilter(filterFlag);
// 画像のダウンサンプリングを行う最低サンプル数を設定
paramOptimizeImage.setMinSampleSize(110);
// その他のダウンサンプリングの値を設定
paramOptimizeImageColor.setSourcePPI(150);
paramOptimizeImageColor.setTargetPPI(75);
paramOptimizeImageColor.setDownSampling(PtlParamOptimizeImageDownSampling.DOWNSAMPLING_TYPE.DOWNSAMPLING_BICUBIC);
paramOptimizeImageColor.setMinDownSamplingRate(0.65f);
paramOptimizeImageColor.setCompress(PtlParamOptimizeImageColor.COMPRESS_TYPE.COMPRESS_AUTO);
paramOptimizeImageColor.setQuality(PtlParamOptimizeImageColor.QUALITY_TYPE.QUALITY_MIDDLE);
// 最適化の実行
doc.optimize(paramOptimize);
}
}
/**
* 0または1を入力されたargsにより、trueまたはfalseを返すメソッド。
*
* @param args 与えられるコマンドライン引数。0または1でtrueまたはfalseを指定する。
* @param errorMessage argsが0か1でなかった場合に出力されるエラーメッセージを指定する。
* @return argsの数値を読み取った結果を戻す
* @throws java.lang.IllegalArgumentException argsが0か1でなかった場合に発生。
*/
public static boolean readBoolArgs(String args, String errorMessage)
throws IllegalArgumentException {
...【ExtractTextSetRect.javaと同じ処理のため省略】...
}
}
OptimizeImageSetValidFilter.java
C:\sample>java cookbook.OptimizeImageSetValidFilter usage: java OptimizeImageSetValidFilter in-pdf-file out-pdf-file ASCII85Decode ASCIIHexDecode CCITTFaxDecode DCTDecode FlateDecode JBIG2Decode JPXDecode LZWDecode RunLengthDecode 各フィルター 0と1でそれぞれの有効を切替え: 0:有効にしない 1:有効にする C:\sample>java cookbook.OptimizeImageSetValidFilter colorImg.pdf optimizeImageColor.pdf 0 0 0 0 0 1 1 0 1 -- 完了 --
ここではJBIG2Decode、JPXDecode、RunLengthDecodeを有効にしています。