จำกัดการแก้ไขเอกสาร
บางครั้งคุณอาจต้องจำกัดความสามารถในการแก้ไขเอกสารและอนุญาตให้มีการกระทำบ ะเป็นประโยชน์ในการป้องกันไม่ให้ผู้อื่นแก้ไขข้อมูลที่สำคัญและเป็นความลับในเอกสารข.
Aspose.Wordsอนุญาตให้คุณจำกัดการแก้ไขเอกสารโดยการตั้งค่าประเภทการจำกัด นอกจากนี้Aspose.Wordsยังช่วยให้คุณสามารถระบุการตั้งค่าการป้องกันการเขียนสำหรับเอกสาร.
บทความนี้อธิบายถึงวิธีใช้Aspose.Wordsเพื่อเลือกประเภทข้อจำกัดวิธีเพิ่มหรือเอาการการป้องกันและวิ.
เลือกประเภทการจำกัดการแก้ไข
Aspose.Wordsช่วยให้คุณสามารถควบคุมวิธีที่คุณจำกัดเนื้อหาโดยใช้พารามิเตอร์การแจงนับProtectionType นี้จะช่วยให้คุณสามารถเลือกชนิดของการป้องกันที่แน่นอนเช่นต่อไปนี้:
- AllowOnlyComments
- AllowOnlyFormFields
- AllowOnlyRevisions
- ReadOnly
- NoProtection
ทุกประเภทมีการรักษาความปลอดภัยด้วยรหัสผ่านและถ้ารหัสผ่านนี้ไม่ได้ป้อนอย่างถูกต้องผู้ใช้จะไม่สามารถเปลี่ยนเนื้อหาของเอกสารของคุณได้อย่างถูกต้องตามกฎหมาย ดังนั้นหากเอกสารของคุณจะถูกส่งกลับไปยังคุณโดยไม่จำเป็นต้องให้รหัสผ่านที่จำเป็นนี้เป็.
ถ้าคุณไม่ได้ตั้งรหัสผ่านเมื่อเลือกชนิดความปลอดภัยผู้ใช้อื่นสามารถละเว้นการป้องกันเอก.
เพิ่มการป้องกันเอกสาร
การเพิ่มการป้องกันเอกสารของคุณเป็นกระบวนการที่ง่าย,เป็นสิ่งที่คุณต้องทำคือการใช้หนึ่.
Aspose.Wordsช่วยให้คุณปกป้องเอกสารของคุณจากการเปลี่ยนแปลงโดยใช้วิธีการProtect วิธีนี้ไม่ใช่คุณลักษณะด้านความปลอดภัยและไม่เข้ารหัสเอกสาร.
ในMicrosoft Wordคุณสามารถจำกัดการแก้ไขในลักษณะที่คล้ายกันโดยใช้ทั้งสอง:
- จำกัดการแก้ไข(แฟ้ม→ข้อมูล Protect Protect ปกป้องเอกสาร)
- คุณลักษณะทางเลือก–“จำกัดการแก้ไข”(ตรวจสอบ Protect ป้องกัน Restric จำกัดการแก้ไข)
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มการป้องกันรหัสผ่านในเอกสารของคุณ:
// 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()); |