📄 wordarea.cs
字号:
//***************************************************************************
//
// 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.ObjectModel;
using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;
namespace WordProtection
{
#region WordArea Class
/// <summary>
/// A base class for all protectable areas in a Word document.
/// </summary>
public abstract class WordArea
{
/// <summary>
/// The type of editor to use when protecting and
/// unprotecting a Word document.
/// </summary>
private static object editor = Word.WdEditorType.wdEditorEveryone;
#region Properties
protected abstract Word.Range Range { get; }
public abstract string Name { get; }
/// <summary>
/// Returns whether the document associated with the
/// area has changed since it was last saved.
/// </summary>
/// <value>
/// Returns true if the document has changed since
/// it was last saved, or false if it has not changed.
/// </value>
protected bool IsDocumentDirty
{
get
{
Debug.Assert(this.Range != null);
Debug.Assert(this.Range.Document != null);
return !this.Range.Document.Saved;
}
set
{
Debug.Assert(this.Range != null);
Debug.Assert(this.Range.Document != null);
this.Range.Document.Saved = !value;
}
}
/// <summary>
/// Returns whether the area represented is or is not
/// marked for protection.
/// </summary>
/// <value>
/// Returns true ig the area is marked for protection,
/// or false if it is not marked for protection.
/// </value>
public bool IsProtected
{
get
{
Debug.Assert(this.Range != null);
Debug.Assert(WordArea.editor != null);
try
{
// If there are no editors on the range,
// the area is marked for protection.
if (this.Range.Editors.Count > 0)
{
// Find the "everyone" editor for the range. If it is not
// found, the area is marked for protection.
Word.Editor editorEveryone =
this.Range.Editors.Item(ref editor);
return (editorEveryone == null);
}
else
{
return true;
}
}
catch (System.Runtime.InteropServices.COMException)
{
// The "everyone" editor could not be found,
// so this area is protected.
return true;
}
}
}
#endregion
#region Methods
/// <summary>
/// Protects the area represented by the object if it
/// is not already protected.
/// </summary>
public virtual void Protect()
{
Debug.Assert(this.Range != null);
// Only protect the region if it is not already protected.
if (!this.IsProtected)
{
// Get the "everyone" editor for this object and delete it.
Word.Editor editorEveryone = this.Range.Editors.Item(ref editor);
Debug.Assert(editorEveryone != null);
editorEveryone.Delete();
// Mark the document as modified since Word does not
// mark the document as modified on changes to protection.
this.IsDocumentDirty = true;
}
}
/// <summary>
/// Unprotects the area represented by the object if
/// it is protected.
/// </summary>
public virtual void Unprotect()
{
Debug.Assert(this.Range != null);
// Only unprotect the region if it is protected.
if (this.IsProtected)
{
this.Range.Editors.Add(ref editor);
// Mark the document as modified since Word does not
// mark the document as modified on changes to protection.
this.IsDocumentDirty = true;
}
}
#endregion
}
#endregion
#region BookmarkArea Class
/// <summary>
/// A class representing bookmarks in a Word
/// document that can be protected.
/// </summary>
public class BookmarkArea : WordArea
{
/// <summary>
/// The bookmark from the Word document that
/// this instance represents.
/// </summary>
private Word.Bookmark bookmarkMember;
/// <summary>
/// Creates a new instance of the BookmarkArea class.
/// </summary>
/// <param name="bookmark">
/// The Word bookmark that this
/// instance represents.
/// </param>
internal BookmarkArea(Word.Bookmark bookmark)
{
Debug.Assert(bookmark != null);
this.bookmarkMember = bookmark;
}
/// <summary>
/// Returns the Word bookmark that this
/// instance represents.
/// </summary>
/// <value>
/// Returns the Word bookmark that this
/// instance represents.
/// </value>
public Word.Bookmark Bookmark
{
get
{
return this.bookmarkMember;
}
}
#region Overrides
/// <summary>
/// The name of the bookmark.
/// </summary>
/// <value>Gets the name of the bookmark.</value>
public override string Name
{
get
{
Debug.Assert(this.bookmarkMember != null);
return this.bookmarkMember.Name;
}
}
/// <summary>
/// The range associated with the bookmark.
/// </summary>
/// <value>Gets the range of the bookmark.</value>
protected override Microsoft.Office.Interop.Word.Range Range
{
get
{
Debug.Assert(this.bookmarkMember != null);
return this.bookmarkMember.Range;
}
}
#endregion
}
#endregion
#region FormFieldArea Class
/// <summary>
/// A class representing form fields in a Word
/// document that can be protected.
/// </summary>
public class FormFieldArea : WordArea
{
/// <summary>
/// The form field from the Word document that
/// this instance represents.
/// </summary>
private Word.FormField formFieldMember;
/// <summary>
/// Creates a new instance of the FormFieldArea class.
/// </summary>
/// <param name="formField">
/// The Word form field that this
/// instance represents.
/// </param>
internal FormFieldArea(Word.FormField formField)
{
Debug.Assert(formField != null);
this.formFieldMember = formField;
}
/// <summary>
/// Returns the Word form field that this
/// instance represents.
/// </summary>
/// <value>
/// Returns the Word form field that this
/// instance represents.
/// </value>
public Word.FormField FormField
{
get
{
return this.formFieldMember;
}
}
#region Overrides
/// <summary>
/// The name of the form field.
/// </summary>
/// <value>Gets the name of the form field.</value>
public override string Name
{
get
{
Debug.Assert(this.formFieldMember != null);
return this.formFieldMember.Name;
}
}
/// <summary>
/// The range associated with the form field.
/// </summary>
/// <value>Gets the range of the form field.</value>
protected override Microsoft.Office.Interop.Word.Range Range
{
get
{
Debug.Assert(this.formFieldMember != null);
// Form fields are surrounded by bookmarks which should
// not be included. So move one character after the starting
// bookmark and one character before the ending bookmark to
// get the range for the form field.
object startIndex = this.formFieldMember.Range.Start + 1;
object endIndex = this.formFieldMember.Range.End - 1;
return this.formFieldMember.Range.Document.Range(
ref startIndex, ref endIndex);
}
}
#endregion
}
#endregion
#region XMLNodeArea Class
/// <summary>
/// A class representing XML nodes in a Word
/// document that can be protected.
/// </summary>
public class XmlNodeArea : WordArea
{
/// <summary>
/// The XML node from the Word document that
/// this instance represents.
/// </summary>
private Word.XMLNode nodeMember;
/// <summary>
/// The XmlNodeArea object that is the parent
/// of the current instance.
/// </summary>
private XmlNodeArea parentNode;
/// <summary>
/// A list of XmlNodeArea objects that are children
/// of the current instance.
/// </summary>
private List<XmlNodeArea> children;
/// <summary>
/// Creates a new instance of an XmlNodeArea with no parent.
/// </summary>
/// <param name="node">
/// The Word XML node that this instance represents.
/// </param>
internal XmlNodeArea(Word.XMLNode node)
{
Debug.Assert(node != null);
this.nodeMember = node;
this.children = new List<XmlNodeArea>();
}
/// <summary>
/// Creates a new instance of an XmlNodeArea with the given parent.
/// </summary>
/// <param name="node">
/// The Word XML node that this instance represents.
/// </param>
/// <param name="parent">
/// The XmlNodeArea instance that represents the parent of this instance.
/// </param>
internal XmlNodeArea(Word.XMLNode node, XmlNodeArea parent) : this(node)
{
Debug.Assert(parent != null);
this.parentNode = parent;
if (!this.parentNode.children.Contains(this))
this.parentNode.children.Add(this);
}
/// <summary>
/// Returns the Word XML Node that this instance represents.
/// </summary>
/// <value>
/// Returns the Word XML Node that this instance represents.
/// </value>
public Word.XMLNode XmlNode
{
get
{
return this.nodeMember;
}
}
/// <summary>
/// Returns the XmlNodeArea that is the parent of this instance.
/// Returns null if it is a root XML Node.
/// </summary>
/// <value>
/// Returns the XmlNodeArea that is the parent of this instance.
/// </value>
public XmlNodeArea Parent
{
get
{
return this.parentNode;
}
}
/// <summary>
/// Returns a collection of XMLNodeAreas that are child
/// areas of the current instance.
/// </summary>
/// <value>
/// Returns a collection of XMLNodeAreas that are child
/// areas of the current instance.
/// </value>
public ReadOnlyCollection<XmlNodeArea> ChildAreas
{
get
{
return new ReadOnlyCollection<XmlNodeArea>(this.children);
}
}
#region Overrides
/// <summary>
/// The name of the XML node.
/// </summary>
/// <value>Gets the name of the XML node.</value>
public override string Name
{
get
{
Debug.Assert(this.nodeMember != null);
return this.nodeMember.BaseName;
}
}
/// <summary>
/// The range associated with the XML node.
/// </summary>
/// <value>Gets the range of the XML node.</value>
protected override Microsoft.Office.Interop.Word.Range Range
{
get
{
Debug.Assert(this.nodeMember != null);
return this.nodeMember.Range;
}
}
/// <summary>
/// Protects the current instance of the node and
/// its parent if required.
/// </summary>
public override void Protect()
{
// Flag indicating if the parent node in unprotected.
bool isParentUnprotected = false;
if (this.parentNode != null)
{
isParentUnprotected = !this.parentNode.IsProtected;
}
// Protect the current instance.
base.Protect();
// Since XML nodes can share editors, protecting the current
// instance may have protected all of the sibling nodes as well.
// Therefore, if the parent was unprotected, then all of the
// children should be unprotected again to ensure their state
// did not change with the above call.
if (isParentUnprotected)
{
foreach (XmlNodeArea childArea in this.parentNode.children)
{
// Ensure that the current instance remains protected.
if (childArea != this)
{
childArea.Unprotect();
}
}
}
}
#endregion
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -