Converting labels to geodatabase annotation for an entire map


About converting labels to geodatabase annotation for an entire map

This topic shows how to use the ConvertLabelsToAnnotation class to change labels for an entire map to a 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's opened via MapDocument or MapReader.
[C#]
static void ConvertLabelsToGDBAnnotationEntireMap(IMap pMap, 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.esriVisibleFeatures, true, pTrackCancel, null);
    IUID pUID = new UIDClass();
    pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; 
        //IGeoFeatureLayer interface ID.
    IMapLayers pMapLayers = pMap as IMapLayers;
    IEnumLayer pEnumLayer = pMapLayers.get_Layers(pUID as UIDClass, true);
    pEnumLayer.Reset();
    IGeoFeatureLayer pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;

    while (pGeoFeatureLayer != null)
    {
        if (pGeoFeatureLayer.Valid == true)
        {
            if (pMapLayers.IsLayerVisible(pGeoFeatureLayer as ILayer))
            //Takes scale and groups layers into account.
            {
                if (pGeoFeatureLayer.DisplayAnnotation == true)
                {
                    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, "");
                }
            }
        }
        pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;
    }
    //Do the conversion.
    pConvertLabelsToAnnotation.ConvertLabels();
    IEnumLayer pAnnoEnumLayer = pConvertLabelsToAnnotation.AnnoLayers;
    //Turn off labeling for the layers converted.
    pEnumLayer.Reset();
    pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;
    while (pGeoFeatureLayer != null)
    {
        if (pGeoFeatureLayer.Valid == true)
        {
            if (pMapLayers.IsLayerVisible(pGeoFeatureLayer as ILayer))
            //Takes scale and groups layers into account.
            {
                if (pGeoFeatureLayer.DisplayAnnotation == true)
                    pGeoFeatureLayer.DisplayAnnotation = false;
            }
        }
        pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;
    }
    //Add the result annotation layer to the map.
    pMap.AddLayers(pAnnoEnumLayer, true);
    //Refresh the map to update the display.
    IActiveView pActiveView = pMap as IActiveView;
    pActiveView.Refresh();
}
[VB.NET]
Shared Sub ConvertLabelsToGDBAnnotationEntireMap(ByVal pMap As IMap, 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.esriVisibleFeatures, True, pTrackCancel, Nothing)

Dim pUID As IUID = New UIDClass()
pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" 'IGeoFeatureLayer interface ID.

Dim pMapLayers As IMapLayers = pMap
Dim pEnumLayer As IEnumLayer = pMapLayers.Layers(pUID, True)
pEnumLayer.Reset()

Dim pGeoFeatureLayer As IGeoFeatureLayer = pEnumLayer.Next()


While Not pGeoFeatureLayer Is Nothing
    If pGeoFeatureLayer.Valid = True Then
        If pMapLayers.IsLayerVisible(pGeoFeatureLayer) Then
            If pGeoFeatureLayer.DisplayAnnotation = True 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, "")
            End If
        End If
    End If
    pGeoFeatureLayer = pEnumLayer.Next()
End While

'Do the conversion.
pConvertLabelsToAnnotation.ConvertLabels()

Dim pAnnoEnumLayer As IEnumLayer = pConvertLabelsToAnnotation.AnnoLayers

'Turn off labeling for the layers converted.
pEnumLayer.Reset()
pGeoFeatureLayer = pEnumLayer.Next()
While Not pGeoFeatureLayer Is Nothing
    If pGeoFeatureLayer.Valid = True Then
        If pMapLayers.IsLayerVisible(pGeoFeatureLayer) Then
            If pGeoFeatureLayer.DisplayAnnotation = True Then
                pGeoFeatureLayer.DisplayAnnotation = False
            End If
        End If
    End If
    pGeoFeatureLayer = pEnumLayer.Next()
End While

'Add the result annotation layer to the map.
pMap.AddLayers(pAnnoEnumLayer, True)

'Refresh the map to update the display.
Dim pActiveView As IActiveView = pMap
pActiveView.Refresh()
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