문서 편집 제한

때로는 문서를 편집 할 수있는 기능을 제한하고 특정 작업 만 허용해야 할 수도 있습니다. 이 기능은 다른 사람이 문서의 민감한 기밀 정보를 편집하지 못하도록 하는 데 유용할 수 있습니다.

Aspose.Words제한 유형을 설정하여 문서 편집을 제한할 수 있습니다. 또한Aspose.Words을 사용하면 문서에 대한 쓰기 보호 설정을 지정할 수도 있습니다.

이 문서에서는Aspose.Words을 사용하여 제한 유형을 선택하는 방법,보호를 추가 또는 제거하는 방법 및 제한되지 않은 편집 가능한 영역을 만드는 방법에 대해 설명합니다.

편집 제한 유형 선택

Aspose.WordsProtectionType열거형 매개 변수를 사용하여 콘텐츠를 제한하는 방법을 제어할 수 있습니다. 이렇게 하면 다음과 같은 정확한 보호 유형을 선택할 수 있습니다:

  • AllowOnlyComments
  • AllowOnlyFormFields
  • AllowOnlyRevisions
  • ReadOnly
  • NoProtection

모든 유형은 암호로 보호되며 이 암호를 올바르게 입력하지 않으면 사용자가 문서 내용을 합법적으로 변경할 수 없습니다. 따라서,문서가 필요한 암호를 제공하는 요구 사항없이 당신에게 반환되는 경우,이 뭔가 잘못되었다는 신호이다.

보안 유형을 선택할 때 암호를 설정하지 않은 경우,다른 사용자는 단순히 문서의 보호를 무시할 수 있습니다.

문서 보호 추가

이 섹션에 설명된 보호 방법 중 하나를 적용하기만 하면 되기 때문에 문서에 보호 기능을 추가하는 것은 간단한 프로세스입니다.

Aspose.WordsProtect방법을 사용하여 문서를 변경으로부터 보호할 수 있습니다. 이 방법은 보안 기능이 아니며 문서를 암호화하지 않습니다.

다음 코드 예제에서는 문서에 암호 보호를 추가하는 방법을 보여 줍니다:

// 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를 사용하면StartEditableRangeEndEditableRange방법을 사용하여 문서에서 변경할 수 있는 부분을 표시할 수 있습니다.

다음 코드 예제에서는 전체 문서를 읽기 전용으로 표시하고 편집 가능한 영역을 지정하는 방법을 보여 줍니다:

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