OEM販売のご相談
ご相談ください!

PDF Tool APIサンプルコード:透かしをカスタム角度で挿入

機能イメージ

画像透かし・PDF透かし:透かし画像・PDFをカスタム角度で挿入します。

概要

サンプルコードの概要

画像透かしとPDF透かしをそれぞれカスタム角度で挿入します。

  • PtlParamWaterMarkImage :画像を透かしに使うパラメータクラス
  • PtlParamWaterMarkImage.setImageStream() :入力画像ストリームを設定
  • PtlParamWaterMarkImage.setName() :透かしの名前を設定
  • PtlParamWaterMarkImage.setAlign() :透かしの配置を設定
  • PtlParamWaterMarkImage.setPageRange() :透かしを入れるページの範囲を設定
  • PtlParamWaterMarkImage.setAngle() :透かしを配置する任意の角度を設定
  • PtlPDFDocument.appendWaterMark() :透かしを設定
  • PtlParamWaterMarkPDF :PDFを透かしに使うパラメータクラス
  • PtlParamWaterMarkPDF.setName() :透かしの名前を設定
  • PtlParamWaterMarkPDF.setAlign() :透かしの配置を設定
  • PtlParamWaterMarkPDF.setPageRange() :透かしを入れるページの範囲を設定
  • PtlParamWaterMarkPDF.setAngle() :透かしを配置する任意の角度を設定
  • PtlParamWaterMarkPDF.setPage() :透かしに使用するPDF文書ページを設定
  • PtlPDFDocument.appendWaterMark() :透かしを設定

サンプルコード

/*
	Antenna House PDF Tool API V8.0
	C++ Interface sample program

	概要:画像透かし・PDF透かし:透かし画像・PDFをカスタム角度で挿入

	Copyright 2025- 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 < 5) {
		printf("usage: AppendWatermarkSetAngle.exe in-pdf-file out-pdf-file watermark-image watermark-pdf\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[4]);

		// ファイルに保存します。
		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.setAngle(-15);

	// 透かしの不透明度の設定
	watermarkimage.setOpacity(0.9f);

	// 透かしをタイリングして配置するかどうかの設定
	watermarkimage.setTiling(false);

	// 透かしの倍率の設定
	watermarkimage.setScale(0.8f);

	// 透かしの設定
	doc.appendWaterMark(watermarkimage);
}

void appendWatermarkPDF(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_BOTTOM_RIGHT = 9 /* 右下 */
	watermarkpdf.setAlign(PtlParamWaterMark::ALIGN_BOTTOM_RIGHT);

	// 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
	watermarkpdf.setZorder(PtlParamWaterMark::ZORDER_FRONT);

	// 透かしを入れるページの範囲の設定 PAGE_RANGE_EVEN = 4 /* 偶数ページ */
	watermarkpdf.setPageRange(PtlParamWaterMark::PAGE_RANGE_EVEN);

	// 先頭ページに透かしを配置しない設定の範囲の設定
	watermarkpdf.setNotInFirst(false);

	// 最終ページに透かしを配置しない設定の設定
	watermarkpdf.setNotInLast(false);

	// PDF表示時に透かしを表示する指定の設定
	watermarkpdf.setDisplayWaterMark(true);

	// PDF印刷時に透かしを印刷する指定の設定
	watermarkpdf.setPrintWaterMark(true);

	// 透かしの角度の設定
	watermarkpdf.setAngle(15);

	// 透かしの不透明度の設定
	watermarkpdf.setOpacity(0.5f);

	// 透かしをタイリングして配置するかどうかの設定
	watermarkpdf.setTiling(false);

	// 透かしに使用するページの取得
	PtlPage page = pages.get(0);

	// 透かしに使用するPDF文書ページを設定
	watermarkpdf.setPage(page);

	// 透かしの倍率の設定
	watermarkpdf.setScale(0.5f);

	// 透かしの設定
	doc.appendWaterMark(watermarkpdf);
}

            
/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program

    概要:画像透かし・PDF透かし:透かし画像・PDFをカスタム角度で挿入

    Copyright 2025- Antenna House, Inc.
*/
package Sample;

import jp.co.antenna.ptl.PtlException;
import jp.co.antenna.ptl.PtlPDFDocument;
import jp.co.antenna.ptl.PtlPage;
import jp.co.antenna.ptl.PtlPages;
import jp.co.antenna.ptl.PtlParamInput;
import jp.co.antenna.ptl.PtlParamOutput;
import jp.co.antenna.ptl.PtlParamWaterMark;
import jp.co.antenna.ptl.PtlParamWaterMarkImage;
import jp.co.antenna.ptl.PtlParamWaterMarkPDF;

public class AppendWatermarkSetAngle {

    /**
     * @param args the command line arguments
     */	
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
        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[3]);

            // ファイルに保存します。
            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.setAngle(-15);

            // 透かしの不透明度の設定
            watermarkimage.setOpacity(0.9f);

            // 透かしをタイリングして配置するかどうかの設定
            watermarkimage.setTiling(false);

            // 透かしの倍率の設定
            watermarkimage.setScale(0.8f);

            // 透かしの設定
            doc.appendWaterMark(watermarkimage);
        }
    }

    public static void appendWatermarkPDF(PtlPDFDocument doc, String pathPdf) throws PtlException, Exception, Error
    {
        try (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF())  // 透かしパラメーター
        {
            // 透かしの名前の設定
            watermarkpdf.setName("透かしの名前");

        	// 透かしの配置の設定 ALIGN_BOTTOM_RIGHT = 9 /* 右下 */
            watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_RIGHT);

            // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
            watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);

        	// 透かしを入れるページの範囲の設定 PAGE_RANGE_EVEN = 4 /* 偶数ページ */
            watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_EVEN);

            // 先頭ページに透かしを配置しない設定の範囲の設定
            watermarkpdf.setNotInFirst(false);

            // 最終ページに透かしを配置しない設定の設定
            watermarkpdf.setNotInLast(false);

            // PDF表示時に透かしを表示する指定の設定
            watermarkpdf.setDisplayWaterMark(true);

            // PDF印刷時に透かしを印刷する指定の設定
            watermarkpdf.setPrintWaterMark(true);
            
        	// 透かしの角度の設定
        	watermarkpdf.setAngle(15);

            // 透かしの不透明度の設定
            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 V8.0
	.NET Interface sample program

	概要:画像透かし・PDF透かし:透かし画像・PDFをカスタム角度で挿入

	Copyright 2025- Antenna House, Inc.
*/

using PdfTkNet;
using System;
using System.Xml;

namespace AppendWatermarkSetAngle
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("usage: AppendWatermarkSetAngle.exe in-pdf-file out-pdf-file watermark-image 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);

                    // 透かしの設定
                    appendWatermarkImage(doc, args[2]);
                    appendWatermarkPDF(doc, args[3]);

                    // ファイルに保存します。
                    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.setAngle(-15);

                // 透かしの不透明度の設定
                watermarkImage.setOpacity(0.9f);

                // 透かしをタイリングして配置するかどうかの設定
                watermarkImage.setTiling(false);

                // 透かしの倍率の設定
                watermarkImage.setScale(0.8f);

                // 透かしの設定
                doc.appendWaterMark(watermarkImage);
            }
        }

        static void appendWatermarkPDF(PtlPDFDocument doc, String pathPdf)
        {
            // 透かしの設定
            using (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF())
            {
                // 透かしの名前の設定
                watermarkpdf.setName("透かしの名前");

                // 透かしの配置の設定 ALIGN_BOTTOM_RIGHT = 9 /* 右下 */
                watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_BOTTOM_RIGHT);

                // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
                watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);

                // 透かしを入れるページの範囲の設定 PAGE_RANGE_EVEN = 4 /* 偶数ページ */
                watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_EVEN);

                // 先頭ページに透かしを配置しない設定の範囲の設定
                watermarkpdf.setNotInFirst(false);

                // 最終ページに透かしを配置しない設定の設定
                watermarkpdf.setNotInLast(false);

                // PDF表示時に透かしを表示する指定の設定
                watermarkpdf.setDisplayWaterMark(true);

                // PDF印刷時に透かしを印刷する指定の設定
                watermarkpdf.setPrintWaterMark(true);

                // 透かしの角度の設定
                watermarkpdf.setAngle(15);

                // 透かしの不透明度の設定
                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);
            }
        }

    }
}

            

サンプルコードのダウンロードはこちら

実行例

コマンドラインでの実行例

AppendWatermarkSetAngle.exe C:\in\test.pdf C:\sav\outAppendWatermarkSetAngle.pdf C:\in\300x300.jpg C:\in\sample.pdf
完了!
java -jar AppendWatermarkSetAngle.jar C:\in\test.pdf C:\sav\outAppendWatermarkSetAngle.pdf C:\in\300x300.jpg C:\in\sample.pdf
-- 完了 --
AppendWatermarkSetAngle.exe C:\in	est.pdf C:\sav\outAppendWatermarkSetAngle.pdf C:\in\300x300.jpg C:\in\sample.pdf
-- 完了 --

出力結果イメージ

このサンプルでは奇数ページ左上に画像が右に15度傾いて表示されます。
偶数ページ右下にPDF文書の1ページ目が左に15度傾いて表示されます。

出力イメージ

サンプルコードのダウンロード

サンプルコード

サンプルで使用した入出力PDF