import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* PDFファイルの結合処理を実行し、元ファイルに問題があるか判定します。
* サーバに悪影響を与えるファイルを事前にはじくことが目的です。
* 本ソースは、利用方法の説明のためのサンプルですのでサポート対象です。
* @version 1.0
*/
public class pdfcheck {
/**
* ypdfcomb.exeを実行して元ファイルに問題があるかどうかチェックします。
* @param infile 元ファイル
* @param outfile 出力ファイル
* @param pwd パスワード
* @throws Exception エラー
*/
public static void execute(String infile, String outfile, String pwd) throws Exception {
List<String> command = getCommandList(infile, outfile, pwd);
try {
ProcessBuilder pb = new ProcessBuilder(command);
// 標準エラーを標準出力にマージ
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = null;
InputStreamReader sr = null;
BufferedReader br = null;
try {
is = p.getInputStream();
sr = new InputStreamReader(is);
br = new BufferedReader(sr);
String line;
StringBuilder message = new StringBuilder();
while ((line = br.readLine()) != null) {
message.append(line);
}
if (p.waitFor() != 0) {
throw new Exception(message.toString());
}
}
finally {
br.close();
sr.close();
is.close();
// 本来はチェックしたPDFファイルの削除が必要
}
}
catch (IOException e) {
throw new Exception(e.getMessage());
}
}
/**
* 実行用のコマンドを返す。
* @param infile 元ファイル
* @param outfile 出力ファイル
* @param pwd パスワード
* @return コマンド
*/
private static List<String> getCommandList(String infile, String outfile,
String pwd) {
List<String> command = new ArrayList<String>();
command.add("ypdfcomb");
command.add("-se");
command.add("y");
command.add("-o");
command.add(outfile);
command.add("-temp");
command.add(".");
command.add("-i");
command.add(infile);
return command;
}
/**
* サンプル実行
*/
public static void main (String[] args) throws Exception{
pdfcheck.execute("C:/temp/in.pdf", "C:/temp/out.pdf", null);
}
}