تقييد تحرير المستندات
في بعض الأحيان قد تحتاج إلى الحد من القدرة على تحرير مستند والسماح فقط بإجراءات معينة به. يمكن أن يكون هذا مفيدا لمنع الأشخاص الآخرين من تحرير المعلومات الحساسة والسرية في المستند.
Aspose.Words يسمح لك بتقييد تحرير مستند عن طريق تعيين نوع تقييد. بالإضافة إلى ذلك، يمكنك Aspose.Words أيضا من تحديد إعدادات الحماية ضد الكتابة للمستند.
تشرح هذه المقالة كيفية استخدام Aspose.Words لتحديد نوع تقييد، وكيفية إضافة أو إزالة الحماية، وكيفية إنشاء مناطق غير مقيدة قابلة للتحرير.
حدد نوع تقييد التحرير
Aspose.Words يسمح لك بالتحكم في الطريقة التي تقيد بها المحتوى باستخدام معلمة التعداد ProtectionType. سيمكنك هذا من تحديد نوع محدد من الحماية مثل ما يلي:
- AllowOnlyComments
- AllowOnlyFormFields
- AllowOnlyRevisions
- ReadOnly
- NoProtection
جميع الأنواع مؤمنة بكلمة مرور، وإذا لم يتم إدخال كلمة المرور هذه بشكل صحيح، فلن يتمكن المستخدم من تغيير محتوى المستند بشكل قانوني. وبالتالي، إذا تم إرجاع المستند إليك دون الحاجة إلى توفير كلمة المرور اللازمة، فهذه علامة على وجود خطأ ما.
إذا لم تقم بتعيين كلمة مرور عند اختيار نوع الأمان، فيمكن للمستخدمين الآخرين ببساطة تجاهل حماية المستند الخاص بك.
إضافة حماية المستندات
تعد إضافة الحماية إلى المستند عملية بسيطة، حيث أن كل ما عليك فعله هو تطبيق إحدى طرق الحماية المفصلة في هذا القسم.
Aspose.Words يسمح لك بحماية مستنداتك من التغييرات باستخدام طريقة Protect. هذه الطريقة ليست ميزة أمان ولا تقوم بتشفير مستند.
في Microsoft Word، يمكنك تقييد التحرير بطريقة مماثلة باستخدام كليهما:
- تقييد التحرير (ملف-معلومات-حماية المستند)
- ميزة بديلة – “تقييد التحرير” (مراجعة-حماية-تقييد التحرير)
يوضح مثال الكود التالي كيفية إضافة حماية بكلمة مرور إلى المستند:
// 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()); |