How to add annotations to xps document through the DocumentViewer control ?  
Author Message
beam





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Hi, I have one question to ask.
How to customize DocumentViewer control for supporting annotations to xps documents
Thanks!


Visual Studio 200845  
 
 
Heather Grantham





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

I'm working on a providing a detailed answer with code samples.  In the interim, here are a few pieces of info/advice.

Annotations currently only work with FixedDocument elements, not FixedDocumentSequence.  (I'll include a code sample on how to get the FixedDocument from an XPS document in my next post.)

Annotations are ran as a service with supporting commands, and the easiest way to hook up the commands is by adding a toolbar to DocumentViewer or modifying the DocumentViewer ContextMenu. (I'll include a sample of this, too.) --Then you just start up the service at document load time, and stop it when you are complete. (another code sample coming.)

Another UI option is to re-style DocumentViewer and set an Annotations-centric contextmenu on the ScrollViewer that is marked as the DocumentViewer.ContentHost. 

Hopefully my next post will be able to answer your questions more thoroughly.
-heather

 
 
beam





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Thank you !
I'm waiting your next post. It must be useful for me

 
 
Heather Grantham





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Sorry for the delayed response, Beam. I was doing a lot of experimenting. Smile
If I come up with anything else that might be cool, I'll be sure to re-post.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

HTH,

-heather


Here's what my Window.xaml.cs class looks like. 
This sample uses the bits available with the September CTP.


#region namespaces
using System;
using System.Net;
using System.IO;
using System.IO.Packaging;
using System.Windows;
using System.Windows.Annotations;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Serialization;
using System.Windows.Shapes;
using System.Windows.Xps.Packaging;
using System.Windows.Xps.Serialization;
#endregion namespaces

namespace
DVApp_Annotations
{
    /// <summary>
    /// Interaction logic for Window1.xaml
   
/// </summary>

    public partial class Window1 : Window

    {

        public Window1()

        {

            InitializeComponent();

        }

 

        public void LoadFile(string filename)

        {

            /*  

            // Code for loading an XPS document
            // into DocumentViewer:

            XpsDocument xpsDoc = new XpsDocument(
                             filename,
                             FileAccess.Read);
            FixedDocumentSequence fixedDocSequence =
                             xpsDoc.GetPackageRoot();

 

            // Print the written version of the doc.

            if (fixedDocSequence != null)

            {

                Viewer.Document = fixedDocSequence;

            }

            else
            {
                MessageBox.Show("No FixedDocSequence");
            }

            xpsDoc.Close();

            */

 

            // Code for loading an XPS document
            // into DocumentViewer with

            // Annotations Support.

            // Annotations currently only support
            // FixedDocuments,
not
            // FixedDocumentSequences. Grab the first
            // FixedDoc
from the XPS Document.

 

            _xpsDocumentPath = filename;

            _xpsPackage = GetPackage(_xpsDocumentPath);

 

            if (_xpsPackage != null)

            {

                // The name below is the name of the
                // fixed document in the zip file.

                Uri fixedDocumentUri = new Uri(
                           "/FixedDocument_0.xaml",
                            UriKind.Relative);

 

                // Grab the FixedDocument from 
                // the open package.

                FixedDocument fixedDocument = 
                    GetFixedDocument(fixedDocumentUri);


               
if (fixedDocument != null)

                {

                    // We now have the fixed Document,
                    // load it in the UI and

                    // start Annotations.

                    _documentViewer.Document =
                                fixedDocument;

 

                    StartAnnotations();

                }

                else
                {
                    MessageBox
.Show("Unable to get"
                      + "FixedDoc from Package."
); 
                }

            }

            else
            {
                 MessageBox.Show("Unable to get"
                      + "Package from file."
); 
            }

        }

 

        private Package GetPackage(string filename)

        {

            Package inputPackage = null;

 

            // filename must include the
            // full path like c:\dir/filename

            WebRequest webRequest = WebRequest.Create(filename);

            if (webRequest != null)

            {

                WebResponse webResponse = webRequest.GetResponse();

                if (webResponse != null)

                {

                    Stream packageStream = webResponse.GetResponseStream();

                    if (packageStream  != null)

                    {

                        // We now have the Package
                        // opened as a Stream, let's
                        // retreive a Package from
                        // that stream.

                        inputPackage = Package.Open(packageStream);

                    }

                }

            }

            return inputPackage;

        }

 

        private FixedDocument GetFixedDocument(
                                      Uri fixedDocUri)

        {

            FixedDocument fixedDocument = null;

 

            // Create a Uri for the FixedDoc inside
            // the package and
use that uri for 
            // setting our Parser context, so things
            // like fonts can load.

            Uri tempUri = new Uri(
                          _xpsDocumentPath,
                          UriKind.RelativeOrAbsolute);

            ParserContext parserContext = new ParserContext();

            parserContext.BaseUri = PackUriHelper.Create(tempUri);

           

            // Now retreive the fixed document.

            PackagePart fixedDocPart = _xpsPackage.GetPart(fixedDocUri);

            if (fixedDocPart != null)

            {

                object fixedObject = Parser.LoadXml(

                              fixedDocPart.GetStream(),
                             
parserContext);

                if (fixedObject != null)

                {

                    fixedDocument = fixedObject as FixedDocument;

                }

            }

            return fixedDocument;

        }

 

        private void StartAnnotations()

        {

            if (_annotService == null)

            {

                _annotService = new AnnotationService(_documentViewer);

            }

 

            if (_annotService.IsEnabled == true)

            {

                _annotService.Disable();

            }

 

            _annotStore = new FileStream(

                            _annotStorePath,

                            FileMode.OpenOrCreate,

                            FileAccess.ReadWrite);

 

            _annotService.Enable(_annotStore);

        }

 

        private void StopAnnotations()

        {

            if (_annotStore != null)

            {

                _annotStore.Flush();

                _annotStore.Close();

            }

            if (_annotService != null)

            {

                if (_annotService.IsEnabled)

                {

                    _annotService.Disable();

                    _annotService = null;

                }

            }

        }

 

        private void OnClosed(object sender,
                              EventArgs e)

        {

            StopAnnotations();

 

            if (_xpsPackage != null)

            {

                _xpsPackage.Close();

                _xpsPackage = null;

            }

        }

 

        private AnnotationService _annotService = null;

        private Stream _annotStore = null;

        private readonly string _annotStorePath =
;

 

        private string _xpsDocumentPath = string.Empty;

        private Package _xpsPackage = null;

    }

}

 

_____________

This posting is provided "AS IS" with no warranties, and confers no rights.

 


 
 
Heather Grantham





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Sorry. Forgot to include what my style looks like, so you can see how I hook up the Annotations UI.
-heather

Here's what my Application.xaml looks like:



< Mapping XmlNamespace="PresentationUI" ClrNamespace="System.Windows.Documents" Assembly="PresentationUI" >
< Mapping XmlNamespace="PresentationFramework" ClrNamespace="System.Windows.Annotations" Assembly="PresentationFramework" >
<Application x:Class="DVApp_Styling.MyApp"
    xmlns="//schemas.microsoft.com/winfx/avalon/2005" href="http://schemas.microsoft.com/winfx/avalon/2005">http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
    xmlns:ui="PresentationUI"
    xmlns:ann="PresentationFramework"
    Startup="AppStartup"
    >
 <Application.Resources>

  <!-- DocumentViwer ContextMenu Definition -->
  <ContextMenu  x:Key="ViewerContextMenu">
   <MenuItem Command="ApplicationCommands.Copy" />
   <MenuItem Mode="Separator" />
   <MenuItem Command="ComponentCommands.ScrollPageUp" Header="Previous Page" />
   <MenuItem Command="ComponentCommands.ScrollPageDown" Header="Next Page" />
   <MenuItem Mode="Separator" />
   <MenuItem Command="NavigationCommands.FirstPage" />
   <MenuItem Command="NavigationCommands.LastPage" />
   <MenuItem Mode="Separator" />
   <MenuItem Command="NavigationCommands.IncreaseZoom" />
   <MenuItem Command="NavigationCommands.DecreaseZoom" />
   <MenuItem Mode="Separator" />
   <MenuItem Header="Annotate">
    <MenuItem Command="ann:AnnotationService.CreateHighlightCommand" />
    <MenuItem Command="ann:AnnotationService.CreateTextStickyNoteCommand" />
    <MenuItem Command="ann:AnnotationService.CreateInkStickyNoteCommand" />
    <MenuItem Mode="Separator" />
    <MenuItem Command="ann:AnnotationService.ClearHighlightsCommand"/>
    <MenuItem Command="ann:AnnotationService.DeleteStickyNotesCommand" />
    <MenuItem Command="ann:AnnotationService.DeleteAnnotationsCommand"/>
   </MenuItem>
  </ContextMenu>


  <!-- DocumentViewer -->
  <Style TargetType="{x:Type DocumentViewer}">
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type DocumentViewer}" >
      <Grid>
       <ColumnDefinition />
       <RowDefinition Height="*" />
       <RowDefinition Height="Auto" />

       <ScrollViewer
        Grid.Row="0"
        Grid.Column="0"
        Background="LightBlue"
        CanContentScroll="true"
        HorizontalScrollBarVisibility="Auto"
        VerticalScrollBarVisibility="Auto"
        DocumentViewer.ContentHost="true"
        Focusable="true"
        ContextMenu="{DynamicResource ViewerContextMenu}"/>

       <ContentControl
        Grid.Row="1"
        Grid.Column="0"
        Name="DocumentViewerToolBar"
        Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type ui:PresentationUIStyleResources},
                                ResourceId=PUIDocumentViewerToolBarStyleKey}}"

                            />
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

 </Application.Resources>
</Application>

 


_____________< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

This posting is provided "AS IS" with no warranties, and confers no rights


 
 
beam





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Thank you, Heather.
If the xps file has multiple documents, for example FixedDocuments\FixedDocument_1.xaml, FixedDocuments\FixedDocument_2.xaml. the way you replied only supports one fixed document, Is there a way to annotate the full xps file thanks.


 
 
Heather Grantham





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Sorry, Beam, I didn't see this reply until now.

There are planned features to support the full xps document  (i.e., the FixedDocumentSequence) with Annotations, but it probably won't be available for another month or two.  (I'm hoping it will be available with the next CTP, but I can't confirm that.)

Until then, you are limited to supporting only one FixedDocument inside the FixedDocumentSequence at a time.

-heather


 
 
XPSUser





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Is this available now
 
 
Keith Boyd





PostPosted: Windows Presentation Foundation (WPF), How to add annotations to xps document through the DocumentViewer control ? Top

Yes, this functionality is available now. See the following SDK
references for assistance:

DocumentViewer with Annoations XML-Store Sample
http://windowssdk.msdn.microsoft.com/en-us/ms771348.aspx

Annotations Overview:
http://windowssdk.msdn.microsoft.com/en-us/ms748864.aspx

SDK Samples are not currently available online, so in order to get the
source for the sample above, you'll either need to install the offline
SDK, or download all of the WPF SDK samples from the team blog site:

http://blogs.msdn.com/wpfsdk/