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

PDF Tool APIサンプルコード:ファイル添付注釈の作成

機能イメージ

PDFにファイル添付注釈を挿入します。添付するファイルは指定したパスにおきます。

概要

サンプルコードの概要

指定したパスにあるファイルを見つけて、入力PDFの1ページ目に、ファイル添付注釈として挿入します。
ファイル添付注釈のアイコンは、下から50mm、左から50mmの位置に、
ポップアップについては(100, 100, 150, 150)の座標にある矩形に表示されます。

  • PtlAnnotFileAttachment: ファイル添付注釈を表現したクラス。
  • PtlAnnotFileAttachment.setFileName(java.lang.String filename): 添付ファイル名を設定します。
  • PtlAnnotFileAttachment.readFile(PtlParamInput inParam): 添付するファイルを読み込みます。
  • PtlAnnotFileAttachment.setIconType(PtlAnnotFileAttachment.ICON_TYPE type): アイコンタイプを設定します。
  • PtlAnnotFileAttachment.ICON_TYPE: アイコンのタイプを表した列挙型定数。

サンプルコード

/*
	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 addAnnotFileAttachment(PtlPage& page, const PtlParamString& attachmentfilename);

int main(int argc, char* argv[])
{
	if (argc < 4) {
		printf("usage: AppendAnnotFileAttachment.exe in-pdf-file out-pdf-file attachment-file\n");
		return 1;
	}
	try
	{
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);
		PtlParamString attachmentfilename(argv[3]);

		PtlPDFDocument doc;

		// PDFファイルをロードします。
		doc.load(input);

		// ページコンテナの取得
		PtlPages& pages = doc.getPages();

		// ページコンテナが空かどうか
		if (pages.isEmpty()){
			printf("ページコンテナが空\n");
			return 1;
		}

		// ページの取得
		PtlPage page = pages.get(0);

		// 注釈の設定
		addAnnotFileAttachment(page, attachmentfilename);
		
		// ファイルに保存します。
		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 addAnnotFileAttachment(PtlPage& page, const PtlParamString& attachmentfilename)
{
	// 注釈コンテナの取得
	PtlAnnots& annots = page.getAnnots();

	// PDFのファイル添付注釈
	PtlAnnotFileAttachment annotfileattachment;

	// アイコンタイプ設定 ICON_PUSHPIN = 3, /* プッシュピン */
	annotfileattachment.setIconType(PtlAnnotFileAttachment::ICON_PUSHPIN);

	// アイコンサイズ
	PtlRect rectSize = annotfileattachment.getRect();

	// 矩形座標を設定 座標の単位はmmで原点(0,0)は左下
	annotfileattachment.setRect(PtlRect(50.0f, 50.0f, 50.0f+rectSize.getRight(), 50.0f+rectSize.getTop()));

	// 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明)
	annotfileattachment.setTextContents("可読な形式での注釈コンテンツの代替説明");

	// 日時の設定(2000/5/5 12:00:00)
	annotfileattachment.setDate(PtlDate(2000, 5, 5, 12, 0, 0));

	// 色を設定
	annotfileattachment.setColor(PtlColorDeviceRGB(1.0f, 0.0f, 1.0f));

	// 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */
	annotfileattachment.setBorderStyle(PtlAnnotText::BORDER_SOLID);

	// 境界線幅を取得 BORDER_WIDTH_THICK = 3 /* 太い */
	annotfileattachment.setBorderWidth(PtlAnnotText::BORDER_WIDTH_THICK);

	// ポップアップウィンドウのタイトル文字列設定
	annotfileattachment.setMarkUpTitle("ポップアップウィンドウのタイトル文字列");

	// サブジェクトの短い説明設定
	annotfileattachment.setMarkUpSubj("サブジェクトの短い説明設定");

	// 注釈生成日時の設定
	annotfileattachment.setMarkUpDate(PtlDate(2000, 4 , 1, 23, 59, 59));

	// 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明
	annotfileattachment.setMarkUpCA(1.0f);

	// 添付ファイル名設定
	annotfileattachment.setFileName(attachmentfilename);

	// 添付するファイルの読み込み
	PtlParamInput inpitAttach(attachmentfilename);
	annotfileattachment.readFile(inpitAttach);

	//------
	PtlAnnotPopup annotpopup;

	// 矩形座標を設定 座標の単位はmmで原点(0,0)は左下
	annotpopup.setRect(PtlRect(100.0f, 100.0f, 150.0f, 150.0f)); 

	// オープン状態を設定 true=オープン状態
	annotpopup.setOpenState(true);

	// ポップアップ注釈を設定
	annotfileattachment.setAnnotPopUp(annotpopup);

	//------

	// 注釈の追加
	annots.append(annotfileattachment);
}

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java AppendAnnotFileAttachment in-pdf-file out-pdf-file attachment-file");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロードします。
            doc.load(inputFile);

            try (PtlPages pages = doc.getPages()) //ページコンテナの取得
            {
                // ページコンテナが空かどうか
                if (pages.isEmpty())
                {
                    System.out.println("ページコンテナが空\n");
                    return;
                }

                try (PtlPage page = pages.get(0)) //1ページ目の取得
                {
                    // ファイル添付注釈追加
                    addAnnotFileAttachment(page, 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 addAnnotFileAttachment(PtlPage page, String attachmentfilename) throws PtlException, Exception, Error
    {
        try (PtlAnnots annots = page.getAnnots(); //注釈コンテナの取得
             PtlAnnotFileAttachment annotfileattachment = new PtlAnnotFileAttachment(); //PDFのファイル添付注釈
             PtlAnnotPopup annotpopup = new PtlAnnotPopup()) // ポップアップ注釈
        {
            // アイコンタイプ設定 ICON_PUSHPIN = 3, /* プッシュピン */
            annotfileattachment.setIconType(PtlAnnotFileAttachment.ICON_TYPE.ICON_PUSHPIN);

            // 矩形座標を設定 座標の単位はmmで原点(0,0)は左下
            try (PtlRect rectSize = annotfileattachment.getRect();
                 PtlRect rectAnnot = new PtlRect(50.0f, 50.0f, 50.0f+rectSize.getRight(), 50.0f+rectSize.getTop()))
            {
                annotfileattachment.setRect(rectAnnot);
            }

            // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明)
            annotfileattachment.setTextContents("注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明");

            // 日時の設定(2000/5/5 12:00:00)
            try (PtlDate date = new PtlDate(2000, 5, 5, 12, 0, 0))
            {
                annotfileattachment.setDate(date);
            }

            // 色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(1.0f, 0.0f, 1.0f))
            {
                annotfileattachment.setColor(color);
            }

            // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */
            annotfileattachment.setBorderStyle(PtlAnnotText.BORDER_STYLE.BORDER_SOLID);

            // 境界線幅を取得 BORDER_WIDTH_THICK = 3 /* 太い */
            annotfileattachment.setBorderWidth(PtlAnnotText.BORDER_LINE_WIDTH.BORDER_WIDTH_THICK);

            // ポップアップウィンドウのタイトル文字列設定
            annotfileattachment.setMarkUpTitle("ポップアップウィンドウのタイトル文字列");

            // サブジェクトの短い説明設定
            annotfileattachment.setMarkUpSubj("サブジェクトの短い説明設定");

            // 注釈生成日時の設定
            try (PtlDate dateMarkup = new PtlDate(2000, 4, 1, 23, 59, 59))
            {
                annotfileattachment.setMarkUpDate(dateMarkup);
            }

            // 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明
            annotfileattachment.setMarkUpCA(1.0f);

            // 添付ファイル名設定
            annotfileattachment.setFileName(attachmentfilename);

            // 添付するファイルの読み込み
            try (PtlParamInput attachmentfile = new PtlParamInput(attachmentfilename))
            {
                annotfileattachment.readFile(attachmentfile);
            }

            // ポップアップ
            // 矩形座標を設定 座標の単位はmmで原点(0,0)は左下
            try (PtlRect rectPopup = new PtlRect(100.0f, 100.0f, 150.0f, 150.0f))
            {
                annotpopup.setRect(rectPopup);
            }

            // オープン状態を設定 true=オープン状態
            annotpopup.setOpenState(true);

            // ポップアップ注釈を設定
           annotfileattachment.setAnnotPopUp(annotpopup);

            // 注釈の追加
            annots.append(annotfileattachment);
        }
    }
}

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

	概要:ファイル添付注釈の作成

	Copyright 2013-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace AppendAnnotFileAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("usage: AppendAnnotFileAttachment.exe in-pdf-file out-pdf-file attachment-file");
                return;
            }

            try
            {
                using (PtlParamInput inputFile = new PtlParamInput(args[0]))
                using (PtlParamOutput outputFile = new PtlParamOutput(args[1]))
                using (PtlPDFDocument doc = new PtlPDFDocument())
                {
                    string attachmentfilename = args[2];

                    //PDFファイルをロードします。
                    doc.load(inputFile);

                    //ページコンテナの取得
                    using (PtlPages pages = doc.getPages())
                    {
                        //ページコンテナが空かどうか
                        if (pages.isEmpty())
                        {
                            Console.WriteLine("ページコンテナが空");
                            return;
                        }

                        //ページの取得
                        using (PtlPage page = pages.get(0))
                        {
                            // 注釈の設定
                            addAnnotFileAttachment(page, attachmentfilename);
                        }
                    }

                    // ファイルに保存します。
                    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 addAnnotFileAttachment(PtlPage page, String attachmentfilename)
        {
            // 注釈コンテナの取得
            using (PtlAnnots annots = page.getAnnots())
            {
                // PDFのファイル添付注釈
                using (PtlAnnotFileAttachment annotfileattachment = new PtlAnnotFileAttachment())
                using (PtlAnnotPopup annotpopup = new PtlAnnotPopup())  // Popupは親注釈と同じスコープでなければなりません
                {
                    // アイコンタイプ設定 ICON_PUSHPIN = 3, /* プッシュピン */
                    annotfileattachment.setIconType(PtlAnnotFileAttachment.ICON_TYPE.ICON_PUSHPIN);

                    // 矩形座標を設定 座標の単位はmmで原点(0,0)は左下
                    using (PtlRect rectSize= annotfileattachment.getRect())
                    using (PtlRect rectAnnot = new PtlRect(50.0f, 50.0f, 50.0f + rectSize.getRight(), 50.0f + rectSize.getTop()))
                    {
                        annotfileattachment.setRect(rectAnnot);
                    }

                    // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明)
                    annotfileattachment.setTextContents("注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明");

                    // 日時の設定(2000/5/5 12:00:00)
                    using (PtlDate date = new PtlDate(2000, 5, 5, 12, 0, 0))
                    {
                        annotfileattachment.setDate(date);
                    }

                    // 色を設定
                    using (PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 1.0f))
                    {
                        annotfileattachment.setColor(color);
                    }

                    // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */
                    annotfileattachment.setBorderStyle(PtlAnnotText.BORDER_STYLE.BORDER_SOLID);

                    // 境界線幅を取得 BORDER_WIDTH_THICK = 3 /* 太い */
                    annotfileattachment.setBorderWidth(PtlAnnotText.BORDER_LINE_WIDTH.BORDER_WIDTH_THICK);

                    // ポップアップウィンドウのタイトル文字列設定
                    annotfileattachment.setMarkUpTitle("ポップアップウィンドウのタイトル文字列");

                    // サブジェクトの短い説明設定
                    annotfileattachment.setMarkUpSubj("サブジェクトの短い説明設定");

                    // 注釈生成日時の設定
                    using (PtlDate date = new PtlDate(2000, 4, 1, 23, 59, 59))
                    {
                        annotfileattachment.setMarkUpDate(date);
                    }

                    //不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明
                    annotfileattachment.setMarkUpCA(1.0f);

                    // 添付ファイル名設定
                    annotfileattachment.setFileName(attachmentfilename);

                    // 添付するファイルの読み込み
                    using (PtlParamInput attachmentfile = new PtlParamInput(attachmentfilename))
                    {
                        annotfileattachment.readFile(attachmentfile);
                    }

                    //------

                    // 矩形座標を設定 座標の単位はmmで原点(0,0)は左下
                    using (PtlRect rectPopup = new PtlRect(100.0f, 100.0f, 150.0f, 150.0f))
                    {
                        annotpopup.setRect(rectPopup);
                    }

                    // オープン状態を設定 true=オープン状態
                    annotpopup.setOpenState(true);

                    // ポップアップ注釈を設定
                    annotfileattachment.setAnnotPopUp(annotpopup);

                    //------

                    // 注釈の追加
                    annots.append(annotfileattachment);
                }
            }
        }
    }
}

            

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

実行例

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

AppendAnnotFileAttachment.exe c:\in\in.pdf c:\sav\outAppendAnnotFileAttachment.pdf c:\in\夏目漱石.txt
完了!
java -jar AppendAnnotFileAttachment.jar C:\in\in.pdf C:\sav\outAppendAnnotFileAttachment.pdf C:\in\夏目漱石.txt
-- 完了 --
AppendAnnotFileAttachment.exe c:\in\in.pdf c:\sav\outAppendAnnotFileAttachment.pdf c:\in\夏目漱石.txt
-- 完了 --

出力結果イメージ

次図はファイル添付注釈のアイコンをダブルクリックしてポップアップ注釈を開いたところです。

出力イメージ

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

サンプルコード

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