
PDF文書PDF文書の分割を指定したページ位置で2つに分割します。
PDF文書とページ分割位置を入力します。
ページ分割位置は1~(総ページ数-1)とし、PDF文書の先頭から分割位置までをコピーして1つ目のPDF文書を作成し、分割位置から最後のページまでをコピーして2つ目のPDFを作成します。
元のPDF文書の文書情報(タイトル、サブタイトル、作成者、キーワード、プロデューサ、クリエータ、作成日、更新日)を複製して、分割後の文書情報として設定します。但し、タイトルに1/2、2/2を追記します。
この例では次の機能を使っています。
package cookbook;
import jp.co.antenna.ptl.*;
public class DividePdf {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 3)
{
System.out.println("usage: java DividePdf in-pdf-file out-folder end-page-of-the-first-half");
return;
}
...【GetPageCount.javaと同じ処理のため省略
・PtlParamInputを用いてPtlPDFDocument docに入力PDFをロード】...
int endOfFirstHalf = Integer.parseInt(args[2]);
if (endOfFirstHalf >= doc.getPageCount())
{
System.err.println("end-of-pages-of-the-first-half should not be bigger than end of pdf.");
return;
}
try (PtlDocProperty docproperty = doc.getDocProperty();// 文書プロパティの取得
PtlDocInfo docinfo = docproperty.getDocInfo(); // 文書情報の取得
PtlPDFDocument doc_ext = new PtlPDFDocument();
PtlDocProperty docproperty_ext = doc_ext.getDocProperty(); // 文書プロパティの取得
PtlDocInfo docinfo_ext = docproperty_ext.getDocInfo(); // 文書情報の取得
PtlPages pages_ext = doc_ext.getPages())
{
String outputfile;
int startPage = 1;
int endPage = endOfFirstHalf;
int pagesToPrint;
// タイトルをコピーし、末尾に1/2をつける
docinfo_ext.setTitle(docinfo.getTitle()+"1/2");
//タイトルについては1/2,2/2を追加する必要あり。
// 著者をコピー
docinfo_ext.setAuthor(docinfo.getAuthor());
// サブジェクトをコピー
docinfo_ext.setSubject(docinfo.getSubject());
// キーワードをコピー
docinfo_ext.setKeywords(docinfo.getKeywords());
// クリエータをコピー
docinfo_ext.setCreator(docinfo.getCreator());
// プロデューサをコピー
docinfo_ext.setProducer(docinfo.getProducer());
// 作成日付をコピー
try (PtlDate dateCreate = docinfo.getCreationDate())
{
docinfo_ext.setCreationDate(dateCreate);
}
// 更新日付をコピー
try (PtlDate dateMod = docinfo.getModDate())
{
docinfo_ext.setModDate(dateMod);
}
// ページ挿入オプション
// OPTION_COPY_OUTLINES = 0x00000004 ページ挿入時にあわせてしおりをコピーします。
// OPTION_COPY_ATTACHEDFILES = 0x00000008ページ挿入時にあわせて添付ファイルをコピーします。
int insertoption = PtlPages.OPTION_COPY_OUTLINES | PtlPages.OPTION_COPY_ATTACHEDFILES;
// 追加ページ数の算出(startPageからendPageまで。)
pagesToPrint = endPage - startPage + 1;
// ページの追加
// PtlPages.append() のパラメータfromNumは0スタートのため、startPageから1引いています。
pages_ext.append(doc, startPage - 1, pagesToPrint, insertoption);
// 出力ファイル名
outputfile = (String)args[1] + "\\" + "output_1.pdf";
try (PtlParamOutput outputFile = new PtlParamOutput(outputfile))
{
// ファイルに保存します。
doc_ext.save(outputFile);
}
//ページを一旦全消去
pages_ext.removeAll();
// タイトル末尾に2/2をつけたものに変更
docinfo_ext.setTitle(docinfo.getTitle()+"2/2");
// 後半の追加ページを再度指定
startPage = endPage + 1;
endPage = doc.getPageCount(); //ページ数を取得し終端ページを指定。
// 追加ページ数の算出(startPageからendPageまで。)
pagesToPrint = endPage - startPage + 1;
// ページの追加
// PtlPages.append() のパラメータfromNumは0スタートのため、startPageから1引いています。
pages_ext.append(doc, startPage - 1, pagesToPrint, insertoption);
// 出力ファイル名を指定
outputfile = (String)args[1] + "\\" + "output_2.pdf";
try (PtlParamOutput outputFile = new PtlParamOutput(outputfile))
{
// ファイルに保存します。
doc_ext.save(outputFile);
}
}
}
...【GatPageCount.javaと同じ処理のため省略
・PtlException, Exception, Error を catchするエラー処理
・finally文で"--完了--"と表示する処理】...
}
}
DividePdf.java
C:\samples>java cookbook.DividePdf usage: java DividePdf in-pdf-file out-folder end-page-of-the-first-half C:\samples>java cookbook.DividePdf testPages_10.pdf out 6 -- 完了 --