/*
    Antenna House PDF Tool API V6.0
    Java Interface sample program

    概要：セキュリティの設定

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import java.io.*;
import jp.co.antenna.ptl.*;

public class Encrypt {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java Encrypt in-pdf-file out-pdf-file 暗号化種類 [in-pdf-password]\n");
            System.out.println("暗号化種類\n0 : 128 bit RC4  1 : 128 bit AES  2 : 256 bit AES \n");
            return;
        }

        switch (args[2]) {
        case "0":
        case "1":
        case "2":
            break;
        default:
            System.out.println("usage: java Encrypt in-pdf-file out-pdf-file 暗号化種類 [in-pdf-password]\n");
            System.out.println("暗号化種類\n0 : 128 bit RC4  1 : 128 bit AES  2 : 256 bit AES \n");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            if (args.length > 3)
            {
                String password = (String)args[3];
                // パスワードのセット
                doc.setPassword(password);
            }

            // PDFファイルをロードします。
            doc.load(inputFile);

            // セキュリティ設定
            switch (args[2])
            {
            case "0":
                // 128 bit RC4
                try (PtlEncryptStandard128RC4 enc = new PtlEncryptStandard128RC4())
                {
                    // 暗号化情報の設定
                    setEncrypt(enc);
                    // 暗号化の設定
                    doc.setEncrypt(enc);
                }
                break;
            case "1":
                // 128 bit AES
                try (PtlEncryptStandard128AES enc = new PtlEncryptStandard128AES())
                {
                    // 暗号化情報の設定
                    setEncrypt(enc);
                    // 暗号化の設定
                    doc.setEncrypt(enc);
                }
                break;
            case "2":
                // 256 bit AES
                try (PtlEncryptStandard256AES enc = new PtlEncryptStandard256AES())
                {
                    // 暗号化情報の設定
                    setEncrypt(enc);
                    // 暗号化の設定
                    doc.setEncrypt(enc);
                }
                break;
            }

            // ファイルに保存します。
            doc.save(outputFile);
        }
        catch (PtlException pex) {
            System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n  " + pex.getErrorMessage());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        catch (Error ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        finally {
            System.out.println("-- 完了 --");
        }
    }

    public static void setEncrypt(PtlEncryptStandard enc) throws PtlException, Exception, Error
    {
        // 文書の全てのコンテンツを暗号化
        enc.setEncryptComponent(PtlEncrypt.ENCRYPT_COMPONENT.ENCRYPT_ALL);

        // ユーザパスワード値の設定
        enc.setUserPassword("user");

        // オーナーパスワード値の設定
        enc.setOwnerPassword("owner");

        try (PtlEncryptPermissionType2 permissionType2 = new PtlEncryptPermissionType2())
        {
            // 印刷権限の設定
            // PERM_PRINT_LOW = 1 低解像度 
            permissionType2.setPrint(PtlEncryptPermissionType2.PERMISSION_PRINT.PERM_PRINT_LOW);

            // 変更権限の設定
            // PERM_MODIFY_ASSEMBLEDOC = 1 ページの挿入、削除、回転
            permissionType2.setModify(PtlEncryptPermissionType2.PERMISSION_MODIFY.PERM_MODIFY_ASSEMBLEDOC);

            // テキスト、画像、その他の内容のコピーを有効にするかどうかの設定
            // false: 有効にしない
            permissionType2.setCopy(false);

            // スクリーンリーダーデバイスのテキストアクセスを有効にするかどうかの設定
            // true: 有効にする
            permissionType2.setAccessibility(true);

            // ユーザアクセス許可フラグの設定
            enc.setPermission(permissionType2);
        }
    }
}

