OEM販売のご相談
ご相談ください!

PDF Tool APIサンプルコード:セキュリティの解除

機能イメージ

PDF文書に設定されているセキュリティ(暗号化情報)を削除します。

概要

サンプルコードの概要

PDF文書に設定されているセキュリティを削除して、セキュリティの付いていないPDF文書としての保存ができます。
入力PDFにユーザーパスワードのみ設定されているときは、読み込み時にユーザーパスワードを入力してセキュリティを削除できます。
入力PDFにオーナーパスワードが設定されているときは、読み込み時にオーナーパスワードを入力してセキュリティを削除します。
オーナーパスワードとユーザーパスワードの両方設定されているときは、オーナーパスワードを入力してセキュリティを削除します。
ユーザーパスワードではセキュリティ削除はできません。

  • PtlPDFDocument :PDF文書を表現したクラス
  • PtlPDFDocument.removeEncrypt() :暗号化情報の削除

サンプルコード

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

            
/*
    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("-- 完了 --");
        }
    }
}

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

	概要:セキュリティ解除

	Copyright 2013-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace Decrypt
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: Decrypt.exe in-pdf-file out-pdf-file in-pdf-password");
                return;
            }

            try
            {
                using (PtlParamInput inputFile = new PtlParamInput(args[0]))
                using (PtlParamOutput outputFile = new PtlParamOutput(args[1]))
                using (PtlPDFDocument doc = new PtlPDFDocument())
                {
                    // パスワードのセット
                    doc.setPassword(args[2]);

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

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

                    // ファイルに保存します。
                    doc.save(outputFile);
                }
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("-- 完了 --");
            }
        }
    }
}

            
AHPDFToolCmd70.exe -decrypt –d c:\in\test_pass.pdf ownerpass –o c:\sav\outDecrypt.pdf

            

サンプルコードのダウンロードはこちら

実行例

コマンドラインでの実行例

Decrypt.exe C:\in\test_pass.pdf C:\sav\outDecrypt.pdf ownerpass
完了!
java -jar Decrypt.jar C:\in\test_pass.pdf C:\sav\outDecrypt.pdf ownerpass
-- 完了 --
Release\net8.0\Decrypt.exe C:\in\test_pass.pdf C:\sav\outDecrypt.pdf userpass
パスワードに処理権限がありません
-- 完了 --

Decrypt.exe C:\in\test_pass.pdf C:\sav\outDecrypt.pdf ownerpass
-- 完了 --
AHPDFToolCmd70.exe -decrypt -d C:\in\test_pass.pdf ownerpass -o C:\sav\outDecrypt.pdf
 use time 0.062000s

出力結果イメージ

出力されたPDFのセキュリティは削除されています。

サンプルコードのダウンロード

サンプルコード

サンプルで使用した入出力PDF