/*
    Antenna House PDF Tool API V7.0
    Java Interface sample program

    概要：セキュリティ解除

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import java.io.*;
import jp.co.antenna.ptl.*;

public class Decrypt {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java Decrypt in-pdf-file out-pdf-file in-pdf-password");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // パスワードのセット
            doc.setPassword(args[2]);

            // PDFファイルをロードします。
            doc.load(inputFile);

            // 暗号化の取得
            if (doc.isEncrypted())
            {
                if (doc.hasOwnerAuthority())
                {
                    // 暗号化の削除
                    doc.removeEncrypt();
                }
                else
                {
                    System.out.println("パスワードに処理権限がありません");
                    return;
                }
            }
            else
            {
                System.out.println("暗号化されていないファイルです\n");
                return;
            }

            // ファイルに保存します。
            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("-- 完了 --");
        }
    }
}
