批注概述
更新:2007 年 11 月
在书面文档上写说明或注释是一件我们几乎认为理所当然的平常事。这些说明或注释是在文档中添加的“批注”,用于标注信息或突出显示重要项,以便于将来参考。尽管在打印文档上写注释很简单也很平常,但是在电子文档上添加个人注释却通常有很多的限制,且并不是在所有电子文档上都能添加个人注释。
本主题介绍了几种常见类型的批注,重点介绍便笺和突出显示,并举例说明 Microsoft Annotations Framework 如何通过 Windows Presentation Foundation (WPF) 文档查看控件推动这些类型的批注在应用程序中的使用。支持批注的 WPF 文档查看控件包括 FlowDocumentReader 和 FlowDocumentScrollViewer,以及从 DocumentViewerBase 派生的控件,如 DocumentViewer 和 FlowDocumentPageViewer。
本主题包括下列各节。
- 便笺
- 突出显示
- 数据创作
- 将批注与所批注的对象匹配
- 相关主题
便笺
典型的便笺包含在小块的彩色纸上书写的信息,之后这张彩色纸将“粘贴”到文档上。数字便笺为电子文档提供类似的功能,但灵活性更高,可包括许多其他类型的内容,如键入文本、手写注释(如 Tablet PC“墨迹”笔划)或 Web 链接。
下图显示了突出显示、文本便笺以及墨迹便笺批注的一些示例。
下面的示例演示了可用来在应用程序中启用批注支持的方法。 有关完整示例,请参见带有批注 XML 存储区的 DocumentViewer 的示例或带有批注 XPS 存储区的 DocumentViewer 的示例。
// ------------------------ StartAnnotations --------------------------
/// <summary>
/// Enables annotations and displays all that are viewable.</summary>
private void StartAnnotations()
{
// If there is no AnnotationService yet, create one.
if (_annotService == null)
// docViewer is a document viewing control named in Window1.xaml.
_annotService = new AnnotationService(docViewer);
// If the AnnotationService is currently enabled, disable it.
if (_annotService.IsEnabled == true)
_annotService.Disable();
// Open a stream to the file for storing annotations.
_annotStream = new FileStream(
_annotStorePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// Create an AnnotationStore using the file stream.
_annotStore = new XmlStreamStore(_annotStream);
// Enable the AnnotationService using the new store.
_annotService.Enable(_annotStore);
}// end:StartAnnotations()
突出显示
当人们在书面文档上作标记时,往往使用创造性的方法来吸引他人对某些重要项的关注,这些方法有加下划线、突出显示、将句子中的个别字圈出来或者在空白的地方绘制标记或写注释。 Microsoft Annotations Framework 中的突出显示批注具有类似的功能,用于标记显示在 WPF 文档查看控件中的信息。
下图显示了突出显示批注的示例。
用户通常以如下方法创建批注:首先选择一些文本或者关注的某一项,然后右击以显示批注选项的 ContextMenu。 下面的示例演示了可用来声明包含路由命令的 ContextMenu 的可扩展应用程序标记语言 (XAML),用户可以访问这些命令来创建和管理批注。 有关完整示例,请参见带有批注 XPS 存储区的 DocumentViewer 的示例。
<DocumentViewer.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Copy" />
<Separator />
<!-- Add a Highlight annotation to a user selection. -->
<MenuItem Command="ann:AnnotationService.CreateHighlightCommand"
Header="Add Highlight" />
<!-- Add a Text Note annotation to a user selection. -->
<MenuItem Command="ann:AnnotationService.CreateTextStickyNoteCommand"
Header="Add Text Note" />
<!-- Add an Ink Note annotation to a user selection. -->
<MenuItem Command="ann:AnnotationService.CreateInkStickyNoteCommand"
Header="Add Ink Note" />
<Separator />
<!-- Remove Highlights from a user selection. -->
<MenuItem Command="ann:AnnotationService.ClearHighlightsCommand"
Header="Remove Highlights" />
<!-- Remove Text Notes and Ink Notes from a user selection. -->
<MenuItem Command="ann:AnnotationService.DeleteStickyNotesCommand"
Header="Remove Notes" />
<!-- Remove Highlights, Text Notes, Ink Notes from a selection. -->
<MenuItem Command="ann:AnnotationService.DeleteAnnotationsCommand"
Header="Remove Highlights & Notes" />
</ContextMenu>
</DocumentViewer.ContextMenu>
数据创作
Annotations Framework 将批注绑定到用户选择的数据,而不仅仅是绑定到显示视图上的某个位置。因此,如果文档视图更改,例如,当用户滚动显示窗口或者调整其大小时,批注将仍然与所绑定到的选择数据捆绑在一起。例如,下图显示了用户可以在选择文本上做的批注。当文档视图更改(滚动、调整大小、缩放或者移动)时,突出显示批注将与最初选择的数据一起移动。
将批注与所批注的对象匹配
可以将批注与对应的批注对象匹配。以一个具有注释窗格的简单文档读取器应用程序为例。注释窗格可能是一个列表框,用于显示锚定到文档的批注列表中的文本。如果用户在列表框中选择一项,则应用程序将显示相应的批注对象所锚定到的文档段落。
下面的示例演示如何实现充当注释窗格的列表框的事件处理程序。
void annotationsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Annotation comment = (sender as ListBox).SelectedItem as Annotation;
if (comment != null)
{
// IAnchorInfo info;
// service is an AnnotationService object
// comment is an Annotation object
info = AnnotationHelper.GetAnchorInfo(this.service, comment);
TextAnchor resolvedAnchor = info.ResolvedAnchor as TextAnchor;
TextPointer textPointer = (TextPointer)resolvedAnchor.BoundingStart;
textPointer.Paragraph.BringIntoView();
}
}
另一示例方案涉及通过电子邮件实现文档读取器之间批注和便笺的交换的应用程序。此功能使这些应用程序可以将读取器导航到包含要交换的批注的页面。
请参见
任务
带有批注 XPS 存储区的 DocumentViewer 的示例
带有批注 XML 存储区的 DocumentViewer 的示例