Java でプレゼンテーションを書き込み保護する
はじめに
書き込み保護パスワードはプレゼンテーションの変更を制限しますが、コンテンツを暗号化しません。ユーザーはパスワードなしで書き込み保護されたプレゼンテーションを読み込み、表示できます。アプリケーションによっては、コンテンツを編集して別名で保存できる場合もあるため、書き込み保護は機密保持の手段とみなすべきではありません。
開くパスワードは別の目的で使用されます:プレゼンテーションを暗号化し、コンテンツの読み込みに必要です。プレゼンテーションを暗号化したり、開くパスワードを検証したりするには、パスワードで保護されたプレゼンテーションをご参照ください。
この記事のワークフローは PPT と PPTX の両方のプレゼンテーションに適用されます。例では PPTX ファイルを使用しています。PPT に保存する場合は、.ppt 拡張子と対応する PPT 保存形式を使用してください。
プレゼンテーションへの書き込み保護の設定
IProtectionManager.setWriteProtection を使用して、プレゼンテーションの変更用パスワードを割り当てます。プレゼンテーションを保存すると、保護設定が保持されます。
次の例は PPTX プレゼンテーションに書き込み保護を設定します。
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation("pres.pptx");
try {
presentation.getProtectionManager().setWriteProtection("modify_password");
presentation.save("write-protected-pres.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
書き込み保護されたプレゼンテーションの読み込み
書き込み保護はプレゼンテーションのコンテンツを暗号化しないため、プレゼンテーションの読み込みにパスワードは不要です。パスワードは、保護されたプレゼンテーションの変更権限を検証する場合にのみ関係します。
import com.aspose.slides.Presentation;
Presentation presentation = new Presentation("write-protected-pres.pptx");
try {
System.out.println("Slide count: " + presentation.getSlides().size());
} finally {
presentation.dispose();
}
ILoadOptions.setPassword に書き込み保護パスワードを渡さないでください。このメソッドは暗号化されたコンテンツ用の開くパスワードを受け取ります。プレゼンテーションに両方の保護タイプがある場合は、開くパスワードを使用して読み込み、書き込み保護パスワードは別途処理してください。
プレゼンテーションから書き込み保護を解除する
IProtectionManager.removeWriteProtection を使用して変更制限を解除し、プレゼンテーションを保存します。
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation("write-protected-pres.pptx");
try {
presentation.getProtectionManager().removeWriteProtection();
presentation.save("write-protection-removed.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
プレゼンテーションが書き込み保護されているかの確認
[Presentation] インスタンスを作成せずにファイルを検査するには、IPresentationFactory.getPresentationInfo を呼び出し、IPresentationInfo.isWriteProtected を確認します。このメソッドは NullableBool を使用し、書き込み保護が検出された場合は NullableBool.True を返します。
import com.aspose.slides.IPresentationInfo;
import com.aspose.slides.NullableBool;
import com.aspose.slides.PresentationFactory;
IPresentationInfo presentationInfo = PresentationFactory.getInstance().getPresentationInfo("write-protected-pres.pptx");
if (presentationInfo.isWriteProtected() == NullableBool.True) {
System.out.println("The presentation is write protected.");
} else {
System.out.println("Write protection was not detected.");
}
IPresentationFactory.getPresentationInfo のストリームオーバーロードは、ストリームで提供されたプレゼンテーションに対して同じ情報を提供します。
書き込み保護パスワードの検証
IPresentationInfo.checkWriteProtection を使用して、プレゼンテーション全体を読み込まずに変更パスワードを検証します。まず IPresentationInfo.isWriteProtected を確認し、書き込み保護がある場合にのみアプリケーションがパスワードを要求または検証するようにしてください。
import com.aspose.slides.IPresentationInfo;
import com.aspose.slides.NullableBool;
import com.aspose.slides.PresentationFactory;
IPresentationInfo presentationInfo = PresentationFactory.getInstance().getPresentationInfo("write-protected-pres.pptx");
if (presentationInfo.isWriteProtected() != NullableBool.True) {
System.out.println("The presentation is not write protected.");
} else if (presentationInfo.checkWriteProtection("modify_password")) {
System.out.println("The write-protection password is correct.");
} else {
System.out.println("The write-protection password is incorrect.");
}
IPresentationInfo.checkWriteProtection は書き込み保護パスワードのみを検証します。開くパスワードの検証や、暗号化されたコンテンツが読み込めるかは判断しません。逆に、IPresentationInfo.checkPassword は開くパスワードのみを検証します。完全なプレゼンテーションがすでに読み込まれている場合は、IProtectionManager.checkWriteProtection が保護マネージャーを通じて同等の書き込み保護チェックを提供します。
本番環境のアプリケーションでは、パスワードをログに記録したり診断メッセージに含めたりしないでください。不必要な繰り返し検証を避け、パスワードは必要な期間だけメモリに保持してください。
よくある質問
書き込み保護はプレゼンテーションを暗号化しますか?
いいえ。変更を制限しますが、プレゼンテーションのコンテンツは読み込みおよび表示が可能なままです。
書き込み保護パスワードはプレゼンテーションを開くために必要ですか?
いいえ。暗号化されたプレゼンテーションのコンテンツを読み込むには、開くパスワードだけが必要です。
プレゼンテーションは開くパスワードと書き込み保護パスワードの両方を持つことができますか?
はい。暗号化されたプレゼンテーションを開くにはロードオプションで開くパスワードを提供し、変更権限が必要な場合は書き込み保護パスワードを別途検証してください。