PDF ドキュメントは、パスワードと暗号化を使用して特定のユーザーへのアクセスを制限することで、不正アクセスから保護できます。
注意:PDF/A規格はPDFの暗号化を許可しません。
暗号化プロセスではすべてのストリーム(画像など)と文字列に暗号化が適用されますが、PDF内の他の項目には適用されません。
そのため、PDF構造にはアクセスできますがそのページの内容は暗号化されるため不正なアクセスから保護されます。
APIリファレンスはこちらです。
C#のサンプルプロジェクトではPdftools SDKライブラリ(DLL)をNuGetから自動でダウンロードします。
CのサンプルプロジェクトにはPdftools SDKライブラリ(DLL)が含まれています。
ライセンスキー無し(無償)で試用できます。ただし、結果に「透かし」が入ります。
「透かし」の削除をご希望の場合は問い合わせページまたはメールでお問い合わせください。
License Agreement(利用許諾契約書)が含まれていますので必ず確認してください。
PDF文書をユーザー名とパスワードで暗号化します。
文書を開くにはユーザー名とパスワードが必要となります。
// 入力PDFファイルを開く
pInStream = _tfopen(szInPath, _T("rb"));
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath);
TPdfToolsSys_StreamDescriptor inDesc;
PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0);
pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T(""));
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(
pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath,
szErrorBuff, PdfTools_GetLastError());
// 書き込み用の出力ストリームを生成
pOutStream = _tfopen(szOutPath, _T("wb+"));
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath);
TPdfToolsSys_StreamDescriptor outDesc;
PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0);
// 暗号化オプションを設定
pOptions = PdfToolsSign_OutputOptions_New();
// ドキュメントを開くときに必要なユーザー パスワードを設定
// これにより入力ファイルのPDF/A準拠が削除されることに注意してください
// (警告のePdfToolsSign_WarningCategory_PdfARemoved参照)
pEncryption = PdfToolsPdf_Encryption_New(szPassword, NULL, ePdfToolsPdf_Permission_All);
PdfToolsPdf_OutputOptions_SetEncryption((TPdfToolsPdf_OutputOptions*)pOptions, pEncryption);
// 署名の削除を許可します。それ以外の場合は署名された入力文書の暗号化プロパティは無視されます
// (警告のePdfToolsSign_WarningCategory_SignedDocEncryptionUnchanged参照)
PdfToolsSign_OutputOptions_SetRemoveSignatures(pOptions, ePdfToolsSign_SignatureRemoval_Signed);
// PDF文書を暗号化
pSigner = PdfToolsSign_Signer_New();
pOutDoc = PdfToolsSign_Signer_Process(pSigner, pInDoc, &outDesc, pOptions, NULL);
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"),
PdfTools_GetLastError());
static void Encrypt(string password, string inPath, string outPath)
{
// 入力PDFファイルを開く
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// 出力ファイルのストリームを生成
using var outStr = File.Create(outPath);
// 暗号化オプションを設定
var outputOptions = new Sign.OutputOptions()
{
// ドキュメントを開くときに必要なユーザー パスワードを設定
// これにより、入力ファイルのPDF/A準拠が削除されることに注意してください
// (警告のSign.WarningCategory.PdfARemovedを参照)
Encryption = new Encryption(password, null, Permission.All),
// 署名の削除を許可します。それ以外の場合は署名された入力文書の暗号化プロパティは無視されます
// (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照)
RemoveSignatures = Sign.SignatureRemoval.Signed,
};
// PDF文書を暗号化
using var outDoc = new Sign.Signer().Process(inDoc, outStr, outputOptions);
}
def encrypt(password: str, input_path: str, output_path: str):
# 入力PDFファイルを開く
with io.FileIO(input_path, 'rb') as in_stream:
with Document.open(in_stream) as input_document:
# 出力ファイルのストリームを生成
with io.FileIO(output_path, 'wb+') as output_stream:
# 暗号化オプションを設定
output_options = SignOutputOptions()
# ドキュメントを開くときに必要なユーザー パスワードを設定
# これにより、入力ファイルの PDF/A 準拠が削除されることに注意してください
# (警告のSign.WarningCategory.PdfARemovedを参照)
output_options.encryption = Encryption(password, None, Permission.ALL)
# 署名の削除を許可します。それ以外の場合は署名された入力文書の暗号化プロパティは無視されます
# (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照)
output_options.remove_signatures = SignatureRemoval.SIGNED
# PDF文書を暗号化
signer = Signer()
signer.process(input_document, output_stream, output_options)
# PDF文書を暗号化 encrypt(password, input_path, output_path)
PDF文書から暗号化情報を削除し、通常通り開けるPDF文書にします。
// 暗号化された入力文書を開くにはパスワードを使用する
pInStream = _tfopen(szInPath, _T("rb"));
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath);
TPdfToolsSys_StreamDescriptor inDesc;
PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0);
pInDoc = PdfToolsPdf_Document_Open(&inDesc, szPassword);
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(
pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath,
szErrorBuff, PdfTools_GetLastError());
// 出力ファイルのストリームを作成
pOutStream = _tfopen(szOutPath, _T("wb+"));
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath);
TPdfToolsSys_StreamDescriptor outDesc;
PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0);
// 入力ファイルが暗号化されているかどうかを確認
BOOL bIsGetPermissionsSuccessful = PdfToolsPdf_Document_GetPermissions(pInDoc, &iPermissions);
if (!bIsGetPermissionsSuccessful)
{
if (PdfTools_GetLastError() == 0)
{
_tprintf(_T("Validation failed, input file \"%s\" is not encrypted.\n"), szInPath);
iRet = 1;
goto cleanup;
}
else
{
nBufSize = PdfTools_GetLastErrorMessage(NULL, 0);
PdfTools_GetLastErrorMessage(szErrorBuff, MIN(ARRAY_SIZE(szErrorBuff), nBufSize));
_tprintf(_T("Failed to get permissions for input file \"%s\", error %s \n"), szInPath, szErrorBuff);
iRet = 1;
goto cleanup;
}
}
// 暗号化オプションを設定
pOptions = PdfToolsSign_OutputOptions_New();
// 暗号化パラメータを暗号化なしに設定
PdfToolsPdf_OutputOptions_SetEncryption((TPdfToolsPdf_OutputOptions*)pOptions, NULL);
// 署名の削除を許可
// それ以外の場合、署名された入力ドキュメントの暗号化プロパティは無視されます
// (警告カテゴリ Sign.WarningCategory.SignedDocEncryptionUnchanged を参照)
PdfToolsSign_OutputOptions_SetRemoveSignatures(pOptions, ePdfToolsSign_SignatureRemoval_Signed);
// 文書を復号
pSigner = PdfToolsSign_Signer_New();
pOutDoc = PdfToolsSign_Signer_Process(pSigner, pInDoc, &outDesc, pOptions, NULL);
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"),
PdfTools_GetLastError());
static void Decrypt(string password, string inPath, string outPath)
{
// 暗号化された入力文書を開くにはパスワードを使用する
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr, password);
if (inDoc.Permissions == null)
throw new Exception("Input file is not encrypted.");
// 出力ファイルのストリームを作成
using var outStr = File.Create(outPath);
// 暗号化オプションを設定
var outputOptions = new Sign.OutputOptions()
{
// 暗号化パラメータを暗号化なしに設定
Encryption = null;
// 署名の削除を許可します。それ以外の場合は署名された入力ドキュメントの暗号化プロパティを無視します
// (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照)
RemoveSignatures = Sign.SignatureRemoval.Signed;
};
// 文書を復号
using var outDoc = new Sign.Signer().Process(inDoc, outStr, outputOptions);
}
def decrypt(password, input_path, output_path):
# 暗号化された入力文書を開くにはパスワードを使用する
with io.FileIO(input_path, 'rb') as in_stream:
with Document.open(in_stream, password) as input_document:
if input_document.permissions == None:
print(f"Input file is not encrypted.")
return
# 出力ファイルのストリームを作成
with io.FileIO(output_path, 'wb+') as output_stream:
# 暗号化オプションを設定
output_options = SignOutputOptions()
# 暗号化パラメータを暗号化なしに設定
output_options.encryption = None
# 署名の削除を許可します。それ以外の場合は署名された入力ドキュメントの暗号化プロパティを無視します
# (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照)
output_options.remove_signatures = SignatureRemoval.SIGNED
# 文書を復号
signer = Signer()
signer.process(input_document, output_stream, output_options)
# PDF文書を復号 decrypt(password, input_path, output_path)
他の機能サンプルを参照してください。
質問のページからお送りいただくようお願いします。
または、メールでsupport@trustss.co.jpあてにお送りください。
ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。