/*
	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 removeLinkAction(PtlPage& page);

int main(int argc, char* argv[])
{
	if (argc < 4) {
		printf("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n\n");
		printf("分割方向\n0 : ページを上下に分割  1 : ページを左右に分割\n");
		return 1;
	}
	try
	{
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);

		PtlPDFDocument doc;

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

		// ページ数の取得
		int numPages = doc.getPageCount();
		printf("ページ数：%d\n", numPages);

		// ページコンテナの取得
		PtlPages& pages = doc.getPages();
		if (pages.isEmpty()){
			printf("ページコンテナが空\n");
			return 1;
		}

		const char* direction = argv[3];
		switch (direction[0]) {
		case '0':
		case '1':
			break;
		default:
			printf("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n\n");
			printf("分割方向\n0 : ページを上下に分割  1 : ページを左右に分割\n");
			return 1;
		}

		// 新規PDF
		PtlPDFDocument docNew;
		// ページコンテナの取得
		PtlPages& pagesNew = docNew.getPages();

		for (int i=0; i < numPages; ++i) {
			// 読み込んだページの取得
			PtlPage page = pages.get(i);
			PtlSize size = page.getSize();
			float width = size.getWidth();
			float height = size.getHeight();
			// 読み込んだページの表示矩形取得
			PtlRect viewBox = page.getViewBox();

			// ページを分割していくので注釈のLinkアクションを削除しておく
			removeLinkAction(page);

			// 読み込んだページを新規PDFに2ページ分追加
			pagesNew.append(page, PtlPages::OPTION_NONE);
			pagesNew.append(page, PtlPages::OPTION_NONE);

			// 追加されたページを取得
			int numPagesNew = pagesNew.getCount();
			PtlPage pageNew1 = pagesNew.get(numPagesNew-2);
			PtlPage pageNew2 = pagesNew.get(numPagesNew-1);

			// 追加されたページに設定する表示矩形
			PtlRect rect1(viewBox);
			PtlRect rect2(viewBox);

			switch (direction[0]) {
			case '0':
				{
					// 半分の高さ
					float halfHeight = size.getHeight()/2;
					// 矩形の上半分
					rect1.setBottom(viewBox.getBottom()+halfHeight);
					// 矩形の下半分
					rect2.setTop(viewBox.getTop()-halfHeight);
				}
				break;
			case '1':
				{
					// 半分の幅
					float halfWidth = size.getWidth()/2;
					// 矩形の左半分
					rect1.setRight(viewBox.getRight()-halfWidth);
					// 矩形の右半分
					rect2.setLeft(viewBox.getLeft()+halfWidth);
				}
				break;
			}
			// 追加されたページの表示矩形を設定
			pageNew1.setViewBox(rect1);
			pageNew2.setViewBox(rect2);
		}

		// ファイルに保存します。
		docNew.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 removeLinkAction(PtlPage& page)
{
	if (!page.hasAnnots()) {
		return;
	}
	PtlAnnots& annots = page.getAnnots() ;
	int numAnnots = annots.getCount();
	for(int i=0; i < numAnnots; ++i) {
		PtlAnnot& annot = annots.get(i);
		switch (annot.getType()) {
		case PtlAnnot::TYPE_LINK:
			{
				PtlAnnotLink& annotLink = (PtlAnnotLink&)annot;
				PtlAction& action = annotLink.getAction();
				switch (action.getType()) {
				case PtlAction::TYPE_GOTO:
					annotLink.removeAction();
					break;
				default:
					break;
				}
			}
			break;
		default:
			break;
		}
	}
}
