Home : Annotations : Storage : Manually read XMP data into an AnnotationController
Q10218 - HOWTO: Manually read XMP data into an AnnotationController

When loading XMP data into the AnnotateViewer, it will normally replace all existing annotations and layers in the control with those from the XMP.  The code in this article provides a simple method that will append the layers and annotations rather than replace them.

The process is simple:  Create an XmpFormatter and use its Deserialize method.  This will return whatever annotation, layer or layer collection that was saved.  You can then append these items into the existing AnnotationController any way you wish.

The method below takes the filename of the XMP data and the AnnotationController to add the annotations into.  The AnnotateViewer.Annotations property can be passed in for the controller argument.

[C#]
private void AddAnnotationData(string fileName, AnnotationController controller)
{
   Atalasoft.Annotate.Formatters.XmpFormatter xmp = new Atalasoft.Annotate.Formatters.XmpFormatter();
   object dataObject = null;
   using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
   {
      dataObject = xmp.Deserialize(fs);
   }
if (dataObject == null) return;
// Most of the time you will receive a LayerCollection. // In this case, we will simply add these layers into // the existing LayerCollection. LayerCollection layers = dataObject as LayerCollection; if (layers != null) { foreach (LayerAnnotation layer in layers) { controller.Layers.Add(layer); } return; }
// If you receive a single layer, simply add it. LayerAnnotation l = dataObject as LayerAnnotation; if (l != null) { controller.Layers.Add(l); return; }
// If only a single annotation is returned, add it // to the CurrentLayer (making sure a layer exists). AnnotationUI ann = dataObject as AnnotationUI; if (ann != null) { if (controller.CurrentLayer == null) controller.Layers.Add(new LayerAnnotation()); controller.CurrentLayer.Items.Add(ann); return; } }

 

[VB.NET]
Private Sub AddAnnotationData(ByVal fileName As String, ByVal controller As AnnotationController)
   Dim xmp As Atalasoft.Annotate.Formatters.XmpFormatter = New Atalasoft.Annotate.Formatters.XmpFormatter
   Dim dataObject As Object = Nothing
   Dim fs As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
   Try
        dataObject = xmp.Deserialize(fs)
   Finally
        fs.Close()
   End Try
If dataObject Is Nothing Then Return End If
' Most of the time you will receive a LayerCollection. ' In this case, we will simply add these layers into ' the existing LayerCollection. Dim layers As LayerCollection = CType(dataObject, LayerCollection) If Not layers Is Nothing Then Dim layer As LayerAnnotation For Each layer In layers controller.Layers.Add(layer) Next Return End If
' If you receive a single layer, simply add it. Dim l As LayerAnnotation = CType(dataObject, LayerAnnotation) If Not l Is Nothing Then controller.Layers.Add(l) Return End If
' If only a single annotation is returned, add it ' to the CurrentLayer (making sure a layer exists). Dim ann As AnnotationUI = CType(dataObject, AnnotationUI) If Not ann Is Nothing Then If controller.CurrentLayer Is Nothing Then controller.Layers.Add(New LayerAnnotation) End If controller.CurrentLayer.Items.Add(ann) Return End If End Sub
Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved