
レイヤーに使用するPDFのページを指定し、設定したレイヤーに貼り付けます。
入力PDFの指定したページに、挿入PDFの指定したページをレイヤー機能で重ねて挿入します。
それ以外の項目については以下をデフォルトとし、第2項~第6項の各項で指定するもの以外は共通の指定をしています。
| ダウンサンプリング条件 | 値 |
|---|---|
| レイヤーの名前 | Layer1 |
| レイヤーのZオーダー | 前面 |
| レイヤーの表示/非表示 | 表示 |
| レイヤー描画の矩形 | ページ全体 |
| レイヤーの配置指定 | 中央 |
PtlPage.APIgetContent(): ページコンテントを取得
PtlContent.APIdrawLayer(PtlRect rectMM, PtlContent.ALIGN align, ParamDrawLayer paramDrawLayer): レイヤーを描画
PtlParamDrawLayer: レイヤーの描画に使うパラメータクラス
PtlParamDrawLayer.APIsetPage(PtlPage page): レイヤーに使用するPDF文書ページを設定
package cookbook;
import jp.co.antenna.ptl.*;
public class DrawLayerSetPage {
// そのクラスのusageを表示する関数
private static void printUsage() {
System.out.println("usage: java DrawLayerSetPage in-pdf-file out-pdf-file" +
" page-num insert-pdf-file page-num-to-insert");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 5) {
printUsage();
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument()) {
// PDFファイルをロードします。
doc.load(inputFile);
// コマンドライン引数の判定
int pageToAdd = Integer.parseInt(args[2]);
int numPages = doc.getPageCount();
System.out.println("ページ数:" + numPages);
if((numPages < 0)||(numPages < pageToAdd)) {
System.err.println("page-numは入力PDFの全ページ数よりも小さい正の値を" +
"指定してください。");
return;
}
String insertPDFPath = args[3];
int insertPageNum = Integer.parseInt(args[4]);
try (PtlPages pages = doc.getPages()) { // ページコンテナの取得
// ページコンテナが空かどうか
if (pages.isEmpty()) {
System.out.println("ページコンテナが空");
return;
}
// ページの取得(パラメータindexは0が先頭のため1を引く)
try (PtlPage page = pages.get(pageToAdd - 1)) {
drawLayer(page, insertPDFPath, insertPageNum); //レイヤーの描画
}
}
// ファイルに保存します。
doc.save(outputFile);
}
...【ExtractText.javaと同じ処理のため省略
・エラーメッセージ処理と出力】...
}
public static void drawLayer(PtlPage page, String insertPDFPath, int insertPageNum)
throws PtlException, Exception, Error {
try (PtlSize pageSize = page.getSize(); // ページサイズ
PtlContent content = page.getContent(); // ページコンテントの取得
PtlPDFDocument docToInsert = new PtlPDFDocument();
//挿入するPDF
PtlParamInput inputFileToInsert = new PtlParamInput(insertPDFPath);
// 描画矩形の指定(PDFいっぱいに指定)
PtlRect rect = new PtlRect(0.0f,
0.0f,
pageSize.getWidth(),
pageSize.getHeight())) {
// PDFファイルをロードします。
docToInsert.load(inputFileToInsert);
try (PtlPages pagesToInsert = docToInsert.getPages()) { // ページコンテナの取得
// ページコンテナが空かどうか
if (pagesToInsert.isEmpty()) {
System.out.println("ページコンテナが空");
return;
}
// ページの取得(パラメータindexは0が先頭のため1を引く)
// レイヤーのパラメーターの取得
try (PtlPage pageInsert = pagesToInsert.get(insertPageNum - 1);
PtlParamDrawLayer param = new PtlParamDrawLayer()) {
// レイヤーにするページ
param.setPage(pageInsert);
// レイヤーの名前
param.setName("Layer1");
// レイヤーを前面に
param.setZorder(PtlParamDrawLayer.ZORDER.ZORDER_FRONT);
// レイヤーを表示
param.setShow(PtlParamDrawLayer.SHOW.SHOW_ON);
// レイヤーの描画
content.drawLayer(rect, PtlContent.ALIGN.ALIGN_CENTER, param);
}
}
}
}
}
DrawLayerSetPage.java
C:\samples>java cookbook.DrawLayerSetPage usage: java DrawLayerSetPage in-pdf-file out-pdf-file page-num insert-pdf-file page-num-to-insert C:\samples>>java cookbook.DrawLayerSetPage AHLetterHead.pdf DrawLayerSetPage.pdf 1 AHLogo.pdf 1 ページ数:1 -- 完了 --
会社のレターヘッドとロゴをPDFに重ねます。
