/*
Antenna House PDF Tool API V7.0
C++ Interface sample program
概要:画像透かしの挿入
Copyright 2013-2021 Antenna House, Inc.
*/
#include < PdfTk.h >
#include < stdio.h >
using namespace PdfTk;
void appendWatermarkImage(PtlPDFDocument& doc, const char* pathImage);
void appendWatermarkPDF(PtlPDFDocument& doc, const char* pathImage);
int main(int argc, char* argv[])
{
if (argc < 4) {
printf("usage: AppendImageWatermark.exe in-pdf-file out-pdf-file watermark-image\n");
return 1;
}
try
{
PtlParamInput input(argv[1]);
PtlParamOutput output(argv[2]);
PtlPDFDocument doc;
// PDFファイルをロードします。
doc.load(input);
// 透かしの設定
appendWatermarkImage(doc, argv[3]);
//appendWatermarkPDF(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 appendWatermarkImage(PtlPDFDocument& doc, const char* pathImage)
{
// 透かしの設定
PtlParamWaterMarkImage watermarkimage;
// 入力画像ストリームの設定
PtlParamInput inputimage(pathImage);
watermarkimage.setImageStream(inputimage);
inputimage.close();
// 透かしの名前の設定
watermarkimage.setName("透かしの名前");
// 透かしを配置するときの余白の設定
watermarkimage.setMargin(10.0f, 10.0f, 10.0f, 10.0f);
// 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */
watermarkimage.setAlign(PtlParamWaterMark::ALIGN_TOP_LEFT);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkimage.setZorder(PtlParamWaterMark::ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */
watermarkimage.setPageRange(PtlParamWaterMark::PAGE_RANGE_ODD);
// 透かしの不透明度の設定
watermarkimage.setOpacity(0.9f);
// 透かしをタイリングして配置するかどうかの設定
watermarkimage.setTiling(false);
// 透かしの倍率の設定
watermarkimage.setScale(0.8f);
// 透かしの設定
doc.appendWaterMark(watermarkimage);
}
void appendWatermarkPDF(PtlPDFDocument& doc, const char* pathImage)
{
// 透かしの設定
PtlParamWaterMarkPDF watermarkpdf;
// 透かしに使用する画像ページを設定
PtlParamImagePage imagepage;
// 用紙タイプの設定 PAPER_IMAGE_SIZE /* 画像サイズに合わせる */
imagepage.setPaperType(PtlParamImagePage::PAPER_IMAGE_SIZE);
// 画像の描画に使うパラメータクラス
PtlParamDrawImage paramDrawImage;
// 入力画像ストリームの設定
PtlParamInput inputimage(pathImage);
paramDrawImage.setImageStream(inputimage);
inputimage.close();
// ページに挿入する画像パラメータの設定。
imagepage.setImage(paramDrawImage);
PtlPDFDocument doc_img;
// ページコンテナの取得
PtlPages& pages = doc_img.getPages();
// 画像ページの追加
pages.append(imagepage);
// 指定位置にあるページを取得
PtlPage page = pages.get(0);
// 透かしに使用するPDF文書ページを設定
watermarkpdf.setPage(page);
// 透かしの名前の設定
watermarkpdf.setName("透かしの名前");
// 透かしを配置するときの余白の設定
watermarkpdf.setMargin(10.0f, 10.0f, 10.0f, 10.0f);
// 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */
watermarkpdf.setAlign(PtlParamWaterMark::ALIGN_TOP_LEFT);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkpdf.setZorder(PtlParamWaterMark::ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */
watermarkpdf.setPageRange(PtlParamWaterMark::PAGE_RANGE_ODD);
// 透かしの不透明度の設定
watermarkpdf.setOpacity(0.9f);
// 透かしをタイリングして配置するかどうかの設定
watermarkpdf.setTiling(false);
// 透かしの倍率の設定
watermarkpdf.setScale(0.8f);
// 透かしの設定
doc.appendWaterMark(watermarkpdf);
}
PDF Tool APIサンプルコード:画像透かしの挿入
PDF文書に画像を透かしとして配置します
概要
サンプルコードの概要
指定したイメージ画像ファイルを透かしとしてPDF文書のページに挿入します。
- PtlParamWaterMarkImage :画像を透かしに使うパラメータクラス
- PtlParamWaterMarkImage.setImageStream() :入力画像ストリームの設定
- PtlPDFDocument.appendWaterMark() :透かしの設定
サンプルコード
/*
Antenna House PDF Tool API V7.0
Java Interface sample program
概要:画像透かしの挿入
Copyright 2015-2021 Antenna House, Inc.
*/
package Sample;
import jp.co.antenna.ptl.*;
public class AppendImageWatermark {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 3)
{
System.out.println("usage: java AppendImageWatermark in-pdf-file out-pdf-file watermark-image-file");
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument())
{
// PDFファイルをロード
doc.load(inputFile);
// 透かしの追加
appendWatermarkImage(doc, args[2]);
//appendWatermarkPDF(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 appendWatermarkImage(PtlPDFDocument doc, String pathImage) throws PtlException, Exception, Error
{
try (PtlParamWaterMarkImage watermarkimage = new PtlParamWaterMarkImage()) // 透かしパラメーター
{
try (PtlParamInput inputImage = new PtlParamInput(pathImage)) // 画像ファイル
{
// 画像描画パラメータに画像ファイルを設定
watermarkimage.setImageStream(inputImage);
}
// 透かしの名前の設定
watermarkimage.setName("透かしの名前");
// 透かしを配置するときの余白の設定
watermarkimage.setMargin(10.0f, 10.0f, 10.0f, 10.0f);
// 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */
watermarkimage.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_LEFT);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkimage.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */
watermarkimage.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ODD);
// 透かしの不透明度の設定
watermarkimage.setOpacity(0.9f);
// 透かしをタイリングして配置するかどうかの設定
watermarkimage.setTiling(false);
// 透かしの倍率の設定
watermarkimage.setScale(0.8f);
// 透かしの設定
doc.appendWaterMark(watermarkimage);
}
}
public static void appendWatermarkPDF(PtlPDFDocument doc, String pathImage) throws PtlException, Exception, Error
{
try (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF()) // 透かしパラメーター
{
try (PtlPDFDocument aDocTmp = new PtlPDFDocument();
PtlPages pagesCustomStamp = aDocTmp.getPages();
PtlParamDrawImage paramDrawImage = new PtlParamDrawImage(); // 画像描画パラメータ
PtlParamInput inputImage = new PtlParamInput(pathImage); // 画像ファイル
PtlParamImagePage paramImagePage = new PtlParamImagePage()) // 画像ページパラメータ
{
// 画像描画パラメータに画像ファイルを設定
paramDrawImage.setImageStream(inputImage);
// 画像ページパラメータに画像描画パラメータを設定
paramImagePage.setImage(paramDrawImage);
// 画像ページのサイズを画像サイズにあわせる
paramImagePage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE);
// ページコンテナに画像ページパラメータを追加
pagesCustomStamp.append(paramImagePage);
// 追加された画像ページを取得する
try (PtlPage page = pagesCustomStamp.get(0)) {
// 透かしに使用するPDF文書ページを設定
watermarkpdf.setPage(page);
}
}
// 透かしの名前の設定
watermarkpdf.setName("透かしの名前");
// 透かしを配置するときの余白の設定
watermarkpdf.setMargin(10.0f, 10.0f, 10.0f, 10.0f);
// 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */
watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_LEFT);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */
watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ODD);
// 透かしの不透明度の設定
watermarkpdf.setOpacity(0.9f);
// 透かしをタイリングして配置するかどうかの設定
watermarkpdf.setTiling(false);
// 透かしの倍率の設定
watermarkpdf.setScale(0.8f);
// 透かしの設定
doc.appendWaterMark(watermarkpdf);
}
}
}
/*
Antenna House PDF Tool API V7.0
.NET Interface sample program
概要:画像透かしの挿入
Copyright 2013-2021 Antenna House, Inc.
*/
using System;
using PdfTkNet;
namespace AppendImageWatermark
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("usage: AppendImageWatermark.exe input-pdf output-pdf watermark-image");
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);
// 透かしの追加
appendWatermarkImage(doc, args[2]);
//appendWatermarkPDF(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 appendWatermarkImage(PtlPDFDocument doc, String pathImage)
{
// 透かしの設定
using (PtlParamWaterMarkImage watermarkImage = new PtlParamWaterMarkImage())
using (PtlParamInput inputimage = new PtlParamInput(pathImage)) // 画像のパス
{
// 入力画像ストリームの設定
watermarkImage.setImageStream(inputimage);
// 透かしの名前の設定
watermarkImage.setName("透かしの名前");
// 透かしを配置するときの余白の設定
watermarkImage.setMargin(10.0f, 10.0f, 10.0f, 10.0f);
// 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */
watermarkImage.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_LEFT);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkImage.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */
watermarkImage.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ODD);
// 透かしの不透明度の設定
watermarkImage.setOpacity(0.9f);
// 透かしをタイリングして配置するかどうかの設定
watermarkImage.setTiling(false);
// 透かしの倍率の設定
watermarkImage.setScale(0.8f);
// 透かしの設定
doc.appendWaterMark(watermarkImage);
}
}
static void appendWatermarkPDF(PtlPDFDocument doc, String pathImage)
{
// 透かしの設定
using (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF())
{
// 透かしに使用する画像ページを設定
using (PtlParamImagePage imagepage = new PtlParamImagePage())
using (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage()) // 画像の描画に使うパラメータクラス
using (PtlParamInput inputimage = new PtlParamInput(pathImage)) // 画像のパス
using (PtlPDFDocument doc_img = new PtlPDFDocument()) // 画像から作成したPDF
using (PtlPages pages = doc_img.getPages()) //ページコンテナの取得
{
// 用紙タイプの設定 PAPER_IMAGE_SIZE /* 画像サイズに合わせる */
imagepage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE);
// 入力画像ストリームの設定
paramDrawImage.setImageStream(inputimage);
// ページに挿入する画像パラメータの設定。
imagepage.setImage(paramDrawImage);
// 画像ページの追加
pages.append(imagepage);
// 先頭ページを取得
using (PtlPage page = pages.get(0))
{
// 透かしに使用するPDF文書ページを設定
watermarkpdf.setPage(page);
}
}
// 透かしの名前の設定
watermarkpdf.setName("透かしの名前");
// 透かしを配置するときの余白の設定
watermarkpdf.setMargin(10.0f, 10.0f, 10.0f, 10.0f);
// 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */
watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_LEFT);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */
watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ODD);
// 透かしの不透明度の設定
watermarkpdf.setOpacity(0.9f);
// 透かしをタイリングして配置するかどうかの設定
watermarkpdf.setTiling(false);
// 透かしの倍率の設定
watermarkpdf.setScale(0.8f);
// 透かしの設定
doc.appendWaterMark(watermarkpdf);
}
}
}
}
AHPDFToolCmd70.exe -setImageWatermark -imagePath C:\in\300x300.jpg -margin 10.0 10.0 10.0 10.0 -align 1 -zorder 1 -pageRange 3 -opacity 0.9 -scale 0.8 -d C:\in\in.pdf -o C:\sav\outAppendImageWatermark.pdf
実行例
コマンドラインでの実行例
AppendImageWatermark.exe C:\in\in.pdf C:\sav\outAppendImageWatermark.pdf C:\in\300x300.jpg 完了!
java -jar AppendImageWatermark.jar C:\in\in.pdf C:\sav\outAppendImageWatermark.pdf C:\in\300x300.jpg -- 完了 --
AppendImageWatermark.exe C:\in\in.pdf C:\sav\outAppendImageWatermark.pdf C:\in\300x300.jpg -- 完了 --
AHPDFToolCmd70.exe -setImageWatermark -imagePath C:\in\300x300.jpg -margin 10.0 10.0 10.0 10.0 -align 1 -zorder 1 -pageRange 3 -opacity 0.9 -scale 0.8 -d C:\in\in.pdf -o C:\sav\outAppendImageWatermark.pdf use time 0.036000s
出力結果イメージ
透かしは偶数ページの左上に倍率0.8不透明度0.9で追加されている。

