Bagikan melalui


Annotations in WPF

Currently working on some Annotation stuff. So I thought it would be nice to post something on Annotations. It actually is pretty sinple to create/delete annotations- a couple of lines and its done.

 
viewer = new FlowDocumentPageViewer(); //viewer needs to be populated :)XmlStreamStore myStream = new XmlStreamStore(new FileStream("annotations.xml", FileMode.OpenOrCreate));service = new AnnotationService(viewer);service.Enable(myStream);AnnotationService.CreateTextStickyNoteCommand.Execute(null, viewer);
 

There are commands fro creating and deleting highlights as part of the AnnotationService. In the above code, the annotations are stored in the xml file.
 A more popular form is to store it in an xps file and Derek's blog explains it in more detail. The important part of it is a slight modification of the above.

 Package xpsPackage = PackageStore.GetPackage(_xpsDocument.Uri);Uri annotPartUri = PackUriHelper.CreatePartUri(new Uri("AnnotationStream", UriKind.Relative));PackagePart annotPart = null;try{// If annotation stream already exists load that.annotPart = xpsPackage.GetPart(annotPartUri);}catch (Exception){// If annotation stream doesn't exist create one.annotPart = xpsPackage.CreatePart(annotPartUri, "Annotations/Stream");}// Load annotations from the package.AnnotationStore store = new XmlStreamStore(annotPart.GetStream());service.Enable(store);

We look for the annotationPart in the Package. If it doesnt exist we create a new one. Pretty simple huh!! :).. and yeah, Remember to flush the store on closing the document so that the annotations get stored in the package..

Annotations has never been simpler :)  .......