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

	概要：セキュリティの設定

	Copyright 2013-2021 Antenna House, Inc.
*/

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

using namespace PdfTk;

void setEncrypt(PtlEncryptStandard& enc);

int main(int argc, char* argv[])
{
	if (argc < 4) {
		printf("usage: Encrypt.exe in-pdf-file out-pdf-file 暗号化種類 [in-pdf-password]\n");
		printf("暗号化種類\n0 : 128 bit RC4  1 : 128 bit AES  2 : 256 bit AES \n");
		return 1;
	}
	try
	{
		switch (argv[3][0]) {
		case '0':
		case '1':
		case '2':
			break;
		default:
			printf("usage: Encrypt.exe in-pdf-file out-pdf-file 暗号化種類 [in-pdf-password]\nn");
			printf("暗号化種類\n0 : 128 bit RC4  1 : 128 bit AES  2 : 256 bit AES \n");
			return 1;
		}

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

		PtlPDFDocument doc;
		if (argc > 4) {
			PtlParamString password(argv[4]);
			// パスワードのセット
			doc.setPassword(password);
		}
		// PDFファイルをロードします。
		doc.load(input);

		// セキュリティ設定
		switch (argv[3][0])
		{
		case '0':
			{
				// 128 bit RC4
				PtlEncryptStandard128RC4 enc;
				// 暗号化情報の設定
				setEncrypt(enc);
				// 暗号化設定
				doc.setEncrypt(enc);
			}
			break;
		case '1':
			{
				// 128 bit AES
				PtlEncryptStandard128AES enc;
				// 暗号化情報の設定
				setEncrypt(enc);
				// 暗号化設定
				doc.setEncrypt(enc);
			}
			break;
		case '2':
			{
				// 256 bit AES
				PtlEncryptStandard256AES enc;
				// 暗号化情報の設定
				setEncrypt(enc);
				// 暗号化設定
				doc.setEncrypt(enc);
			}
			break;
		default:
			break;
		}

		// ファイルに保存します。
		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 setEncrypt(PtlEncryptStandard& enc)
{
	// 文書の全てのコンテンツを暗号化
	enc.setEncryptComponent(PtlEncrypt::ENCRYPT_ALL);

	// ユーザパスワード値の設定
	enc.setUserPassword("pass");

	// オーナーパスワード値の設定
	enc.setOwnerPassword("test");

	PtlEncryptPermissionType2 typ;

	// 印刷権限の設定
	// PERM_PRINT_LOW = 1, /* 低解像度 */
	typ.setPrint(PtlEncryptPermissionType2::PERM_PRINT_LOW);

	// 変更権限の設定
	// PERM_MODIFY_ASSEMBLEDOC = 1, /* ページの挿入、削除、回転 */
	typ.setModify(PtlEncryptPermissionType2::PERM_MODIFY_ASSEMBLEDOC);

	// テキスト、画像、その他の内容のコピーを有効にするかどうかの設定
	// false: 有効にしない
	typ.setCopy(false);

	// スクリーンリーダーデバイスのテキストアクセスを有効にするかどうかの設定
	// true: 有効にする
	typ.setAccessibility(true);

	// ユーザアクセス許可フラグの設定
	enc.setPermission(typ);
}
