-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnnotationsHelperXps - Copy (3).cs
244 lines (203 loc) · 9.48 KB
/
AnnotationsHelperXps - Copy (3).cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// DocViewerAnnotationsXps SDK Sample - AnnotationsHelperXps.cs
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Annotations.Storage;
using System.IO;
using System.Windows.Annotations;
using System.IO.Packaging;
using System.Windows.Controls;
using System.Windows.Documents;
namespace SDKSample
{
// ====================== class AnnotationsHelperXps ======================
/// <summary>
/// Provides helper methods for creating and storing user annotations
/// within an XML file stored in an XPS document container.</summary>
public class AnnotationsHelperXps
{
// ------------- AnnotationsHelperXps default constructor -------------
/// <summary>
/// Initializes a new instance of the class.</summary>
/// Initializes a new instance of the class.</summary>
public AnnotationsHelperXps()
{
}
// --------- AnnotationsHelperXps DocumentViewer constructor ----------
/// <summary>
/// Initializes a new instance of the AnnotationsHelperXps
/// class with a given target DocumentViewer.</summary>
public AnnotationsHelperXps(DocumentViewer dv)
{
_docViewer = dv;
}
// --------------------- DocViewer Getter/Setter ----------------------
/// <summary>
/// Sets and gets the DocumentViewer control that
/// contains the content to be annotated.</summary>
public DocumentViewer DocViewer
{
set
{ _docViewer = value; _docViewer = value;}
get
{ return _docViewer; }
}
// ---------------------------- SetSource -----------------------------
/// <summary>
/// Sets the source document packageURI
/// and FixedDocumentSequence URI.</summary>
/// <param name="packageUri">
/// The URI path and filename of the XPS document container.</param>
/// <param name="rootUri">
/// The URI of the root FixedDocumentSequence in the package.</param>
/// <remarks>
/// SetSource must be called first before
/// calling StartAnnotations().</remarks>
public void SetSource(Uri packageUri, Uri rootUri)
{
_packageUri = packageUri;
_rootUri = rootUri;
}
// ------------------------- StartAnnotations -------------------------
/// <summary>
/// Enables annotations processing and
/// displays viewable annotations.</summary>
/// <remarks>
/// SetSource must be called first before
/// calling StartAnnotations().</remarks>
public void StartAnnotations()
{
if (_docViewer==null)
throw new InvalidOperationException(
"Required DocumentViewer control has not been specified.");
if ((_packageUri==null) || (_rootUri==null))
throw new InvalidOperationException(
"Required SetSource() has not been called.");
// If there is no AnnotationService yet, create one.
if (_annotService == null)
{
// Get the annotations data stream from the XPS container.
_annotStream = GetAnnotationPart(_rootUri).GetStream();
// Create the AnnotationService.
_annotService = new AnnotationService(_docViewer);
// Enable the service with the annotation data stream.
_annotService.Enable(new XmlStreamStore(_annotStream));
}
// Else if the annotationService exists but is not enabled, enable it.
else if (!_annotService.IsEnabled)
_annotService.Enable(_annotService.Store);
}// end:StartAnnotations()
// -------------------------- StopAnnotations -------------------------
/// <summary>
/// Disables annotations processing and hides annotations.</summary>
public void StopAnnotations()
{
// If the AnnotationStore is active, flush and close it.
if ( (_annotService != null) && _annotService.IsEnabled )
{
_annotService.Store.Flush();
_annotStream.Flush();
_annotStream.Close();
}
// If the AnnotationService is active, shut it down.
if (_annotService != null)
{
if (_annotService.IsEnabled)
_annotService.Disable();
_annotService = null;
}
}// end:StopAnnotations()
// ------------------ GetAnnotationDocumentPaginator ------------------
/// <summary>
/// Returns a paginator for printing annotations.</summary>
/// <param name="fds">
/// The FixedDocumentSequence containing
/// the annotations to print.</param>
/// <returns>
/// An paginator for printing the document's annotations.</returns>
public AnnotationDocumentPaginator GetAnnotationDocumentPaginator(
FixedDocumentSequence fds)
{
return new AnnotationDocumentPaginator(
fds.DocumentPaginator, _annotService.Store);
}
// ------------------------ GetAnnotationsPart ------------------------
/// <summary>
/// Returns the part within an XPS document
/// for storing user annotations.</summary>
/// <param name="documentUri">
/// The URI of the FixedDocumentSequence
/// within the package to annotate.</param>
/// <returns>
/// The package part containing existing user annotations
/// and for storing new annotations.</returns>
/// <remarks>
/// If the document package does not as yet contain an annotations
/// part, a new empty one is created and returned.</remarks>
private PackagePart GetAnnotationPart(Uri documentUri)
{
// Open the document package.
Package package = PackageStore.GetPackage(_packageUri);
if (package == null)
{
throw new InvalidOperationException(
"The document package '" + _packageUri + "' does not exist.");
}
// Get the FixedDocumentSequence part from the package.
PackagePart docPart = package.GetPart(documentUri);
// Search through all the document relationships to find the
// annotations relationship part (or null, of there is none).
PackageRelationship annotRel = null;
foreach ( PackageRelationship rel in
docPart.GetRelationshipsByType(_annotRelsType) )
{
annotRel = rel;
}
// If annotations relationship does not exist, create a new
// annotations part along with a relationship part for it.
PackagePart annotPart = null;
if (annotRel == null)
{
// Create a new Annotations part.
annotPart = package.CreatePart(PackUriHelper.CreatePartUri(
new Uri(_annotFile, UriKind.Relative)), _annotContentType);
// Create a new relationship that points to the Annotations part.
docPart.CreateRelationship(
annotPart.Uri, TargetMode.Internal, _annotRelsType);
}
// If an annotations relationship exists,
// get the annotations part that it references.
else // if (annotRel != null)
{ // Get the Annotations part specified by the relationship.
annotPart = package.GetPart(annotRel.TargetUri);
if (annotPart == null)
{
throw new InvalidOperationException(
"The Annotations part referenced by the Annotations " +
"Relationship TargetURI '" + annotRel.TargetUri +
"' could not be found.");
}
}
return annotPart;
}// end:GetAnnotationPart()
#region private variables
// Application's DocumentViewer control.
private DocumentViewer _docViewer = null;
// URI path and filename of the document container.
private Uri _packageUri = null;
// URI of the document's FixedDocumentSequence part within the package.
private Uri _rootUri = null;
// URI of the annotations XML filestore part within the package.
private string _annotFile = "/Annotations.xml";
private AnnotationService _annotService = null; // AnnotationService
private Stream _annotStream = null; // Annotation IO stream
// Annotations Relationship part type
private readonly string _annotRelsType =
"http://schemas.microsoft.com/xps/2005/06/annotations";
// Annotations Content part type
private readonly string _annotContentType =
"application/vnd.ms-package.annotations+xml";
#endregion // private variables
}// end:class AnnotationsHelperXps
}// end:namespace SDKSample