﻿/*
	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;

int main(int argc, char* argv[])
{
	if (argc < 2) {
		printf("usage: AnnotToContent.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);

		// ページコンテナの取得
		PtlPages& pages = doc.getPages();

		// ページコンテナが空かどうか
		if (pages.isEmpty()){
			printf("ページコンテナが空\n");
			return 1;
		}

		int numPages = pages.getCount();
        for (int i=0; i<numPages; i++)
        {
			printf("ページ : %d\n", i+1);
			// ページの取得
			PtlPage page = pages.get(i);
	
			// 注釈コンテナの取得
			PtlAnnots& annots = page.getAnnots();

			// 注釈コンテナが空かどうか
			if (annots.isEmpty()) {
				printf("注釈なし\n");
			} else {
				// ページコンテント取得
				PtlContent& content = page.getContent();

				// 注釈数の取得
				int numAnnots = annots.getCount();
				printf("注釈数 : %d\n", numAnnots);
				for (int j=numAnnots-1; j >= 0; j--)
				{
					// 注釈取得
					PtlAnnot annot = annots.get(j);

					// 注釈をページに書き込む
					content.drawForm(annot);

					// 元の注釈を削除する
					annots.remove(j);
				}
			}
		}

		// ファイルに保存します。
		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;
}
