📄 wordprotector.cs
字号:
// TODO: This cast seems to be temporary. Don't need it in Everett.
Word.DocumentClass docClassObject =
this.document as Word.DocumentClass;
Debug.Assert(docClassObject != null);
docClassObject.Close(ref saveOption, ref missing, ref missing);
//this.document.Close(ref saveOption, ref missing, ref missing);
}
/// <summary>
/// Protects the document with no password.
/// </summary>
public void Protect()
{
Debug.Assert(this.document != null);
this.Protect(String.Empty);
}
/// <summary>
/// Protects the document with the given password.
/// </summary>
/// <param name="password">
/// A string representing the password to
/// protect the document with.
/// </param>
public void Protect(string password)
{
Debug.Assert(this.document != null);
// Ensure that the password is not null.
if (password == null)
throw new ArgumentException("password");
// Ensure that the document is not already protected.
if (this.IsProtected)
throw new InvalidOperationException();
object missing = System.Reflection.Missing.Value;
object passwordObject = password as Object;
// Protect the document so that it can only be read and
// unprotected with the specified password.
this.document.Protect(Word.WdProtectionType.wdAllowOnlyReading,
ref missing, ref passwordObject, ref missing, ref missing);
// Raise the DocumentProtected event.
this.OnDocumentProtected(new EventArgs());
}
/// <summary>
/// Unprotects the document with no password.
/// </summary>
public void Unprotect()
{
Debug.Assert(this.document != null);
this.Unprotect(String.Empty);
}
/// <summary>
/// Unprotects the document with the given password.
/// </summary>
/// <param name="password">
/// The password to use to unprotect the document.
/// </param>
public void Unprotect(string password)
{
Debug.Assert(this.document != null);
// Ensure that some value was given for the password.
if (password == null)
throw new ArgumentException("password");
// Ensure that the document is not already unprotected.
if (!this.IsProtected)
throw new InvalidOperationException();
// Try to unprotect the document with the given password.
object passwordObject = password as Object;
this.document.Unprotect(ref passwordObject);
// Raise the DocumentUnprotected event.
this.OnDocumentUnprotected(new EventArgs());
}
/// <summary>
/// Protects all areas available in the document.
/// </summary>
public void ProtectAllAreas()
{
Debug.Assert(this.protectableAreas != null);
this.protectableAreas.ProtectAll();
}
/// <summary>
/// Unprotects all areas available in the document.
/// </summary>
public void UnprotectAllAreas()
{
Debug.Assert(this.protectableAreas != null);
this.protectableAreas.UnprotectAll();
}
/// <summary>
/// Refreshes the collection of protectable areas within the document.
/// </summary>
public void Refresh()
{
Debug.Assert(this.protectableAreas != null);
this.protectableAreas.Clear();
this.GetBookmarkAreas();
this.GetFormFieldAreas();
this.GetXmlNodeAreas();
}
#region Helper Methods
/// <summary>
/// Finds all of the bookmarks within the document, creates
/// BookmarkArea objects for them, and adds them to the collection
/// of protectable areas available within the document.
/// </summary>
protected void GetBookmarkAreas()
{
Debug.Assert(this.document != null);
Debug.Assert(this.protectableAreas != null);
// Loop through all of the bookmarks in the document.
for (int i = 1; i <= this.document.Bookmarks.Count; i++)
{
object index = i;
bool isFormFieldBookmark = false;
Word.Bookmark bookmark =
this.document.Bookmarks.get_Item(ref index);
// Determine if the bookmark is a holder for a form field by
// looping through all of the form fields in the bookmark.
IEnumerator enumerator =
bookmark.Range.FormFields.GetEnumerator();
while (enumerator.MoveNext())
{
Word.FormField field = enumerator.Current as Word.FormField;
Debug.Assert(field != null);
// Bookmarks and Form Fields need to have unique names.
// So if the bookmark and form field have the same name,
// this bookmark is a holder for the form field.
if (bookmark.Name == field.Name)
{
isFormFieldBookmark = true;
break;
}
}
// If the bookmark is a holder for a form field,
// it should not be added here to the collection as
// a bookmark.
if (!isFormFieldBookmark)
{
BookmarkArea bookmarkArea = new BookmarkArea(bookmark);
// Only add the new area if it is not already part
// of the collection.
if (!this.protectableAreas.Contains(bookmarkArea))
this.protectableAreas.Add(bookmarkArea);
}
}
}
/// <summary>
/// Finds all of the form fields within the document, creates
/// FormFieldArea objects for them, and adds them to the collection
/// of protectable areas available within the document.
/// </summary>
protected void GetFormFieldAreas()
{
Debug.Assert(this.document != null);
Debug.Assert(this.protectableAreas != null);
// Loop through all of the form fields in the document.
for (int i = 1; i <= this.document.FormFields.Count; i++)
{
object index = i;
Word.FormField field =
this.document.FormFields.get_Item(ref index);
// Add a new area to the collection if it does not
// already exist.
FormFieldArea formFieldArea = new FormFieldArea(field);
if (!this.protectableAreas.Contains(formFieldArea))
this.protectableAreas.Add(formFieldArea);
}
}
/// <summary>
/// Finds all of the XML nodes within the document, creates
/// XmlNodeArea objects for them, and adds them to the collection
/// of protectable areas available within the document.
/// </summary>
protected void GetXmlNodeAreas()
{
Debug.Assert(this.document != null);
Debug.Assert(this.protectableAreas != null);
// Loop through all of the XML nodes in the document.
for (int i = 1; i <= this.document.XMLNodes.Count; i++)
{
// If it is a root node, create a new area and populate
// its child nodes.
if (this.document.XMLNodes[i].ParentNode == null)
{
Word.XMLNode node = this.document.XMLNodes[i];
XmlNodeArea xmlNodeArea = new XmlNodeArea(node);
// Add a new area to the collection if it does not
// already exist.
if (!this.protectableAreas.Contains(xmlNodeArea))
this.protectableAreas.Add(xmlNodeArea);
// Populate the child nodes.
this.GetXmlNodeAreas(xmlNodeArea);
}
}
}
/// <summary>
/// Finds all of the child XML nodes of the given node area, creates
/// XmlNodeArea objects for them, and adds them to the collection
/// of protectable areas available within the document.
/// </summary>
/// <param name="parentXMLNodeArea"></param>
private void GetXmlNodeAreas(XmlNodeArea parentXMLNodeArea)
{
Debug.Assert(parentXMLNodeArea != null);
// Loop through all the child nodes of the given parent node.
for (int i = 1; i <= parentXMLNodeArea.XmlNode.ChildNodes.Count; i++)
{
Word.XMLNode childNode = parentXMLNodeArea.XmlNode.ChildNodes[i];
XmlNodeArea childNodeArea =
new XmlNodeArea(childNode, parentXMLNodeArea);
// Add a new area to the collection if it does not
// already exist.
if (!this.protectableAreas.Contains(childNodeArea))
this.protectableAreas.Add(childNodeArea);
// Populate the child node areas.
this.GetXmlNodeAreas(childNodeArea);
}
}
#endregion
#region Event Raising Methods
protected virtual void OnDocumentProtected(EventArgs eventArgs)
{
Debug.Assert(eventArgs != null);
if (this.DocumentProtected != null)
{
DocumentProtected(this, eventArgs);
}
}
protected virtual void OnDocumentUnprotected(EventArgs eventArgs)
{
Debug.Assert(eventArgs != null);
if (this.DocumentUnprotected != null)
{
DocumentUnprotected(this, eventArgs);
}
}
#endregion
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -