
PDF文書の4ページを新しいPDF文書の1ページに割り付け(PDFの面付け面付け:4in1)します。
新しいA4版サイズのPDF文書を用意し、各ページの上に、その高さと幅を半分にした配置矩形を上下左右に4つ設定します。元のPDF文書を1ページから順にコピーして、新しいPDF文書の配置矩形に順番に貼り付けます。
新しいPDF文書では1ページの上に元のPDF文書が4ページずつ割り付けられます。これは2.3.1 PDFのページを貼り付けの応用例です。
package cookbook;
import jp.co.antenna.ptl.*;
public class AllocatePages {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 2)
{
System.out.println("usage: java AllocatePages in-pdf-file out-pdf-file");
return;
}
float width = 210.0f; // A4横長
float height = 297.0f; // A4縦長
PtlRect rect[] = null;
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument();
PtlPDFDocument docNew = new PtlPDFDocument())
{
// PDFファイルをロード
doc.load(inputFile);
try (PtlPages pagesNew = docNew.getPages(); //新規PDFページコンテナの取得
PtlPages pages = doc.getPages(); //入力PDFページコンテナの取得
PtlRect rectA4V = new PtlRect(0.0f, 0.0f, width, height))// A4縦用紙
{
rect = new PtlRect[] {new PtlRect(0.0f, height/2, width/2, height), // 上左
new PtlRect(width/2, height/2, width, height), // 上右
new PtlRect(0.0f, 0.0f, width/2, height/2), // 下左
new PtlRect(width/2, 0.0f, width, height/2)}; // 下右
int numPages = doc.getPageCount();
int pageNumber = 0;
int i = 0;
while (i<numPages) {
try (PtlPage pageTemp = new PtlPage()) // 割り付けを行うA4縦ページ
{
// 割り付けを行うA4縦ページの追加
pagesNew.append(pageTemp, PtlPages.OPTION_NONE);
// 追加したページの取得
try (PtlPage pageNew = pagesNew.get(pageNumber))
{
pageNew.setViewBox(rectA4V);
// 割り付けを行うA4縦ページのコンテント取得
try (PtlContent content = pageNew.getContent())
{
// 4ページを1ページに割り付ける
for(int j=0; j<4 && i<numPages; ++j,++i) {
// 割り付けするページ
try (PtlPage page = pages.get(i))
{
// ページ割り付け
content.drawForm(rect[j], PtlContent.ALIGN.ALIGN_CENTER, page);
}
}
}
}
++pageNumber;
}
}
}
finally {
if ( rect != null )
{
for(int i=0; i<4; ++i) {
rect[i].close();
}
}
}
// ファイルに保存します。
docNew.save(outputFile);
}
...【GatPageCount.javaと同じ処理のため省略
・PtlException, Exception, Error を catchするエラー処理
・finally文で"--完了--"と表示する処理】...
}
}
AllocatePages.java
C:\samples>java cookbook.AllocatePages usage: java AllocatePages in-pdf-file out-pdf-file C:\samples>java cookbook.AllocatePages Sample1.pdf Sample1-allocated.pdf -- 完了 --
入力PDF(Sample1.pdf)はA4サイズです。これを出力PDFでは4ページ毎にA4の1ページに配置しています。
