文書の編集を制限する

場合によっては、ドキュメントを編集する機能を制限し、特定のアクションのみを許可する必要がある場合があります。 これは、他の人がドキュメント内の機密情報や機密情報を編集できないようにするのに役立ちます。

Aspose.Words制限の種類を設定して文書の編集を制限できます。 また、Aspose.Wordsを使用すると、文書の書き込み保護設定を指定することもできます。

この記事では、Aspose.Wordsを使用して制限の種類を選択する方法、保護を追加または削除する方法、および無制限の編集可能領域を作成する方法について説明し

編集制限タイプの選択

Aspose.Wordsでは、ProtectionType列挙パラメーターを使用してコンテンツを制限する方法を制御できます。 これにより、次のような正確なタイプの保護を選択できます:

  • AllowOnlyComments
  • AllowOnlyFormFields
  • AllowOnlyRevisions
  • ReadOnly
  • NoProtection

すべてのタイプはパスワードで保護されており、このパスワードが正しく入力されないと、ユーザーは文書の内容を合法的に変更することはできません。 したがって、必要なパスワードを提供する必要がなくてもドキュメントが返された場合、これは何かが間違っていることを示しています。

セキュリティタイプを選択するときにパスワードを設定しなかった場合、他のユーザーはドキュメントの保護を無視することができます。

文書保護の追加

このセクションで説明する保護方法のいずれかを適用するだけであるため、文書に保護を追加するのは簡単なプロセスです。

Aspose.Wordsを使用すると、Protectメソッドを使用して文書を変更から保護できます。 このメソッドはセキュリティ機能ではなく、ドキュメントを暗号化しません。

次のコード例は、ドキュメントにパスワード保護を追加する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create a new document and protect it with a password.
auto doc = System::MakeObject<Document>();
// Apply Document Protection.
doc->Protect(ProtectionType::NoProtection, u"password");
// Save the document.
doc->Save(ArtifactsDir + u"DocumentProtection.PasswordProtection.docx");

次のコード例は、フォームフィールドでの編集のみが可能になるように、ドキュメントでの編集を制限する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Insert two sections with some text.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Text added to a document.");
// A document protection only works when document protection is turned and only editing in form fields is allowed.
doc->Protect(ProtectionType::AllowOnlyFormFields, u"password");
// Save the protected document.
doc->Save(ArtifactsDir + u"DocumentProtection.PasswordProtection.docx");

文書の保護を削除する

Aspose.Wordsは、文書を簡単かつ直接的に変更して、文書から保護を解除できます。 実際のパスワードを知らずに文書の保護を削除するか、Unprotectメソッドを使用して文書のロックを解除するための正しいパスワードを指定できます。 どちらの削除方法にも違いはありません。

次のコード例は、ドキュメントから保護を削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create a new document and protect it with a password.
auto doc = System::MakeObject<Document>();
// Add text.
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Text added to a document.");
// Documents can have protection removed either with no password, or with the correct password.
doc->Unprotect();
doc->Protect(ProtectionType::ReadOnly, u"newPassword");
doc->Unprotect(u"newPassword");
// Save the document.
doc->Save(ArtifactsDir + u"DocumentProtection.RemoveDocumentProtection.docx");

無制限の編集可能領域の指定

ドキュメントの編集を制限すると同時に、選択した部分への変更を許可することができます。 そのため、ドキュメントを開いた人は誰でも、これらの無制限の部分にアクセスしてコンテンツを変更することができます。

Aspose.Wordsは、StartEditableRangeメソッドとEndEditableRangeメソッドを使用して、文書内で変更できる部分に印を付けることができます。

次のコード例は、ドキュメント全体を読み取り専用としてマークし、その中で編集可能な領域を指定する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Upload a document and make it as read-only.
auto doc = System::MakeObject<Document>(MyDir + u"Document.docx");
auto builder = System::MakeObject<DocumentBuilder>(doc);
doc->Protect(ProtectionType::ReadOnly, u"MyPassword");
builder->Writeln(u"Hello world! Since we have set the document's protection level to read-only, we cannot edit this paragraph without the password.");
// Start an editable range.
auto edRangeStart = builder->StartEditableRange();
// An EditableRange object is created for the EditableRangeStart that we just made.
auto editableRange = edRangeStart->get_EditableRange();
// Put something inside the editable range.
builder->Writeln(u"Paragraph inside first editable range");
// An editable range is well-formed if it has a start and an end.
auto edRangeEnd = builder->EndEditableRange();
// Save your document.
builder->Writeln(u"This paragraph is outside any editable ranges, and cannot be edited.");
doc->Save(ArtifactsDir + u"DocumentProtection.UnrestrictedEditableRegions.docx");

また、セクションごとに異なる文書編集制限を選択することもできます。

次のコード例は、ドキュメント全体に制限を追加してから、いずれかのセクションの制限を削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Insert two sections with some text.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Section 1. Unprotected.");
builder->InsertBreak(BreakType::SectionBreakContinuous);
builder->Writeln(u"Section 2. Protected.");
// Section protection only works when document protection is turned and only editing in form fields is allowed.
doc->Protect(ProtectionType::AllowOnlyFormFields, u"password");
// By default, all sections are protected, but we can selectively turn protection off.
doc->get_Sections()->idx_get(0)->set_ProtectedForForms(false);
doc->Save(ArtifactsDir + u"DocumentProtection.UnrestrictedSection.docx");
doc = System::MakeObject<Document>(ArtifactsDir + u"DocumentProtection.UnrestrictedSection.docx");
ASSERT_FALSE(doc->get_Sections()->idx_get(0)->get_ProtectedForForms());
ASSERT_TRUE(doc->get_Sections()->idx_get(1)->get_ProtectedForForms());