/*
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 appendWatermark(PtlPDFDocument& doc);
int main(int argc, char* argv[])
{
if (argc < 3) {
printf("usage: AppendTextWatermark.exe in-pdf-file out-pdf-file\n");
return 1;
}
try
{
PtlParamInput input(argv[1]);
PtlParamOutput output(argv[2]);
PtlPDFDocument doc;
// PDFファイルをロードします。
doc.load(input);
// 透かしの設定
appendWatermark(doc);
// ファイルに保存します。
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)
{
PtlParamWaterMarkText watermarktext;
//透かしの名前の設定
watermarktext.setName("透かしの名前");
//透かしを配置する矩形の設定
watermarktext.setRect(PtlRect(10.0f, 10.0f, 150.0f, 150.0f));
//透かしの配置の設定 ALIGN_BOTTOM_LEFT = 7 /* 左下 */
watermarktext.setAlign(PtlParamWaterMark::ALIGN_BOTTOM_LEFT);
//透かしのZオーダーの設定 ZORDER_FRONT /* 前面 */
watermarktext.setZorder(PtlParamWaterMark::ZORDER_FRONT);
//透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
watermarktext.setPageRange(PtlParamWaterMark::PAGE_RANGE_ALL);
//透かしの不透明度の設定
watermarktext.setOpacity(1.0f);
//透かしをタイリングして配置するかどうかの設定
watermarktext.setTiling(false);
//透かしに指定する文字列の設定
PtlParamString strbuf("PDFToolAPI TextWatermark");
watermarktext.setString(strbuf);
//透かしに指定するフォントの設定
watermarktext.setFont(PtlParamFont("Times New Roman", 180.0f, PtlParamFont::WEIGHT_NORMAL, false, true));
//透かしの文字に指定する色の設定
watermarktext.setTextColor(PtlColorDeviceRGB(1.0f, 0.0f, 0.0f));
//透かしの文字の縁取りに指定する色の設定
watermarktext.setOutlineColor(PtlColorDeviceRGB(0.0f, 1.0f, 0.0f));
//透かしのテキストを対角線上に配置する設定
watermarktext.setWriteDiagonal(false);
//透かしのテキストを任意の角度で配置する設定
watermarktext.setTextAngle(15.0f);
//透かしの設定
doc.appendWaterMark(watermarktext);
}
PDF Tool APIサンプルコード:テキスト透かしの挿入
PDF文書にテキスト透かしを挿入します。
概要
サンプルコードの概要
指定した文字列を透かしとして挿入します。
全てのページの左下に指定した角度の文字列の透かしを配置しています。
- PtlParamWaterMarkText :文字列を透かしに使うパラメータクラス
- PtlParamWaterMarkText.setPageRange() :透かしを配置するページ範囲の設定
- PtlParamWaterMarkText.setString() :透かしに指定する文字列の設定
- 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 AppendTextWatermark {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 2)
{
System.out.println("usage: java AppendTextWatermark in-pdf-file out-pdf-file");
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument())
{
// PDFファイルをロードします。
doc.load(inputFile);
// 透かしの追加
appendWatermark(doc);
// ファイルに保存します。
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) throws PtlException, Exception, Error
{
try (PtlParamWaterMarkText watermarktext = new PtlParamWaterMarkText())
{
// 透かしの名前の設定
watermarktext.setName("透かしの名前");
// 透かしを配置する矩形の設定
try (PtlRect rect = new PtlRect(10.0f, 10.0f, 150.0f, 150.0f))
{
watermarktext.setRect(rect);
}
// 透かしの配置の設定 ALIGN_BOTTOM_LEFT = 7 /* 左下 */
watermarktext.setAlign(PtlParamWaterMark.ALIGN.ALIGN_BOTTOM_LEFT);
// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
watermarktext.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
watermarktext.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);
// 透かしの不透明度の設定
watermarktext.setOpacity(1.0f);
// 透かしをタイリングして配置するかどうかの設定
watermarktext.setTiling(false);
// 透かしに指定する文字列の設定
watermarktext.setString("PDFToolAPI TextWatermark");
// 透かしに指定するフォントの設定
try (PtlParamFont font = new PtlParamFont("Times New Roman", 180.0f, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
{
watermarktext.setFont(font);
}
// 透かしの文字に指定する色の設定
try (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
{
watermarktext.setTextColor(colorText);
}
// 透かしの文字の縁取りに指定する色の設定
try (PtlColorDeviceRGB colorOutline = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
{
watermarktext.setOutlineColor(colorOutline);
}
// 透かしのテキストを対角線上に配置する設定
watermarktext.setWriteDiagonal(false);
// 透かしのテキストを任意の角度で配置する設定
watermarktext.setTextAngle(15.0f);
// 透かしの設定
doc.appendWaterMark(watermarktext);
}
}
}
/*
Antenna House PDF Tool API V7.0
.NET Interface sample program
概要:テキスト透かしの挿入
Copyright 2013-2021 Antenna House, Inc.
*/
using System;
using PdfTkNet;
namespace AppendTextWatermark
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("usage: AppendTextWatermark.exe in-pdf-file out-pdf-file");
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);
// ファイルに保存します。
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)
{
using (PtlParamWaterMarkText watermarktext = new PtlParamWaterMarkText())
{
// 透かしの名前の設定
watermarktext.setName("透かしの名前");
// 透かしを配置する矩形の設定
watermarktext.setRect(new PtlRect(10.0f, 10.0f, 150.0f, 150.0f));
// 透かしの配置の設定 ALIGN_BOTTOM_LEFT = 7 /* 左下 */
watermarktext.setAlign(PtlParamWaterMark.ALIGN.ALIGN_BOTTOM_LEFT);
// 透かしのZオーダーの設定 ZORDER_BACK = 2 /* 前面 */
watermarktext.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);
// 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
watermarktext.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);
// 透かしの不透明度の設定
watermarktext.setOpacity(1.0f);
// 透かしをタイリングして配置するかどうかの設定
watermarktext.setTiling(false);
// 透かしに指定する文字列の設定
watermarktext.setString("PDFToolAPI TextWatermark");
// 透かしに指定するフォントの設定
using (PtlParamFont font = new PtlParamFont("Times New Roman", 180.0f, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
{
watermarktext.setFont(font);
}
// 透かしの文字に指定する色の設定
using (PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
{
watermarktext.setTextColor(color);
}
// 透かしの文字の縁取りに指定する色の設定
using (PtlColorDeviceRGB color = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
{
watermarktext.setOutlineColor(color);
}
// 透かしのテキストを対角線上に配置する設定
watermarktext.setWriteDiagonal(false);
// 透かしのテキストを任意の角度で配置する設定
watermarktext.setTextAngle(15.0f);
// 透かしの設定
doc.appendWaterMark(watermarktext);
}
}
}
}
AHPDFToolCmd70.exe 窶都etTextWatermark -rect 10.0 10.0 150.0 150.0 -align 7 -zorder 1 -pageRange 0 -opacity 1.0 -text "PDFToolAPI TextWatermark" -font -name "Times New Roman" -size 180.0 -embed true -colorText 1 0 0 -colorOutline 0 1 0 -angle 15.0 窶電 c:\in\in.pdf 窶登 c:\sav\outAppendTextWatermark.pdf
実行例
コマンドラインでの実行例
AppendTextWatermark.exe C:\in\in.pdf C:\sav\outAppendTextWatermark.pdf 完了!
java -jar AppendTextWatermark.jar C:\in\in.pdf C:\sav\outAppendTextWatermark.pdf -- 完了 --
AppendTextWatermark.exe C:\in\in.pdf C:\sav\outAppendTextWatermark.pdf -- 完了 --
AHPDFToolCmd70.exe -setTextWatermark -rect 10.0 10.0 150.0 150.0 -align 7 -zorder 1 -pageRange 0 -opacity 1.0 -text ""PDFToolAPI TextWatermark"" -font -name ""Times New Roman"" -size 180.0 -embed true -colorText 1 0 0 -colorOutline 0 1 0 -angle 15.0 -d c:\in\in.pdf -o c:\sav\outAppendTextWatermark.pdf use time 0.175000s
出力結果イメージ
出力されたPDFにはテキスト透かし「PDFToolAPI TextWatermark」が左下に斜め15度で追加されている。

