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

	概要：各種注釈の作成

	Copyright 2025 Antenna House, Inc.
*/

#include < PdfTk.h >
#include < stdio.h >

using namespace PdfTk;

void addAnnotFreeText(PtlPage& page, bool on);
void addAnnotCircle(PtlPage& page);
void addAnnotSquare(PtlPage& page);
void addAnnotLine(PtlPage& page);
void addAnnotPolyLine(PtlPage& page);
void addAnnotPolygone(PtlPage& page);

int main(int argc, char* argv[])
{
	if (argc < 4)
	{
		printf("usage: AppendAnnotShapes.exe in-pdf-file out-pdf-file 規格\n");
		printf("規格\n1 : FreeText注釈（引き出し線あり）  2 : FreeText（引き出し線なし）注釈  3 : 円注釈\n");
		printf("4 : 矩形注釈  5 : ライン注釈  6 : 折れ線注釈  7 : 多角形注釈\n");
		return 1;
	}

	try {
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);

		PtlPDFDocument doc;

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

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

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

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

		const char* kind = argv[3];
		switch (kind[0]) {
		case '1':
		// FreeText注釈（引き出し線あり）の追加		
			addAnnotFreeText(page, true);
			break;
		case '2':
			// FreeText注釈（引き出し線なし）の追加		
			addAnnotFreeText(page, false);
			break;
		case '3':
			// 円注釈の追加
			addAnnotCircle(page);
			break;
		case '4':
			// 矩形注釈の追加
			addAnnotSquare(page);
			break;
		case '5':
			// ライン注釈の追加
			addAnnotLine(page);
			break;
		case '6':
			// 折れ線注釈の追加
			addAnnotPolyLine(page);
			break;
		case '7':
			// 多角形注釈の追加
			addAnnotPolygone(page);
			break;
		default:
			printf("usage: AppendAnnotShapes.exe in-pdf-file out-pdf-file 規格\n");
			printf("規格\n1 : FreeText注釈（引き出し線あり）  2 : FreeText（引き出し線なし）注釈  3 : 円注釈\n");
			printf("4 : 矩形注釈  5 : ライン注釈  6 : 折れ線注釈  7 : 多角形注釈\n");
			return 1;
		}

		// ファイルに保存します。
		doc.save(output);
		printf("完了!\n");
	}
	catch (PtlException e)
	{
		fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());
		return 1;
	}

}

void addAnnotFreeText(PtlPage& page, bool on)
{
	// 注釈コンテナの取得
	PtlAnnots& annots = page.getAnnots();

	// フリーテキスト注釈
	PtlAnnotFreeText annotFreeText;

	// 矩形座標を設定 座標の単位はmmで原点(0,0)は左下
	PtlRect rect(50, 100, 95, 150);
	annotFreeText.setTextBoxRect(rect);

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

	// 内部色を設定
	PtlColorDeviceRGB colorBlue(0.0f, 0.0f, 1.0f);
	annotFreeText.setInteriorColor(colorBlue);

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

	// 日時の設定(2025/01/01 00:00:00)
	annotFreeText.setDate(PtlDate(2025, 1, 1, 0, 0, 0));

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

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

	// 注釈生成日時の設定(2025/01/01 00:00:00)
	annotFreeText.setMarkUpDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
	annotFreeText.setMarkUpCA(0.8f);

	// フリーテキスト注釈のテキストボックス
	PtlTextBoxAnnot textBox;

	// テキストボックスに書く文字のパラメータ1
	PtlParamTextBoxAnnot paramTextBox1;
	PtlParamFontAnnot font("ＭＳ ゴシック", 12, false, false);
	paramTextBox1.setFont(font);
	paramTextBox1.setTextColor(colorRed);

	// テキストボックスに書く文字のパラメータ2
	PtlParamTextBoxAnnot paramTextBox2;
	PtlParamFontAnnot font2("ＭＳ 明朝", 20, false, false);
	paramTextBox2.setFont(font2);
	paramTextBox2.setTextColor(PtlColorDeviceRGB(0.0f, 0.0f, 0.0f));
	paramTextBox2.setUnderline(true);

	// テキストボックスに書く文字のパラメータ3
	PtlParamTextBoxAnnot paramTextBox3;
	PtlParamFontAnnot font3("ＭＳ Ｐ明朝", 20, true, true);
	paramTextBox3.setFont(font3);
	paramTextBox3.setTextColor(PtlColorDeviceRGB(0.0f, 0.0f, 0.0f));
	paramTextBox3.setStrikeOut(true);

	// テキストボックスに書く文字のパラメータ4
	PtlParamTextBoxAnnot paramTextBox4;
	PtlParamFontAnnot font4("ＭＳ 明朝", 10, false, true);
	paramTextBox4.setFont(font4);
	paramTextBox4.setTextColor(PtlColorDeviceRGB(0.0f, 0.0f, 0.0f));

	// テキストボックスへテキスト書き込み
	textBox.writeStringNL("あいうえお", paramTextBox1);
	textBox.writeStringNL("かきくけこ", paramTextBox2);
	textBox.writeString("さしすせそ", paramTextBox1);
	textBox.writeString("たちつてと", paramTextBox3);
	textBox.writeStringNL("なにぬねの", paramTextBox4);

	// フリーテキスト注釈にテキストボックスを設定
	annotFreeText.setTextBox(textBox);

	if (on)
	{
		// 引き出し線の設定
		PtlPoints& points = annotFreeText.getCalloutPoints();
		// 右から右下へ
		points.append(150, 70);
		points.append(130, 125);
		points.append(95, 125);
		// 引き出し線の線端
		annotFreeText.setLineEndingStyle(PtlAnnotFreeText::STYLE_OPEN_ARROW);
	}

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

void addAnnotCircle(PtlPage& page)
{
	// 注釈コンテナの取得
	PtlAnnots& annots = page.getAnnots();

	// 円形注釈
	PtlAnnotCircle annotCircle;

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

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

	// 日時の設定(2025/01/01 00:00:00)
	annotCircle.setDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
	annotCircle.setAnnotFlags(PtlAnnotText::FLAG_NOROTATE);

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

	// 内部色を設定
	annotCircle.setInteriorColor(PtlColorDeviceRGB(1.0f, 1.0f, 0.0f));

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

	// 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
	annotCircle.setBorderWidth(PtlAnnotText::BORDER_WIDTH_THIN);

	//------

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

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

	// 注釈生成日時の設定(2025/01/01 00:00:00)
	annotCircle.setMarkUpDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
	annotCircle.setMarkUpCA(0.8f);

	//------

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

void addAnnotSquare(PtlPage& page)
{
	// 注釈コンテナの取得
	PtlAnnots& annots = page.getAnnots();

	// 矩形注釈
	PtlAnnotSquare annotSquare;

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

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

	// 日時の設定(2025/01/01 00:00:00)
	annotSquare.setDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
	annotSquare.setAnnotFlags(PtlAnnotText::FLAG_NOROTATE);

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

	// 内部色を設定
	annotSquare.setInteriorColor(PtlColorDeviceRGB(1.0f, 1.0f, 0.0f));

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

	// 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
	annotSquare.setBorderWidth(PtlAnnotText::BORDER_WIDTH_THIN);

	//------

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

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

	// 注釈生成日時の設定(2025/01/01 00:00:00)
	annotSquare.setMarkUpDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
	annotSquare.setMarkUpCA(0.8f);

	//------

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

void addAnnotLine(PtlPage& page)
{
	// 注釈コンテナの取得
	PtlAnnots& annots = page.getAnnots();

	// PDFのテキスト注釈
	PtlAnnotLine annotLine;

	// 開始座標を設定 座標の単位はmmで原点(0,0)は左下
	annotLine.setStartPoint(PtlPoint(0.0f, 100.0f));

	// 終了座標を設定 座標の単位はmmで原点(0,0)は左下
	annotLine.setEndPoint(PtlPoint(100.0f, 200.0f));

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

	// 日時の設定(2025/01/01 00:00:00)
	annotLine.setDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
	annotLine.setAnnotFlags(PtlAnnotText::FLAG_NOROTATE);

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

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

	// 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
	annotLine.setBorderWidth(PtlAnnotText::BORDER_WIDTH_THIN);

	// キャプション設定
	annotLine.setCaptionPosition(PtlAnnotLine::POSITION_TOP);
	annotLine.setViewCaption(true);
	annotLine.setHorizontalOffset(30.0f);
	annotLine.setVerticalOffset(10.0f);

	// 線端スタイル設定
	annotLine.setLineStartPointStyle(PtlAnnotLine::LINE_ENDING_STYLE::STYLE_R_OPEN_ARROW);
	annotLine.setLineEndPointStyle(PtlAnnotLine::LINE_ENDING_STYLE::STYLE_DIAMOND);

	// 引き出し線
	annotLine.setLeaderLinesLength(-20.0f);
	annotLine.setExtensionLeaderLines(50.0f);

	// 引き出し線オフセット
	annotLine.setLeaderLinesOffset(20.0f);

	//------

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

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

	// 注釈生成日時の設定(2025/01/01 00:00:00)
	annotLine.setMarkUpDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
	annotLine.setMarkUpCA(0.8f);

	//------

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

void addAnnotPolyLine(PtlPage& page)
{
	// 注釈コンテナの取得
	PtlAnnots& annots = page.getAnnots();

	// 折れ線注釈
	PtlAnnotPolyLine annotPolyLine;

	// 座標を設定 座標の単位はmmで原点(0,0)は左下
	PtlPoints& points = annotPolyLine.getVertices();
	points.append(PtlPoint(92.0f, 123.0f));
	points.append(PtlPoint(96.0f, 117.0f));
	points.append(PtlPoint(103.0f, 121.0f));
	points.append(PtlPoint(115.0f, 119.0f));
	points.append(PtlPoint(121.0f, 112.0f));
	points.append(PtlPoint(130.0f, 113.0f));
	points.append(PtlPoint(121.0f, 122.0f));
	points.append(PtlPoint(90.0f, 126.0f));
	points.append(PtlPoint(86.0f, 124.0f));
	points.append(PtlPoint(89.0f, 116.0f));

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

	// 日時の設定(2025/01/01 00:00:00)
	annotPolyLine.setDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
	annotPolyLine.setAnnotFlags(PtlAnnotText::FLAG_NOROTATE);

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

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

	// 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
	annotPolyLine.setBorderWidth(PtlAnnotText::BORDER_WIDTH_THIN);

	//------

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

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

	// 注釈生成日時の設定(2025/01/01 00:00:00)
	annotPolyLine.setMarkUpDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
	annotPolyLine.setMarkUpCA(0.8f);

	//------

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

void addAnnotPolygone(PtlPage& page)
{
	// 注釈コンテナの取得
	PtlAnnots& annots = page.getAnnots();

	// 多角形注釈
	PtlAnnotPolygon annotPolygone;

	// 座標を設定 座標の単位はmmで原点(0,0)は左下
	PtlPoints& points = annotPolygone.getVertices();
	points.append(PtlPoint(92.0f, 123.0f));
	points.append(PtlPoint(96.0f, 117.0f));
	points.append(PtlPoint(103.0f, 121.0f));
	points.append(PtlPoint(115.0f, 119.0f));
	points.append(PtlPoint(121.0f, 112.0f));
	points.append(PtlPoint(130.0f, 113.0f));
	points.append(PtlPoint(121.0f, 122.0f));
	points.append(PtlPoint(90.0f, 126.0f));
	points.append(PtlPoint(86.0f, 124.0f));
	points.append(PtlPoint(89.0f, 116.0f));

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

	// 日時の設定(2025/01/01 00:00:00)
	annotPolygone.setDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
	annotPolygone.setAnnotFlags(PtlAnnotText::FLAG_NOROTATE);

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

	// 内部色を設定
	annotPolygone.setInteriorColor(PtlColorDeviceRGB(1.0f, 1.0f, 0.0f));

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

	// 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
	annotPolygone.setBorderWidth(PtlAnnotText::BORDER_WIDTH_THIN);

	//------

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

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

	// 注釈生成日時の設定(2025/01/01 00:00:00)
	annotPolygone.setMarkUpDate(PtlDate(2025, 1, 1, 0, 0, 0));

	// 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
	annotPolygone.setMarkUpCA(0.8f);

	//------

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