注釈の概要

紙に印刷されたドキュメントにメモやコメントを書き込むことは、当然のように行われる一般的な行為です。 これらのメモやコメントは、後で参照するために、情報に目印を付けたり、関心のある項目を強調するためにドキュメントに追加する "注釈" です。 印刷されたドキュメントにメモを書き込むことは簡単で一般的ですが、電子ドキュメントに個人的なコメントを追加する機能は、利用できるとしても、通常は非常に限られています。

ここでは、いくつかの一般的な種類の注釈、特に付箋と強調表示について説明し、Microsoft Annotations Framework で Windows Presentation Foundation (WPF) ドキュメント表示コントロールを使用することにより、アプリケーションでこれらの種類の注釈を使用できるようにする方法を示します。 注釈をサポートする WPF ドキュメント表示コントロールには、FlowDocumentReaderFlowDocumentScrollViewer などがあり、また、DocumentViewerBase から派生した DocumentViewer や FlowDocumentPageViewer などのコントロールも含まれます。

このトピックは、次のセクションで構成されています。

  • 付箋
  • 強調表示
  • データ アンカー
  • 注釈と注釈先オブジェクトの対応付け
  • 関連トピック

付箋

通常の付箋の場合は、色の付いた小さな用紙に情報を書き込み、それをドキュメントに貼り付けます。 デジタル付箋は、電子ドキュメントに対する同様の機能を提供しますが、入力したテキスト、手書きのメモ (たとえば Tablet PC の "インク" ストローク)、Web リンクなど、さまざまな種類のコンテンツを格納できる柔軟性も備えています。

強調表示、テキスト付箋、およびインク付箋による注釈の例を次の図に示します。

強調表示、テキストとインク付箋注釈。

アプリケーションで注釈のサポートを有効にするために使用できる方法を次の例に示します。

        ' ------------------------ StartAnnotations --------------------------
        ''' <summary>
        '''   Enables annotations and displays all that are viewable.</summary>
        Private Sub StartAnnotations()
            ' If there is no AnnotationService yet, create one.
            If _annotService Is Nothing Then
                ' docViewer is a document viewing control named in Window1.xaml.
                _annotService = New AnnotationService(docViewer)
            End If

            ' If the AnnotationService is currently enabled, disable it.
            If _annotService.IsEnabled = True Then
                _annotService.Disable()
            End If

            ' 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 Sub ' end:StartAnnotations()
// ------------------------ 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 を宣言するために使用できる Extensible Application Markup Language (XAML) を次の例に示します。

<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 &amp; Notes" />
  </ContextMenu>
</DocumentViewer.ContextMenu>

データ アンカー

Annotations Framework では、注釈は、表示ビュー上の位置に対してだけでなく、ユーザーが選択したデータに対してもバインドされます。 そのため、ドキュメント ビューが変更された場合 (ユーザーが表示ウィンドウをスクロールまたはサイズ変更した場合など) でも、注釈はそれがバインドされているデータ選択を保持します。 たとえば、次の図は、ユーザーがテキストを選択して行った注釈を示しています。 ドキュメント ビューが変更 (スクロール、サイズ変更、スケーリング、または移動) されると、強調表示による注釈は、元のデータ選択と共に移動します。

注釈データ固定

注釈と注釈先オブジェクトの対応付け

注釈と、それに応じた注釈先オブジェクトとを対応付けることができます。 たとえば、コメント ペインを持つ簡単なドキュメント リーダー アプリケーションがあるとします。 そのコメント ペインは、そのドキュメントにアンカーされている注釈のリストのテキストを表示するリスト ボックスです。 ユーザーがリスト ボックスの項目を選択すると、対応する注釈オブジェクトがアンカーされているドキュメント内の段落が表示されます。

このようなコメント ペインとして使用するリスト ボックスのイベント ハンドラーを実装する方法を次の例に示します。

        Private Sub annotationsListBox_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

            Dim comment As Annotation = TryCast((TryCast(sender, ListBox)).SelectedItem, Annotation)
            If comment IsNot Nothing Then
                ' service is an AnnotationService object
                ' comment is an Annotation object
                info = AnnotationHelper.GetAnchorInfo(Me.service, comment)
                Dim resolvedAnchor As TextAnchor = TryCast(info.ResolvedAnchor, TextAnchor)
                Dim textPointer As TextPointer = CType(resolvedAnchor.BoundingStart, TextPointer)
                textPointer.Paragraph.BringIntoView()
            End If
        End Sub
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();
    }
}

別のサンプル シナリオとしては、ドキュメント リーダー間で注釈および付箋を電子メールで交換できるアプリケーションが考えられます。 この機能があるアプリケーションでは、交換した注釈を含むページにリーダーを移動できます。

参照

処理手順

方法 : MenuItem にコマンドを追加する

参照

DocumentViewerBase

DocumentViewer

FlowDocumentPageViewer

FlowDocumentScrollViewer

FlowDocumentReader

IAnchorInfo

概念

注釈スキーマ

ContextMenu の概要

コマンド実行の概要

フロー ドキュメントの概要