
文書プロパティに含まれる文書情報の設定。
PDFが持つ文書プロパティで扱う文書情報を設定できます。
設定できる情報はタイトル、サブタイトル、作成者、キーワード、変換元のドキュメントを生成した準拠製品名、PDF変換を行った準拠製品名、作成日、更新日です。
カスタムプロパティの設定については「7.2 カスタムプロパティ」で説明します。
本サンプルプログラムでは、入力PDFに各種情報を設定したPDFドキュメントを出力します。設定する情報は、タイトル、著者名、作成日付と時・分です。更新日付及び時間についてはこのプログラムを実行した日付と時間が自動で入力されます。
package cookbook;
import jp.co.antenna.ptl.*;
import java.time.LocalDateTime;
public class SetDocInfo {
// そのクラスのusageを表示する関数
private static void printUsage() {
System.out.println("usage: java SetDocInfo in-pdf-file out-pdf-file" +
" タイトル 作者 "+
" 作成年 作成月 作成日 作成時 作成分");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 9)
{
printUsage();
return;
}
String title = args[2];
String author = args[3];
int createYear = Integer.parseInt(args[4]);
int createMonth = Integer.parseInt(args[5]);
int createDay = Integer.parseInt(args[6]);
int createHour = Integer.parseInt(args[7]);
int createMinute = Integer.parseInt(args[8]);
int createSecond = 0; //分までの入力にし、秒は0にする。
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument())
{
// PDFファイルをロード
doc.load(inputFile);
try (PtlDocProperty docProperty = doc.getDocProperty(); // PDFの文書プロパティ
PtlDocInfo docinf = docProperty.getDocInfo()) { // PDFの文書情報
// タイトルを設定
docinf.setTitle(title);
// 著者を設定
docinf.setAuthor(author);
// 作成日付を設定
try (PtlDate dateCreateNew = new PtlDate(createYear,
createMonth,
createDay,
createHour,
createMinute,
createSecond)) {
docinf.setCreationDate(dateCreateNew);
}
// 更新日付を設定(更新時の時間で作成)
LocalDateTime timeNow = LocalDateTime.now();
try (PtlDate dateModNew = new PtlDate(timeNow.getYear(),
timeNow.getMonthValue(),
timeNow.getDayOfMonth(),
timeNow.getHour(),
timeNow.getMinute(),
timeNow.getSecond())) {
docinf.setModDate(dateModNew);
}
}
// ファイルに保存します
doc.save(outputFile);
}
...【AppendAnnotStampDefault.javaと同じ処理のため省略
・エラーメッセージ処理と出力】...
}
}
SetDocInfo.java
C:\samples>java cookbook.SetDocInfo usage: java SetDocInfo in-pdf-file out-pdf-file タイトル 作者 作成年 作成月 作成日 作成時 C:\samples>java cookbook.SetDocInfo blank.pdf blank-setinfo.pdf 文書情報を設定したPDF PDFCookBook 2019 12 03 17 30 -- 完了 --
次図はAdobe ReaderでPDFの文書情報を表示したところです。
