ویرایش اسناد را محدود کنید

گاهی اوقات ممکن است لازم باشد توانایی ویرایش یک سند را محدود کنید و فقط اقدامات خاصی را با آن مجاز کنید. این می تواند برای جلوگیری از ویرایش اطلاعات حساس و محرمانه دیگران در سند شما مفید باشد.

Aspose.Words به شما اجازه می دهد تا ویرایش یک سند را با تنظیم یک نوع محدودیت محدود کنید. علاوه بر این، Aspose.Words همچنین شما را قادر می سازد تا تنظیمات حفاظت از نوشتن یک سند را مشخص کنید.

این مقاله توضیح می دهد که چگونه از Aspose.Words برای انتخاب نوع محدودیت استفاده کنید، چگونه حفاظت را اضافه یا حذف کنید و چگونه مناطق قابل ویرایش بدون محدودیت را ایجاد کنید.

نوع محدودیت ویرایش را انتخاب کنید

Aspose.Words به شما اجازه می دهد تا روش محدود کردن محتوا را با استفاده از پارامتر ProtectionType enumeration کنترل کنید. این به شما امکان می دهد نوع دقیق حفاظت مانند موارد زیر را انتخاب کنید:

  • 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-Java
// Create a new document and protect it with a password.
Document doc = new Document();
// Apply Document Protection.
doc.protect(ProtectionType.NO_PROTECTION, "password");
// Save the document.
doc.save(dataDir + "ProtectDocument.PasswordProtection.docx");

مثال کد زیر نشان می دهد که چگونه ویرایش را در یک سند محدود کنیم تا فقط ویرایش در زمینه های فرم امکان پذیر باشد:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// 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.ALLOW_ONLY_FORM_FIELDS, "password");
// Save the protected document.
doc.save(dataDir + "ProtectDocument.AllowOnlyFormFieldsProtect.docx");

حذف حفاظت از اسناد

Aspose.Words به شما اجازه می دهد تا حفاظت از یک سند را با اصلاح ساده و مستقیم سند حذف کنید. شما می توانید محافظت از سند را بدون دانستن رمز عبور واقعی حذف کنید یا رمز عبور صحیح را برای باز کردن قفل سند با استفاده از روش Unprotect ارائه دهید. هر دو روش حذف هیچ تفاوتی ندارند.

مثال کد زیر نشان می دهد که چگونه حفاظت از سند خود را حذف کنید:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Create a new document and protect it with a password.
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.READ_ONLY, "newPassword");
doc.unprotect("newPassword");
doc.save(dataDir + "ProtectDocument.RemoveDocumentProtection.docx");

مناطق قابل ویرایش بدون محدودیت را مشخص کنید

شما می توانید ویرایش سند خود را محدود کنید و در عین حال اجازه تغییر در بخش های انتخاب شده آن را بدهید. بنابراین، هر کسی که سند شما را باز کند می تواند به این قسمت های نامحدود دسترسی داشته باشد و تغییراتی در محتوا ایجاد کند.

Aspose.Words به شما اجازه می دهد تا قسمت هایی را که می تواند در سند شما با استفاده از روش های StartEditableRange و EndEditableRange تغییر کند، علامت گذاری کنید.

مثال کد زیر نشان می دهد که چگونه کل سند را به عنوان فقط برای خواندن علامت گذاری کنید و مناطق قابل ویرایش را در آن مشخص کنید:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Upload a document and make it as read-only.
Document doc = new Document(dataDir + "Document.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.protect(ProtectionType.READ_ONLY, "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.getEditableRange();
// 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(dataDir + "ProtectDocument.UnrestrictedEditableRegions.docx");

همچنین می توانید محدودیت های مختلف ویرایش اسناد را برای بخش های مختلف انتخاب کنید.

مثال کد زیر نشان می دهد که چگونه یک محدودیت برای کل سند اضافه کنیم و سپس محدودیت را برای یکی از بخش ها حذف کنیم:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Insert two sections with some text.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Section 1. Unprotected.");
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
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.ALLOW_ONLY_FORM_FIELDS, "password");
// By default, all sections are protected, but we can selectively turn protection off.
doc.getSections().get(0).setProtectedForForms(false);
doc.save(dataDir + "Section.Protect.docx");
doc = new Document(dataDir + "Section.Protect.docx");
Assert.assertFalse(doc.getSections().get(0).getProtectedForForms());
Assert.assertTrue(doc.getSections().get(1).getProtectedForForms());