限制文档编辑

有时您可能需要限制编辑文档的能力并仅允许对其执行某些操作。这对于防止其他人编辑文档中的敏感和机密信息很有用。

Aspose.Words 允许您通过设置限制类型来限制文档的编辑。此外,Aspose.Words 还允许您指定文档的写保护设置。

本文介绍如何使用 Aspose.Words 选择限制类型、如何添加或删除保护以及如何创建不受限制的可编辑区域。

选择编辑限制类型

Aspose.Words 允许您使用 ProtectionType 枚举参数来控制限制内容的方式。这将使您能够选择确切的保护类型,如下所示:

  • 只允许评论
  • 只允许表单字段
  • 只允许修改
  • 只读
  • 无保护

所有类型均受密码保护,如果密码输入不正确,用户将无法合法更改文档的内容。因此,如果您的文档在没有要求您提供必要密码的情况下被退回,则表明出现了问题。

如果您在选择安全类型时没有设置密码,其他用户可以简单地忽略对您文档的保护。

添加文档保护

向文档添加保护是一个简单的过程,因为您所需要做的就是应用本节中详细介绍的保护方法之一。

Aspose.Words 允许您使用 Protect 方法保护文档免遭更改。此方法不是安全功能,也不加密文档。

以下代码示例展示了如何向文档添加密码保护:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Apply document protection.
doc.Protect(ProtectionType.NoProtection, "password");
doc.Save(ArtifactsDir + "DocumentProtection.PasswordProtection.docx");

以下代码示例演示如何限制文档中的编辑,以便只能在表单字段中进行编辑:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Insert two sections with some text.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("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, "password");
// Save the protected document.
doc.Save(ArtifactsDir + "DocumentProtection.AllowOnlyFormFieldsProtect.docx");

删除文档保护

Aspose.Words 允许您通过简单直接的文档修改来删除文档的保护。您可以在不知道实际密码的情况下取消文档保护,也可以使用 Unprotect 方法提供正确的密码来解锁文档。两种去除方式没有区别。

以下代码示例演示如何从文档中删除保护:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("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, "newPassword");
doc.Unprotect("newPassword");
doc.Save(ArtifactsDir + "DocumentProtection.RemoveDocumentProtection.docx");

指定不受限制的可编辑区域

您可以限制对文档的编辑,同时允许对其选定部分进行更改。因此,任何打开文档的人都可以访问这些不受限制的部分并对内容进行更改。

Aspose.Words 允许您使用 StartEditableRangeEndEditableRange 方法标记文档中可以更改的部分。

以下代码示例演示如何将整个文档标记为只读并指定其中的可编辑区域:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Upload a document and make it as read-only.
Document doc = new Document(MyDir + "Document.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.Protect(ProtectionType.ReadOnly, "MyPassword");
builder.Writeln("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.
EditableRangeStart edRangeStart = builder.StartEditableRange();
// An EditableRange object is created for the EditableRangeStart that we just made.
EditableRange editableRange = edRangeStart.EditableRange;
// Put something inside the editable range.
builder.Writeln("Paragraph inside first editable range");
// An editable range is well-formed if it has a start and an end.
EditableRangeEnd edRangeEnd = builder.EndEditableRange();
builder.Writeln("This paragraph is outside any editable ranges, and cannot be edited.");
doc.Save(ArtifactsDir + "DocumentProtection.UnrestrictedEditableRegions.docx");

您还可以为不同的部分选择不同的文档编辑限制。

以下代码示例演示如何为整个文档添加限制,然后删除其中一个部分的限制:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Insert two sections with some text.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Section 1. Unprotected.");
builder.InsertBreak(BreakType.SectionBreakContinuous);
builder.Writeln("Section 2. Protected.");
// Section protection only works when document protection is turned and only editing in form fields is allowed.
doc.Protect(ProtectionType.AllowOnlyFormFields, "password");
// By default, all sections are protected, but we can selectively turn protection off.
doc.Sections[0].ProtectedForForms = false;
doc.Save(ArtifactsDir + "DocumentProtection.UnrestrictedSection.docx");
doc = new Document(ArtifactsDir + "DocumentProtection.UnrestrictedSection.docx");
Assert.False(doc.Sections[0].ProtectedForForms);
Assert.True(doc.Sections[1].ProtectedForForms);