
挿入するレイヤーの透明度を指定します。
透明度の数値を指定し、6.1.1 レイヤーに使用するPDF文書ページを設定と同様にしてレイヤーを挿入します。
PtlParamDrawLayer.APIsetOpacity(float opacity): レイヤーの不透明度を設定
0.0~1.0の間の小数値で指定し、0.0が透明で、1.0が不透明です。
指定しない場合はデフォルト値として1.0が設定されます。
package cookbook;
import jp.co.antenna.ptl.*;
public class DrawLayerSetOpacity {
// そのクラスのusageを表示する関数
private static void printUsage() {
System.out.println("usage: java DrawLayerSetOpacity in-pdf-file out-pdf-file" +
" page-num insert-pdf-file page-num-to-insert opacity");
}
/**
* @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]);
float opacity = Float.parseFloat(args[5]);
if((opacity < 0)||(1<opacity)){
System.out.println("opacity は0から1の間の小数値を指定してください。");
}
try (PtlPages pages = doc.getPages()) { // ページコンテナの取得
// ページコンテナが空かどうか
if (pages.isEmpty()) {
System.out.println("ページコンテナが空");
return;
}
// ページの取得(パラメータindexは0が先頭のため1を引く)
try (PtlPage page = pages.get(pageToAdd - 1)) {
//レイヤーの描画
drawLayerSetOpacity(page, insertPDFPath, insertPageNum, opacity);
}
}
// ファイルに保存します。
doc.save(outputFile);
}
...【ExtractText.javaと同じ処理のため省略
・エラーメッセージ処理と出力】...
}
public static void drawLayerSetOpacity(PtlPage page, String insertPDFPath,
int insertPageNum, float opacity)
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.setOpacity(opacity);
// レイヤーにするページ
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);
}
}
}
}
}
DrawLayerSetOpacity.java
C:\samples>>java cookbook.DrawLayerSetOpacity usage: java DrawLayerSetOpacity in-pdf-file out-pdf-file page-num insert-pdf-file page-num-to-insert opacity C:\samples>>java cookbook.DrawLayerSetOpacity novelText.pdf DrawLayerSetOpacity.pdf 1 AHLogo.pdf 1 0.5 ページ数:2 -- 完了 --
次の図のようにロゴが半透明ですので、本文が透けて見えます。
