0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C++】Word 文書の保護と解除をプログラムで制御する

0
Posted at

Word 文書に機密情報や重要なデータが含まれる場合、文書のセキュリティ設定は重要な意味を持ちます。Word には、パスワードによる暗号化、編集制限、読み取り専用設定など、複数の保護機能が用意されています。一方で、保護が不要になった場合には、作業効率を高めるためにそれらの制限を解除することも必要です。

本記事では、C++ プログラムから Word 文書に対して各種の保護設定とその解除を行う方法を、具体的なコード例とともに解説します。

事前準備

開発環境に Word 文書操作用のライブラリを導入します。Visual Studio の NuGet パッケージマネージャーからインストールする方法と、パッケージを手動でダウンロードしてプロジェクトに組み込む方法があります。本記事では NuGet 経由での導入を前提とします。

ヘッダーファイルのインクルードと名前空間の指定:

#include "Spire.Doc.o.h"

using namespace Spire::Doc;

1. パスワードによる暗号化と解除

文書をパスワードで保護する

文書を開く際にパスワードを要求するように設定するには、Encrypt() メソッドを使用します。この方法では、パスワードを知っているユーザーのみが文書の内容を閲覧できるようになります。

int main() {
    // Documentオブジェクトを作成
    intrusive_ptr<Document> document = new Document();

    // Word ファイルを読み込み
    document->LoadFromFile(L"sample.docx");

    // パスワードで暗号化
    document->Encrypt(L"open-password");

    // 別名で保存
    document->SaveToFile(L"EncryptedDocument.docx", FileFormat::Docx2013);
    document->Close();
}

パスワードを解除する

暗号化された文書からパスワードを削除するには、まずパスワードを指定して文書を開き、RemoveEncryption() メソッドを呼び出します。

int main() {
    // Documentオブジェクトを作成
    intrusive_ptr<Document> document = new Document();

    // パスワードを指定して暗号化された文書を読み込み
    document->LoadFromFile(L"EncryptedDocument.docx", FileFormat::Docx2013, L"open-password");

    // 暗号化を解除
    document->RemoveEncryption();

    // 別名で保存
    document->SaveToFile(L"DecryptedDocument.docx", FileFormat::Docx2013);
    document->Close();
}

2. 編集制限の設定と解除

編集制限を設定する

文書の内容を閲覧可能としつつ、特定の種類の編集のみを許可したい場合には、Protect() メソッドを使用します。保護の種類として、以下のような定数が用意されています。

保護タイプ 説明
AllowOnlyComments コメントの追加・編集のみ許可
AllowOnlyFormFields フォームフィールドへの入力のみ許可
AllowOnlyReading 読み取り専用(変更不可)
AllowOnlyRevisions 変更履歴の記録のみ許可
NoProtection 保護なし
int main() {
    // Document オブジェクトを作成
    intrusive_ptr<Document> document = new Document();

    // Word ファイルを読み込み
    document->LoadFromFile(L"sample.docx");

    // 読み取り専用に設定(パスワード付き)
    document->Protect(ProtectionType::AllowOnlyReading, L"permission-password");

    // 別名で保存
    document->SaveToFile(L"RestrictEditing.docx", FileFormat::Docx2013);
    document->Close();
}

編集制限を解除する

設定された編集制限を解除するには、Protect() メソッドに NoProtection を指定します。この操作では、設定時に指定したパーミッションパスワードを知らなくても制限解除が可能です。

int main() {
    // Documentオブジェクトを作成
    intrusive_ptr<Document> document = new Document();

    // 編集制限付きの文書を読み込み
    document->LoadFromFile(L"RestrictEditing.docx");

    // 保護を解除
    document->Protect(ProtectionType::NoProtection);

    // 別名で保存
    document->SaveToFile(L"UnprotectedDocument.docx", FileFormat::Docx2013);
    document->Close();
}

3. 書き込み保護の設定と解除

書き込み保護を設定する

文書を開く際に「読み取り専用」を推奨する設定や、書き込み用のパスワードを設定するには、SetWriteProtection() メソッドを使用します。

int main() {
    // Document オブジェクトを作成
    intrusive_ptr<Document> document = new Document();

    // Word ファイルを読み込み
    document->LoadFromFile(L"sample.docx");

    // 書き込み保護を設定(パスワード+読み取り専用推奨)
    document->SetWriteProtection(L"write-password", true);

    // 別名で保存
    document->SaveToFile(L"WriteProtectedDocument.docx", FileFormat::Docx2013);
    document->Close();
}

書き込み保護を解除する

書き込み保護を解除するには、UnWriteProtection() メソッドを使用します。

int main() {
    // Document オブジェクトを作成
    intrusive_ptr<Document> document = new Document();

    // 書き込み保護付きの文書を読み込み
    document->LoadFromFile(L"WriteProtectedDocument.docx");

    // 書き込み保護を解除
    document->UnWriteProtection();

    // 別名で保存
    document->SaveToFile(L"UnwriteProtectedDocument.docx", FileFormat::Docx2013);
    document->Close();
}

4. 特定セクションの保護

文書全体に保護をかけた上で、特定のセクションだけ編集を許可する設定も可能です。以下の例では、文書全体を「フォームフィールドへの入力のみ許可」に設定した後、2番目のセクションの保護を無効化しています。

int main() {
    // Document オブジェクトを作成
    intrusive_ptr<Document> document = new Document();

    // Word ファイルを読み込み
    document->LoadFromFile(L"sample.docx");

    // 文書全体をフォームフィールド入力のみ許可に設定
    document->Protect(ProtectionType::AllowOnlyFormFields, L"permission-password");

    // 2番目のセクション(インデックス1)の保護を解除
    document->GetSections()->GetItemInSectionCollection(1)->SetProtectForm(false);

    // 別名で保存
    document->SaveToFile(L"ProtectSections.docx", FileFormat::Docx2013);
    document->Close();
}

この方法では、指定したセクション内のフォームフィールド以外の領域も編集可能になり、他のセクションではフォームフィールドのみ編集可能という、部分的に異なる制限を実現できます。

注意点

  • パスワードで暗号化された文書を開く際には、必ず正しいパスワードが必要です。パスワードを忘れると文書の内容を復元できなくなるため、管理には十分注意してください。
  • 編集制限の解除(Protect()NoProtection を指定する方法)は、パーミッションパスワードなしで実行できます。これは、文書の保護が不要になった場合の利便性を考慮した仕様です。
  • 各種の保護設定は元に戻せない処理ではないため、必要に応じて元の文書を別途バックアップしておくことを推奨します。

まとめ

本記事では、C++ で記述したプログラムから Word 文書に対してパスワード暗号化、編集制限、書き込み保護、セクション単位の保護を設定・解除する方法を紹介しました。文書のセキュリティレベルは、用途や共有範囲に応じて適切に選択することが重要です。ProtectionType 列挙体には AllowOnlyCommentsAllowOnlyRevisions などのオプションもあり、実際の要件に合わせて柔軟な設定が可能です。なお、本記事で紹介したコード例は、処理の基本を理解するためのものであり、実際のプロジェクトに組み込む際にはエラー処理やファイルパスの適切な管理もあわせて検討してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?