DocumentViewer with Annotations XPS-Store Sample

DocViewerAnnotationsXps shows how to read an XML Paper Specification (XPS) document into a DocumentViewer control and then enable support for adding user-created text note, ink note, and highlight annotations. In this example, user annotations are stored in an XML file as a part within the XPS document.   

When executed, the sample displays a menu bar with a "File | Open..." option. When "Open..." is selected, a "File Open..." dialog displays allowing the user to browse and choose an XpsDocument file to open (a sample XPS document file is provided).

When a document is opened, its content is displayed in the DocumentViewer of the main application window. Within the DocumentViewer control you can select a block of text for adding an annotation. Right-clicking on the selected text displays the Annotations context menu allowing you to choose the type of annotation to create: "Highlight", "Text Note", or "Ink Note" annotation. Once the annotation type is selected the annotation highlight or note form displays on the DocumentViewer control. You can add personal comments to text note or ink note annotations. In the DocViewerAnnotationsXps sample, user annotations are stored in an XML file as a PackagePart within the XPS document itself. Using this design, user-annotations are kept together as part of the document package.

The DocViewerAnnotationsXps sample source code consists of five files:

  • MyApp.xaml - Extensible Application Markup Language (XAML) resources for the application (none in this sample).

  • MyApp.xaml.cs - Code-behind for creating and displaying the application window.

  • Window1.xaml - XAML declarations defining the elements of the application window: main menu, menu commands, DocumentViewer control, DocumentViewer annotations context menu, and context menu commands.

  • Window1.xaml.cs - Code-behind supporting the user interface for the main menu and context menu commands.

  • AnnotationsHelperXps.cs - Annotations helper class to support user annotations stored as part of an XPS document.

A project file and sample XPS document are also provided. To examine the contents of the XPS document and the user annotations XML file contained within, simply change the document file extension from .xps to .zip and then open the file with the Zip utility.

This sample demonstrates a specific feature of the Windows Presentation Foundation and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:

Accessibility - Accessibility Best Practices

Security - Windows Presentation Foundation Security

Localization - WPF Globalization and Localization Overview

Download sample

  • Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.

  • Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.

  • To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.

  • To build the sample in Microsoft Visual Studio, load the sample solution or project file and then press CTRL+SHIFT+B.

  • To run the compiled sample with debugging in Visual Studio, press F5.

  • To run the sample from a CMD Shell command window, execute the .exe file located in the source folder of the sample.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft