Hạn Chế Chỉnh Sửa Tài Liệu

Đôi khi bạn có thể cần hạn chế khả năng chỉnh sửa tài liệu và chỉ cho phép một số hành động nhất định với nó. Điều này có thể hữu ích để ngăn người khác chỉnh sửa thông tin nhạy cảm và bí mật trong tài liệu của bạn.

Aspose.Words cho phép bạn hạn chế chỉnh sửa tài liệu bằng cách đặt loại hạn chế. Ngoài ra, Aspose.Words cũng cho phép bạn chỉ định cài đặt bảo vệ ghi cho tài liệu.

Bài viết này giải thích cách sử dụng Aspose.Words để chọn loại hạn chế, cách thêm hoặc xóa bảo vệ và cách tạo các vùng có thể chỉnh sửa không hạn chế.

Chọn Loại Hạn Chế Chỉnh Sửa

Aspose.Words cho phép bạn kiểm soát cách bạn hạn chế nội dung bằng cách sử dụng tham số liệt kê ProtectionType. Điều này sẽ cho phép bạn chọn một loại bảo vệ chính xác như sau:

  • AllowOnlyComments
  • AllowOnlyFormFields
  • AllowOnlyRevisions
  • ReadOnly
  • NoProtection

Tất cả các loại đều được bảo mật bằng mật khẩu và nếu mật khẩu này không được nhập chính xác, người dùng sẽ không thể thay đổi hợp pháp nội dung tài liệu của bạn. Do đó, nếu tài liệu của bạn được trả lại cho bạn mà không có yêu cầu cung cấp mật khẩu cần thiết, đây là dấu hiệu cho thấy có điều gì đó không ổn.

Nếu bạn không đặt mật khẩu khi chọn loại bảo mật, những người dùng khác có thể chỉ cần bỏ qua việc bảo vệ tài liệu của bạn.

Thêm Bảo Vệ Tài Liệu

Thêm bảo vệ vào tài liệu của bạn là một quá trình đơn giản, vì tất cả những gì bạn cần làm là áp dụng một trong các phương pháp bảo vệ được nêu chi tiết trong phần này.

Aspose.Words cho phép bạn bảo vệ tài liệu của mình khỏi các thay đổi bằng phương thức Protect. Phương pháp này không phải là một tính năng bảo mật và không mã hóa tài liệu.

Ví dụ mã sau đây cho thấy cách thêm bảo vệ mật khẩu vào tài liệu của bạn:

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");

Ví dụ mã sau đây cho thấy cách hạn chế chỉnh sửa trong tài liệu vì vậy chỉ có thể chỉnh sửa trong các trường biểu mẫu:

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");

Xóa Bảo Vệ Tài Liệu

Aspose.Words cho phép bạn xóa bảo vệ khỏi tài liệu với sửa đổi tài liệu đơn giản và trực tiếp. Bạn có thể xóa bảo vệ tài liệu mà không cần biết mật khẩu thực tế hoặc cung cấp mật khẩu chính xác để mở khóa tài liệu bằng cách sử dụng phương thức Unprotect. Cả hai cách loại bỏ không có sự khác biệt.

Ví dụ mã sau đây cho thấy cách xóa bảo vệ khỏi tài liệu của bạn:

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");

Chỉ Định Các Khu Vực Có Thể Chỉnh Sửa Không Hạn Chế

Bạn có thể hạn chế chỉnh sửa tài liệu của mình và đồng thời cho phép thay đổi các phần đã chọn của tài liệu đó. Vì vậy, bất kỳ ai mở tài liệu của bạn sẽ có thể truy cập các phần không hạn chế này và thực hiện các thay đổi đối với nội dung.

Aspose.Words cho phép bạn đánh dấu các phần có thể thay đổi trong tài liệu của mình bằng các phương thức StartEditableRangeEndEditableRange.

Ví dụ mã sau đây cho thấy cách đánh dấu toàn bộ tài liệu là chỉ đọc và chỉ định các vùng có thể chỉnh sửa trong đó:

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");

Bạn cũng có thể chọn các hạn chế chỉnh sửa tài liệu khác nhau cho các phần khác nhau.

Ví dụ mã sau đây cho thấy cách thêm hạn chế cho toàn bộ tài liệu, sau đó xóa hạn chế cho một trong các phần:

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());