تقييد تحرير المستندات

في بعض الأحيان قد تحتاج إلى تقييد القدرة على تحرير مستند والسماح بإجراءات معينة فقط معه. قد يكون هذا مفيدًا لمنع الأشخاص الآخرين من تحرير المعلومات الحساسة والسرية في المستند الخاص بك.

يسمح لك 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 بوضع علامة على الأجزاء التي يمكن تغييرها في مستندك باستخدام طريقتي StartEditableRange وEndEditableRange.

يوضح مثال التعليمات البرمجية التالي كيفية وضع علامة على المستند بأكمله للقراءة فقط وتحديد المناطق القابلة للتحرير فيه:

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