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

PDF Tool APIサンプルコード:色透かしの挿入

機能イメージ

配置領域をカラーで塗りつぶす透かしを挿入します。

概要

サンプルコードの概要

全ページに色透かしを配置します。

  • PtlParamWaterMarkColor :色を透かしに使うパラメータクラス
  • PtlParamWaterMarkColor.setMargin() :透かしを配置するときの空きの設定
  • PtlParamWaterMarkColor.setZorder() :透かしのZオーダーの設定
  • PtlParamWaterMarkColor.setColor() :透かしに指定する色の設定。設定しない場合はデフォルト値として白が使用される
  • PtlParamWaterMarkColor.setOpacity() :透かしの不透明度の設定

サンプルコード

/*
	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: AppendColorWatermark.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)
{
	PtlParamWaterMarkColor watermarkcolor;

	//透かしの名前の設定
	watermarkcolor.setName("透かしの名前");

	//透かしを配置するマージンの設定
	watermarkcolor.setMargin(10.0f, 10.0f, 10.0f, 10.0f);

	//透かしのZオーダーの設定 ZORDER_BACK /* 背面 */
	watermarkcolor.setZorder(PtlParamWaterMark::ZORDER_BACK);

	//透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
	watermarkcolor.setPageRange(PtlParamWaterMark::PAGE_RANGE_ALL);

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

	//透かしの色の設定
	watermarkcolor.setColor(PtlColorDeviceRGB(1.0f, 0.0f, 0.0f));

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

}

            
/*
    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 AppendColorWatermark {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java AppendColorWatermark 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 (PtlParamWaterMarkColor watermarkcolor = new PtlParamWaterMarkColor())
        {
            //透かしの名前の設定
            watermarkcolor.setName("透かしの名前");

            //透かしを配置する矩形の設定
            watermarkcolor.setMargin(10.0f, 10.0f, 10.0f, 10.0f);

            //透かしのZオーダーの設定 ZORDER_FRONT = 2 /* 背面 */
            watermarkcolor.setZorder(PtlParamWaterMark.ZORDER.ZORDER_BACK);

            //透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
            watermarkcolor.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);

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

            //透かしに指定する色の設定
            try (PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
            {
                watermarkcolor.setColor(color);
            }

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

            
/*
	Antenna House PDF Tool API V7.0
	.NET Interface sample program

	概要:色透かしの挿入

	Copyright 2013-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace AppendColorWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: AppendColorWatermark.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 (PtlParamWaterMarkColor watermarkcolor = new PtlParamWaterMarkColor())
            {
                // 透かしの名前の設定
                watermarkcolor.setName("透かしの名前");

                //透かしを配置するマージンの設定
                watermarkcolor.setMargin(10.0f, 10.0f, 10.0f, 10.0f);

                // 透かしのZオーダーの設定 ZORDER_BACK = 2 /* 背面 */
                watermarkcolor.setZorder(PtlParamWaterMark.ZORDER.ZORDER_BACK);

                // 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
                watermarkcolor.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);

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

                // 透かしに指定する色の設定
                using (PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                {
                    watermarkcolor.setColor(color);
                }

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

            
AHPDFToolCmd70.exe -setColorWatermark  -color 1.0 0.0 0.0 -margin 10.0 10.0 10.0 10.0 -zorder 2 -pageRange 0 -opacity 0.4 -d C:\in\in.pdf -o C:\sav\outAppendColorWatermark.pdf
            

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

実行例

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

AppendColorWatermark.exe C:\in\in.pdf C:\sav\outAppendColorWatermark.pdf
完了!
        
java -jar AppendColorWatermark.jar C:\in\in.pdf C:\sav\outAppendColorWatermark.pdf
-- 完了 --
AppendColorWatermark.exe C:\in\in.pdf C:\sav\outAppendColorWatermark.pdf
-- 完了 --
AHPDFToolCmd70.exe -setColorWatermark  -color 1.0 0.0 0.0 -margin 10.0 10.0 10.0 10.0 -zorder 2 -pageRange 0 -opacity 0.4 -d C:\in\in.pdf -o C:\sav\outAppendColorWatermark.pdf
 use time 0.031000s

出力結果イメージ

不透明度40% 赤色の透かしを背後にいれたPDFが出力されます。

出力イメージ

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

サンプルコード

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