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

	概要：フォームの描画

	Copyright 2015-2021 Antenna House, Inc.
*/

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

using namespace PdfTk;

void drawForm(PtlPage& page, const char* pathPdf);

int main(int argc, char* argv[])
{
	if (argc < 4) {
		printf("usage: DrawForm.exe in-pdf-file out-pdf-file in-pdf-file2\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);

		// フォームの描画
		drawForm(page, argv[3]);

		// ファイルに保存します。
		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 drawForm(PtlPage& page, const char* pathPdf)
{
	// ページコンテントの取得
	PtlContent& content = page.getContent();

	// 描画矩形
	PtlRect rect(0.0f, 0.0f, 100.0f, 100.0f);

	// 挿入するPDF
	PtlParamInput input2(pathPdf);

	PtlPDFDocument doc2;

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

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

	// 先頭ページの取得
	PtlPage pageInsert = pages2.get(0);

	// フォームの描画
	content.drawForm(rect, PtlContent::ALIGN_CENTER, pageInsert);
}
