#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;
}
}