
レイヤーの名前を設定作成したレイヤーそれぞれに名前をつけます。
名前を指定し、6.1.1 レイヤーに使用するPDF文書ページを設定と同様にしてレイヤーを挿入します。
PtlParamDrawLayer.APIsetName(java.lang.String name): レイヤーの名前を設定
名前の設定は必須です。
package cookbook;
import jp.co.antenna.ptl.*;
public class DrawLayerSetLayerName {
// そのクラスのusageを表示する関数
private static void printUsage() {
System.out.println("usage: java DrawLayerSetLayerName in-pdf-file out-pdf-file" +
" page-num insert-pdf-file page-num-to-insert layer-name");
}
/**
* @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]);
String layerName = args[5];
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, layerName);
}
}
// ファイルに保存します。
doc.save(outputFile);
}
...【ExtractText.javaと同じ処理のため省略
・エラーメッセージ処理と出力】...
}
public static void drawLayer(PtlPage page, String insertPDFPath, int insertPageNum,
String layerName)
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.setName(layerName);
// レイヤーにするページ
param.setPage(pageInsert);
// レイヤーを前面に
param.setZorder(PtlParamDrawLayer.ZORDER.ZORDER_FRONT);
// レイヤーを表示
param.setShow(PtlParamDrawLayer.SHOW.SHOW_ON);
// レイヤーの描画
content.drawLayer(rect, PtlContent.ALIGN.ALIGN_CENTER, param);
}
}
}
}
}
DrawLayerSetLayerName.java
C:\samples>>java cookbook.DrawLayerSetLayerName usage: java DrawLayerSetLayerName in-pdf-file out-pdf-file page-num insert-pdf-file page-num-to-insert layer-name C:\samples>>java cookbook.DrawLayerSetLayerName AHLetterHead.pdf DrawLayerSetLayerName.pdf 1 AHLogo.pdf 1 アンテナハウスロゴ ページ数:1 -- 完了 --
次の図のようにAdobe Readerのレイヤーのパネルで名前が設定されているのを確認できます。
