Search

Atalasoft Knowledge Base

HOWTO: Manually read XMP data into an AnnotationController

Administrator
DotImage

LEGACY CONTENT WARNING

Please note that this article refers to outdated methods of loading.. please consider just using

 
controller.Load(annotationData, AnnotationDataFormat.Xmp);

instead.

If you do need to load manually, please note that there are other types of annotation xmp that may come in .. such as LayerDataCollection, or LayerData .. this sample was created assuming certain types.. it is for reference only

Original Article

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

Original Article:
Q10218 - HOWTO: Manually read XMP data into an AnnotationController

Details
Last Modified: 7 Months Ago
Last Modified By: Tananda
Type: HOWTO
Article not rated yet.
Article has been viewed 568 times.
Options
Also In This Category