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

	概要：ファイル添付とセキュリティ設定

	Copyright 2026- Antenna House, Inc.
*/

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

using namespace PdfTk;

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

		PtlPDFDocument doc;

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

		// 添付ファイルコンテナの取得
		PtlEmbeddedFiles& embeddedfiles = doc.getEmbeddedFiles();

		// PDFの添付ファイル
		PtlEmbeddedFile embeddedfile;

		// 添付ファイル名の設定
		embeddedfile.setFileName(embeddedfilename);

		// 添付するファイルの読み込み
		PtlParamInput inputEmbed(embeddedfilename);
		embeddedfile.readFile(inputEmbed);

		// 添付ファイルの追加
		embeddedfiles.append(embeddedfile);

		// 256 bit AES
		PtlEncryptStandard256AES enc;

		// 文書の全てのコンテンツを暗号化
		enc.setEncryptComponent(PtlEncrypt::ENCRYPT_ALL);

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

		PtlEncryptPermissionType2 typ;

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

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

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

		// 暗号化設定
		doc.setEncrypt(enc);

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

