DEV RichEditControl 文档保护

一个设置用户编辑Word文档指定区域的编辑权限。
   /// <summary>
    /// 权限用户表类
    /// </summary>
    public class UserService : IUserListService
    {
        List<string> users = new List<string>();

        public List<string> Users { get { return users; } }


        public IList<string> GetUsers()
        {
            return Users;
        }

        public void Update(List<String> userList)
        {
            this.users.Clear();
            this.users.AddRange(userList);
        }
    }
View Code
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;
        }
    }
View Code
 Dictionary<DocumentRange, int> dicRange = new Dictionary<DocumentRange, int>();
            OpenDocxLoadHelper.Load("DocumentProtection.docx", this.richEditControl1);
            DocumentPosition endPosition = this.richEditControl1.Document.Range.End; //获取最末的文档位置
            List<DocumentRange> rangeList = new List<DocumentRange>();
            DocumentRange myRange = richEditControl1.Document.CreateRange(1, 100);
            rangeList.Add(myRange);
            myRange = richEditControl1.Document.CreateRange(200, 100);
            rangeList.Add(myRange);
            myRange = richEditControl1.Document.CreateRange(301, 100);
            rangeList.Add(myRange);
            myRange = richEditControl1.Document.CreateRange(402, 100);
            rangeList.Add(myRange);
            myRange = richEditControl1.Document.CreateRange(503, 100);
            rangeList.Add(myRange);
            new RichEditControlService().DocumentProtect(ref this.richEditControl1, rangeList);
View Code
posted @ 2013-06-07 09:00  fujc2dev  阅读(746)  评论(0编辑  收藏  举报