⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordprotector.cs

📁 文件保护 文件保护 文件保护
💻 CS
📖 第 1 页 / 共 2 页
字号:
//***************************************************************************
//
//         Copyright (c) Microsoft Corporation. All rights reserved.
//
//    This code sample is provided "AS IS" without warranty of any kind.
//
//***************************************************************************
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;

namespace WordProtection
{
    #region ProtectableAreaCollection Class
    /// <summary>
    /// Serves as a class representing a collection of WordArea objects.
    /// </summary>
    public class ProtectableAreaCollection : List<WordArea>
    {
        /// <summary>
        /// Protects all of the WordArea objects in the collection.
        /// </summary>
        public void ProtectAll()
        {
            foreach (WordArea area in this)
            {
                area.Protect();
            }
        }

        /// <summary>
        /// Unprotects all of the WordArea objects in the collection.
        /// </summary>
        public void UnprotectAll()
        {
            foreach (WordArea area in this)
            {
                area.Unprotect();
            }
        }
    }
    #endregion
         
    #region WordProtector Class
    /// <summary>
    /// Serves as a class that exposes document-level methods and properties
    /// for protecting, unprotecting, and saving Word documents.
    /// </summary>
    public class WordProtector
    {
        /// <summary>
        /// The document that this instance of the WordProtector acts upon.
        /// </summary>
        private Word.Document document;

        /// <summary>
        /// A collection of WordAreas within the document that can
        /// be protected and unprotected.
        /// </summary>
        private ProtectableAreaCollection protectableAreas;

        /// <summary>
        /// Event handler for when the document state is changed
        /// from unprotected to protected.
        /// </summary>
        public event System.EventHandler DocumentProtected;

        /// <summary>
        /// Event handler for when the document state is changed
        /// from protected to unprotected.
        /// </summary>
        public event System.EventHandler DocumentUnprotected;

        /// <summary>
        /// Creates a new instance of a WordProtector for the specified document.
        /// </summary>
        /// <param name="document">
        /// The document to create an instance of the WordProtector for.
        /// </param>
        public WordProtector(Word.Document document)
        {
            // The document cannot be null.  If it is, throw 
            // an ArgumentException back to the caller.
            if (document == null)
                throw new ArgumentException("document");

            this.document = document;
            this.protectableAreas = new ProtectableAreaCollection();

            // Refresh the collection of protectable areas within the document.
            this.Refresh();
        }

        #region Properties
        /// <summary>
        /// Returns the name of the document.
        /// </summary>
        /// <value>Returns the name of the document.</value>
        public string Name
        {
            get
            {
                Debug.Assert(this.document != null);
                return this.document.Name;
            }
        }

        /// <summary>
        /// Returns the current Word document.
        /// </summary>
        /// <value>Returns the current Word document.</value>
        public Word.Document Document
        {
            get
            {
                return this.document;
            }
        }

        /// <summary>
        /// Returns the fullname of the document.
        /// </summary>
        /// <value>Returns the fullname of the document.</value>
        public string FullName
        {
            get
            {
                Debug.Assert(this.document != null);
                return this.document.FullName;
            }
        }

        /// <summary>
        /// Returns whether the document is locked using Word protection.
        /// </summary>
        /// <value>
        /// Returns true if the document is locked using Word protection,
        /// or false if it is not.
        /// </value>
        public bool IsProtected
        {
            get
            {
                Debug.Assert(this.document != null);
                return !(this.document.ProtectionType == 
                    Word.WdProtectionType.wdNoProtection);
            }
        }

        /// <summary>
        /// Returns whether changes have been made to the document since
        /// it was last saved.
        /// </summary>
        /// <value>
        /// Returns true if changes have been made to the document since
        /// it was last saved, or false if no changes have been made.
        /// </value>
        public bool IsDirty
        {
            get
            {
                Debug.Assert(this.document != null);
                return !(this.document.Saved);
            }
        }

        /// <summary>
        /// Returns a collection of areas that can be 
        /// excluded from document protection.
        /// </summary>
        /// <value>
        /// Returns a collection of areas that can be 
        /// excluded from document protection.
        /// </value>
        public ProtectableAreaCollection ProtectableAreas
        {
            get
            {
                return this.protectableAreas;
            }
        }
        #endregion

        /// <summary>
        /// Saves the document in its current state.
        /// </summary>
        public void Save()
        {
            Debug.Assert(this.document != null);
            this.document.Save();
        }

        /// <summary>
        /// Saves the document with the specified file name.
        /// </summary>
        /// <param name="filename">
        /// A string representing the full path to save the file to.
        /// </param>
        public void SaveAs(string filename)
        {
            Debug.Assert(this.document != null);

            // Make sure that the filename is reasonable.
            if (String.IsNullOrEmpty(filename))
                throw new ArgumentException("filename");

            // Save the document with the given file name.
            object missing = System.Reflection.Missing.Value;
            object filenameObject = filename as Object;

            this.document.SaveAs(ref filenameObject, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing,
                ref missing);
        }

        /// <summary>
        /// Returns the document to its last saved state.
        /// </summary>
        /// <returns>A reference to the document that was refreshed.</returns>
        public Word.Document Revert()
        {
            Debug.Assert(this.document != null);

            // Initialize the values to call the Word object model with.
            object documentName = this.document.FullName;
            object missing = System.Reflection.Missing.Value;
            Word.Application application = this.document.Application;

            // Close the current instance of the document 
            // without saving changes.
            this.Close();

            // Reopen the document that was just closed.
            this.document = application.Documents.Open(ref documentName,
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);
            Debug.Assert(this.document != null);

            // Refresh the collection of protectable areas within the document.
            this.Refresh();
            return this.document;
        }

        /// <summary>
        /// Closes the document without saving changes.
        /// </summary>
        public void Close()
        {
            Debug.Assert(this.document != null);
            this.Close(false);
        }

        /// <summary>
        /// Closes the document saving changes if specified via saveChanges.
        /// </summary>
        /// <param name="saveChanges">
        /// Specifies if changes to the document should be 
        /// saved before closing the document.
        /// </param>
        public void Close(bool saveChanges)
        {
            Debug.Assert(this.document != null);

            object missing = System.Reflection.Missing.Value;
            object saveOption = null;

            // Determine the Word save option for the specified Boolean value.
            if (saveChanges)
                saveOption = Word.WdSaveOptions.wdSaveChanges;
            else
                saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -