PDF ドキュメントは、パスワードと暗号化を使用して特定のユーザーへのアクセスを制限することで、不正アクセスから保護できます。
注意:PDF/A規格はPDFの暗号化を許可しません。
暗号化プロセスではすべてのストリーム(画像など)と文字列に暗号化が適用されますが、PDF内の他の項目には適用されません。
そのため、PDF構造にはアクセスできますがそのページの内容は暗号化されるため不正なアクセスから保護されます。
この機能は「Pdftools SDK」ライブラリの一部です。
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); }
PDF文書から暗号化情報を削除し、通常通り開けるPDF文書にします。
// Use password to open encrypted input document 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()); // Create output stream for writing 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); // Check if input file is encrypted 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; } } // Set encryption options pOptions = PdfToolsSign_OutputOptions_New(); // Set encryption parameters to no encryption PdfToolsPdf_OutputOptions_SetEncryption((TPdfToolsPdf_OutputOptions*)pOptions, NULL); // Allow removal of signatures. Otherwise the Encryption property is ignored for signed input documents // (see warning category Sign.WarningCategory.SignedDocEncryptionUnchanged). PdfToolsSign_OutputOptions_SetRemoveSignatures(pOptions, ePdfToolsSign_SignatureRemoval_Signed); // Decrypt the document 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) { // Use password to open encrypted input document 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."); // Create stream for output file using var outStr = File.Create(outPath); // Set encryption options var outputOptions = new Sign.OutputOptions() { // Set encryption parameters to no encryption Encryption = null, // Allow removal of signatures. Otherwise the Encryption property is ignored for signed input documents // (see warning category Sign.WarningCategory.SignedDocEncryptionUnchanged). RemoveSignatures = Sign.SignatureRemoval.Signed, }; // Decrypt the document using var outDoc = new Sign.Signer().Process(inDoc, outStr, outputOptions); }
質問のページからお送りいただくようお願いします。
または、メールでsupport@trustss.co.jpあてにお送りください。
ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。