-
Notifications
You must be signed in to change notification settings - Fork 9
/
ProtectionActions.cs
137 lines (114 loc) · 5.87 KB
/
ProtectionActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
namespace RichEditDocumentServerAPIExample.CodeExamples
{
public static class ProtectionActions
{
public static Action<RichEditDocumentServer> ProtectDocumentAction = ProtectDocument;
public static Action<RichEditDocumentServer> UnprotectDocumentAction = UnprotectDocument;
public static Action<RichEditDocumentServer> CreateRangePermissionsAction = CreateRangePermissions;
static void ProtectDocument(RichEditDocumentServer wordProcessor)
{
UnprotectResultingDocument(wordProcessor);
#region #ProtectDocument
// Load a document from a file.
wordProcessor.LoadDocument("Documents//Grimm.docx",DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Check whether the document is protected.
if (!document.IsDocumentProtected)
{
// Protect the document with a password.
document.Protect("123", DocumentProtectionType.ReadOnly);
// Create a comment related to the first paragraph.
document.Comments.Create(document.Paragraphs[0].Range, "Admin");
// Access the comment content.
SubDocument commentDocument = document.Comments[0].BeginUpdate();
// Specify the comment text to indicate that the document is protected.
commentDocument.InsertText(commentDocument.CreatePosition(0),
"Document is protected with a password.\nYou cannot modify the document until protection is removed.");
// Finalize to edit the comment.
commentDocument.EndUpdate();
// Save and open the protected document.
wordProcessor.SaveDocument("ResultProtected.docx", DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("ResultProtected.docx");
}
#endregion #ProtectDocument
}
static void UnprotectDocument(RichEditDocumentServer wordProcessor)
{
#region #UnprotectDocument
// Load a document from a file.
wordProcessor.LoadDocument("Documents//Grimm_Protected.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Check whether the document is protected.
if (document.IsDocumentProtected == true)
{
// Unprotect the document.
document.Unprotect();
// Create a comment related to the first paragraph.
document.Comments.Create(document.Paragraphs[0].Range,"Admin");
// Access the comment content.
SubDocument commentDocument = document.Comments[0].BeginUpdate();
// Specify the comment text to indicate that the document is unprotected.
commentDocument.InsertText(commentDocument.CreatePosition(0),
"Document is unprotected. You can modify the document according to your requests.");
// Finalize to edit the comment.
commentDocument.EndUpdate();
// Save and open the protected document.
wordProcessor.SaveDocument("ResultUnrotected.docx", DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("ResultUnprotected.docx");
}
#endregion #UnprotectDocument
}
static void CreateRangePermissions(RichEditDocumentServer wordProcessor)
{
UnprotectResultingDocument(wordProcessor);
#region #CreateRangePermissions
// Load a document from a file.
wordProcessor.LoadDocument("Documents//Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Access the range permissions collection.
RangePermissionCollection rangePermissions = document.BeginUpdateRangePermissions();
if (document.Paragraphs.Count > 3)
{
// Specify the group of users and the user that are allowed to edit the document range.
RangePermission rp = rangePermissions.CreateRangePermission(document.Paragraphs[3].Range);
rp.Group = "Administrators";
rp.UserName = "admin@somecompany.com";
rangePermissions.Add(rp);
}
// Finalize to update the range permissions collection.
document.EndUpdateRangePermissions(rangePermissions);
// Protect the document with a password.
document.Protect("123");
// Save and open the protected document.
wordProcessor.SaveDocument("ResultProtected.docx", DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("ResultProtected.docx");
#endregion #CreateRangePermissions
}
static void UnprotectResultingDocument(RichEditDocumentServer wordProcessor)
{
try
{
// Load a document from a file.
wordProcessor.LoadDocument("ResultProtected.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
if (document.IsDocumentProtected == true)
{
// Unprotect the document.
document.Unprotect();
}
}
catch { }
}
}
}