public class RichEditControlService
{
private UserService userService;
private RangePermissionCollection rangePermissions = null;
public void DocumentProtect(ref RichEditControl richControl, List<DocumentRange> listRange)
{
richControl.ClearUndo();
richControl.RemoveService(typeof(IUserListService));
this.userService = new UserService();
this.userService.Update(this.GetUser());
richControl.AddService(typeof(IUserListService), this.userService);
richControl.Options.RangePermissions.Visibility = RichEditRangePermissionVisibility.Visible;
rangePermissions = richControl.Document.BeginUpdateRangePermissions();
rangePermissions.AddRange(CreateRangeWithPermissions(richControl.Document.Range, "", "sdt@example.com")); //设置全部权限
foreach (DocumentRange myRange in listRange)
{
this.rangePermissions.AddRange(CreateRangeWithPermissions(myRange, "", "alisa@example.com"));
}
List<DocumentRange> noEdit = GetDocumentRange(richControl, listRange);
foreach (DocumentRange myRange in noEdit)
{
this.rangePermissions.AddRange(CreateRangeWithPermissions(myRange, "", "dane55@example.com"));
}
richControl.Document.EndUpdateRangePermissions(this.rangePermissions);
richControl.Options.Authentication.EMail = "alisa@example.com"; //当前权限
richControl.Document.Protect("dane55");
}
/// <summary>
/// 写入编辑权限
/// </summary>
/// <param name="range"></param>
/// <param name="userGroup"></param>
/// <param name="emails"></param>
/// <returns></returns>
private static List<RangePermission> CreateRangeWithPermissions(DocumentRange range, string userGroup, params string[] emails)
{
List<RangePermission> rangeList = new List<RangePermission>();
foreach (string email in emails)
{
RangePermission rp = new RangePermission(range);
rp.Group = userGroup;
rp.UserName = email;
rangeList.Add(rp);
}
return rangeList;
}
/// <summary>
/// 默认用户
/// </summary>
/// <returns></returns>
private List<String> GetUser()
{
List<String> strList = new List<string>();
strList.Add("sdt@example.com");
strList.Add("alisa@example.com");
strList.Add("dane55@example.com");
return strList;
}
/// <summary>
/// 计算文档中不可编辑范围
/// </summary>
/// <param name="richControl"></param>
/// <param name="listRange"></param>
private List<DocumentRange> GetDocumentRange(RichEditControl richControl, List<DocumentRange> listRange)
{
List<DocumentRange> docRange = new List<DocumentRange>();
int startPosition = richControl.Document.Range.Start.ToInt(); //获取起始的文档位置
int endPosition = richControl.Document.Range.End.ToInt(); //获取最末的文档位置
int myStart = listRange[0].Start.ToInt();
int myEnd = listRange[0].End.ToInt();
int newStartEnd = 0;
int newEnd = 0;
if (startPosition != myStart)
{
DocumentRange editRange = richControl.Document.CreateRange(startPosition, myStart - 1);
docRange.Add(editRange);
}
for (int i = 1; i < listRange.Count - 2; i++)
{
newStartEnd = listRange[i].Start.ToInt();
newEnd = listRange[i].End.ToInt();
if ((myEnd + 1) != newStartEnd)
{
myStart = listRange[i + 1].Start.ToInt();
DocumentRange editRange = richControl.Document.CreateRange(myEnd + 1, myStart - 1);
}
myStart = listRange[i].Start.ToInt();
myEnd = listRange[i].End.ToInt();
}
newEnd = listRange[listRange.Count - 1].Start.ToInt();
if (endPosition > newEnd)
{
DocumentRange editRange = richControl.Document.CreateRange(newEnd + 1, endPosition);
docRange.Add(editRange);
}
return docRange;
}
}