/*
	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;

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

		// パスワードのセット
		doc.setPassword(password);

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

		// 暗号化の取得
		if (doc.isEncrypted()) {
			if (doc.hasOwnerAuthority()) {
				// 暗号化の削除
				doc.removeEncrypt();
			} else {
				printf("パスワードに処理権限がありません\n");
				return 0;
			}
		} else {
		    printf("暗号化されていないファイルです\n");
			return 0;
		}

		// ファイルに保存します。
		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;
}
