Converting labels to geodatabase annotation for a single layer


About converting labels to geodatabase annotation for a single layer

This topic shows how to use the ConvertLabelsToAnnotation class to change a label for a single layer to geodatabase annotation. The ConvertLabelsToAnnotation class is a coarse-grained object that encapsulates the logic needed to perform this conversion. This class works with maps labeled with Maplex or the standard label engine.
The following code examples contain a Boolean option for making the annotation feature linked. Feature-linked annotation can only be created with an ArcGIS for Desktop Standard or Advanced license level.
The map reference passed into the function must be activated on a window. Check the IsMapActivated property on IActiveView to determine if the map is activated. Typically, the map is activated unless it is opened via MapDocument or MapReader.
[C#]
static void ConvertLabelsToGDBAnnotationSingleLayer(IMap pMap, int layerIndex, bool
    featureLinked)
{

    IConvertLabelsToAnnotation pConvertLabelsToAnnotation = new
        ConvertLabelsToAnnotationClass();
    ITrackCancel pTrackCancel = new CancelTrackerClass();
    //Change global level options for the conversion by sending in different parameters to the next line.
    pConvertLabelsToAnnotation.Initialize(pMap,
        esriAnnotationStorageType.esriDatabaseAnnotation,
        esriLabelWhichFeatures.esriAllFeatures, true, pTrackCancel, null);

    ILayer pLayer = pMap.get_Layer(layerIndex);
    IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer;
    if (pGeoFeatureLayer != null)
    {
        IFeatureClass pFeatureClass = pGeoFeatureLayer.FeatureClass;
        IDataset pDataset = pFeatureClass as IDataset;

        IFeatureWorkspace pFeatureWorkspace = pDataset.Workspace as
            IFeatureWorkspace;
        //Add the layer information to the converter object. Specify the parameters of the output annotation feature class here as well.
        pConvertLabelsToAnnotation.AddFeatureLayer(pGeoFeatureLayer,
            pGeoFeatureLayer.Name + "_Anno", pFeatureWorkspace,
            pFeatureClass.FeatureDataset, featureLinked, false, false, true, true, 
            "");

        //Do the conversion.
        pConvertLabelsToAnnotation.ConvertLabels();
        IEnumLayer pEnumLayer = pConvertLabelsToAnnotation.AnnoLayers;
        //Turn off labeling for the layer converted.
        pGeoFeatureLayer.DisplayAnnotation = false;

        //Add the result annotation layer to the map.
        pMap.AddLayers(pEnumLayer, true);

        //Refresh the map to update the display.
        IActiveView pActiveView = pMap as IActiveView;
        pActiveView.Refresh();
    }
}
[VB.NET]
Shared Sub ConvertLabelsToGDBAnnotationSingleLayer(ByVal pMap As IMap, ByVal layerIndex As Integer, ByVal featureLinked As Boolean)

Dim pConvertLabelsToAnnotation As IConvertLabelsToAnnotation = New ConvertLabelsToAnnotationClass()
Dim pTrackCancel As ITrackCancel = New CancelTrackerClass()
'Change global level options for the conversion by sending in different parameters to the next line.
pConvertLabelsToAnnotation.Initialize(pMap, esriAnnotationStorageType.esriDatabaseAnnotation, _
                                      esriLabelWhichFeatures.esriAllFeatures, True, pTrackCancel, Nothing)

Dim pLayer As ILayer = pMap.Layer(layerIndex)
Dim pGeoFeatureLayer As IGeoFeatureLayer = pLayer

If Not pGeoFeatureLayer Is Nothing Then
    Dim pFeatureClass As IFeatureClass = pGeoFeatureLayer.FeatureClass
    Dim pDataset As IDataset = pFeatureClass
    
    Dim pFeatureWorkspace As IFeatureWorkspace = pDataset.Workspace
    'Add the layer information to the converter object. Specify the parameters of the output annotation feature class here as well.
    pConvertLabelsToAnnotation.AddFeatureLayer(pGeoFeatureLayer, pGeoFeatureLayer.Name & "_Anno", _
                                               pFeatureWorkspace, pFeatureClass.FeatureDataset, _
                                               featureLinked, False, False, True, True, "")
    
    'Do the conversion.
    pConvertLabelsToAnnotation.ConvertLabels()
    
    Dim pEnumLayer As IEnumLayer = pConvertLabelsToAnnotation.AnnoLayers
    
    'Turn off labeling for the layer converted.
    pGeoFeatureLayer.DisplayAnnotation = False
    
    'Add the result annotation layer to the map.
    pMap.AddLayers(pEnumLayer, True)
    
    'Refresh the map to update the display.
    Dim pActiveView As IActiveView = pMap
    pActiveView.Refresh()
End If
End Sub






Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine