/*
Antenna House PDF Tool API V7.0
C++ Interface sample program
概要:PDF透かしの挿入
Copyright 2013-2021 Antenna House, Inc.
*/
#include < PdfTk.h >
#include < stdio.h >
using namespace PdfTk;
void appendWatermark(PtlPDFDocument& doc, const char* pathPdf);
int main(int argc, char* argv[])
{
if (argc < 4) {
printf("usage: AppendPdfWatermark.exe in-pdf-file out-pdf-file watermark-pdf\n");
return 1;
}
try
{
PtlParamInput input(argv[1]);
PtlParamOutput output(argv[2]);
PtlPDFDocument doc;
// PDFファイルをロードします。
doc.load(input);
// 透かしの追加
appendWatermark(doc, argv[3]);
// ファイルに保存します。
doc.save(output);
printf("完了!\n");
}
catch (const PtlException &e)
{
fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());
return 1;
}
return 0;
}
void appendWatermark(PtlPDFDocument& doc, const char* pathPdf)
{
PtlPDFDocument doc_watermark;
PtlParamInput watermark(pathPdf);
// PDFファイルをロードします。
doc_watermark.load(watermark);
// ページコンテナの取得
PtlPages& pages = doc_watermark.getPages();
// ページコンテナが空かどうか
if (pages.isEmpty()) {
printf("ページコンテナが空\n");
return;
}
// 透かしの設定
PtlParamWaterMarkPDF watermarkpdf;
// 透かしの名前の設定
watermarkpdf.setName("透かしの名前");
// 透かしの配置の設定 ALIGN_CENTER = 5 /* 中央 */
watermarkpdf.setAlign(PtlParamWaterMark::ALIGN_CENTER);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkpdf.setZorder(PtlParamWaterMark::ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
watermarkpdf.setPageRange(PtlParamWaterMark::PAGE_RANGE_ALL);
// 先頭ページに透かしを配置しない設定の範囲の設定
watermarkpdf.setNotInFirst(false);
// 最終ページに透かしを配置しない設定の設定
watermarkpdf.setNotInLast(false);
// PDF表示時に透かしを表示する指定の設定
watermarkpdf.setDisplayWaterMark(true);
// PDF印刷時に透かしを印刷する指定の設定
watermarkpdf.setPrintWaterMark(true);
// 透かしの不透明度の設定
watermarkpdf.setOpacity(0.5f);
// 透かしをタイリングして配置するかどうかの設定
watermarkpdf.setTiling(false);
// 透かしに使用するページの取得
PtlPage page = pages.get(0);
// 透かしに使用するPDF文書ページを設定
watermarkpdf.setPage(page);
// 透かしの倍率の設定
watermarkpdf.setScale(0.5f);
// 透かしの設定
doc.appendWaterMark(watermarkpdf);
}
PDF Tool APIサンプルコード:PDF透かしの挿入
PDF文書自体を透かしとして挿入します。会社のロゴやレターヘッドなどの線画図形をあらかじめPDF文書として用意しておき、これを透かしとして使用できます。
概要
サンプルコードの概要
任意のPDF文書の1ページを透かしとしてPDF文書のページに挿入します。
全てのページの中心に 倍率0.5 透明度0.5 で指定したPDFの透かしを配置しています
- PtlParamWaterMarkPDF :PDFを透かしに使うパラメータクラス
- PtlPDFDocument.getPages() :透かしに使用するページの取得
- PtlParamWaterMarkPDF.setPage() :透かしに使うPDF文書ページを設定
- PtlPDFDocument.appendWaterMark() :透かしの設定
サンプルコード
/*
Antenna House PDF Tool API V7.0
Java Interface sample program
概要:PDF透かしの挿入
Copyright 2015-2021 Antenna House, Inc.
*/
package Sample;
import jp.co.antenna.ptl.*;
public class AppendPdfWatermark {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 3)
{
System.out.println("usage: java AppendPdfWatermark in-pdf-file out-pdf-file watermark-pdf");
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument())
{
// PDFファイルをロード
doc.load(inputFile);
// 透かしの追加
appendWatermark(doc, args[2]);
// ファイルに保存します。
doc.save(outputFile);
}
catch (PtlException pex) {
System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n " + pex.getErrorMessage());
}
catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
catch (Error ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally {
System.out.println("-- 完了 --");
}
}
public static void appendWatermark(PtlPDFDocument doc, String pathPdf) throws PtlException, Exception, Error
{
try (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF()) // 透かしパラメーター
{
// 透かしの名前の設定
watermarkpdf.setName("透かしの名前");
// 透かしの配置の設定 ALIGN_CENTER = 5 /* 中央 */
watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_CENTER);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);
// 先頭ページに透かしを配置しない設定の範囲の設定
watermarkpdf.setNotInFirst(false);
// 最終ページに透かしを配置しない設定の設定
watermarkpdf.setNotInLast(false);
// PDF表示時に透かしを表示する指定の設定
watermarkpdf.setDisplayWaterMark(true);
// PDF印刷時に透かしを印刷する指定の設定
watermarkpdf.setPrintWaterMark(true);
// 透かしの不透明度の設定
watermarkpdf.setOpacity(0.5f);
// 透かしをタイリングして配置するかどうかの設定
watermarkpdf.setTiling(false);
try (PtlPDFDocument doc_watermark = new PtlPDFDocument();
PtlParamInput inputPdf = new PtlParamInput(pathPdf)) // PDFファイル
{
// PDFファイルをロード
doc_watermark.load(inputPdf);
// 透かしに使用するページの取得
try (PtlPages pages = doc_watermark.getPages()) //ページコンテナの取得
{
//ページコンテナが空かどうか
if (pages.isEmpty())
{
System.out.println("ページコンテナが空\n");
return;
}
try (PtlPage page = pages.get(0)) // 1ページ目
{
// 透かしに使用するPDF文書ページを設定
watermarkpdf.setPage(page);
}
}
}
// 透かしの倍率の設定
watermarkpdf.setScale(0.5f);
// 透かしの設定
doc.appendWaterMark(watermarkpdf);
}
}
}
/*
Antenna House PDF Tool API V7.0
.NET Interface sample program
概要:PDF透かしの挿入
Copyright 2013-2021 Antenna House, Inc.
*/
using System;
using PdfTkNet;
namespace AppendPdfWatermark
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("usage: AppendPdfWatermark.exe in-pdf-file out-pdf-file watermark-pdf");
return;
}
try
{
using (PtlParamInput inputFile = new PtlParamInput(args[0]))
using (PtlParamOutput outputFile = new PtlParamOutput(args[1]))
using (PtlPDFDocument doc = new PtlPDFDocument())
{
// PDFファイルをロードします。
doc.load(inputFile);
// 透かしの追加
appendWatermark(doc, args[2]);
// ファイルに保存します。
doc.save(outputFile);
}
}
catch (PtlException pex)
{
Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
pex.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("-- 完了 --");
}
}
static void appendWatermark(PtlPDFDocument doc, String pathPdf)
{
// 透かしの設定
using (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF())
{
// 透かしの名前の設定
watermarkpdf.setName("透かしの名前");
// 透かしの配置の設定 ALIGN_CENTER = 5 /* 中央 */
watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_CENTER);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);
// 先頭ページに透かしを配置しない設定の範囲の設定
watermarkpdf.setNotInFirst(false);
// 最終ページに透かしを配置しない設定の設定
watermarkpdf.setNotInLast(false);
// PDF表示時に透かしを表示する指定の設定
watermarkpdf.setDisplayWaterMark(true);
// PDF印刷時に透かしを印刷する指定の設定
watermarkpdf.setPrintWaterMark(true);
// 透かしの不透明度の設定
watermarkpdf.setOpacity(0.5f);
// 透かしをタイリングして配置するかどうかの設定
watermarkpdf.setTiling(false);
using (PtlParamInput watermark = new PtlParamInput(pathPdf))
using (PtlPDFDocument doc_watermark = new PtlPDFDocument())
{
// 透かしに使用するPDFファイルをロードします。
doc_watermark.load(watermark);
// 透かしに使用するページの取得
using (PtlPages pages = doc_watermark.getPages())
using (PtlPage page = pages.get(0))
{
// 透かしに使用するPDF文書ページを設定
watermarkpdf.setPage(page);
}
}
// 透かしの倍率の設定
watermarkpdf.setScale(0.5f);
// 透かしの設定
doc.appendWaterMark(watermarkpdf);
}
}
}
}
AHPDFToolCmd70.exe –setPdfWatermark –pdfPath c:\in\icon.pdf -align 5 -zorder 1 –pageRange 0 –scale 0.5 –opacity 0.5 –d c:\in\in.pdf –o c:\sav\outAppendPdfWatermark.pdf
実行例
コマンドラインでの実行例
AppendPdfWatermark.exe C:\in\in.pdf C:\sav\outAppendPdfWatermark.pdf C:\in\icon.pdf 完了!
java -jar AppendPdfWatermark.jar C:\in\in.pdf C:\sav\outAppendPdfWatermark.pdf C:\in\icon.pdf -- 完了 --
AppendPdfWatermark.exe C:\in\in.pdf C:\sav\outAppendPdfWatermark.pdf C:\in\icon.pdf -- 完了 --
AHPDFToolCmd70.exe -setPdfWatermark -pdfPath c:\in\icon.pdf -align 5 -zorder 1 -pageRange 0 -scale 0.5 -opacity 0.5 -d c:\in\in.pdf -o c:\sav\outAppendPdfWatermark.pdf use time 0.034000s
出力結果イメージ
出力されたPDFには全てのページの中心に 倍率0.5 透明度0.5 で指定したPDFの透かしが挿入されます。

