Select to view content in your preferred language

Convert a text to a geometry

301
4
03-10-2026 09:13 AM
BerendVeldkamp
Frequent Contributor

With ArcObjects, it was possible to convert a textstring to a polygon, using IQueryGeometry.GetGeometry on the TextSymbol coclass.

 

Is something like this possible with the ArcGIS Pro SDK?

0 Kudos
4 Replies
RichardDaniels
MVP Regular Contributor

ArcGIS Pro supports Graphics to Features command for basic graphic features (e.g., point, line, polygon). You could probably try the same for 'text' elements but you would most likely get a point for the lower left of the first character of the sentence (i.e., you won't get the text string as a set of lines for example). Best alternative would be to look into converting the graphic text into annotation. 

BerendVeldkamp
Frequent Contributor

@RichardDaniels Do you happen to have an example of how to get a geometry from an AnnotationFeature? I tried this code, but it only returns the bounding box of the annotation. I need the actual shape of the characters in the text.

 var layer = (AnnotationLayer)MapView.Active.Map
    .GetLayersAsFlattenedList()
    .FirstOrDefault(l => l.Name == "AnnoTest");
 using var cursor = layer.Search();
 if (cursor.MoveNext())
 {
     var annotationFeature = (AnnotationFeature)cursor.Current;
     var json1 = annotationFeature.GetGraphicOutline().ToJson();
     var json2 = annotationFeature.GetShape().ToJson();
 }

(Both GetGraphicOutline and GetShape return the same thing)

0 Kudos
BerendVeldkamp
Frequent Contributor

So, it appears to be not that hard after all, when you discover the FormattedText class from Windows.System.Media:

var formattedText = new FormattedText("Text",
    System.Globalization.CultureInfo.CurrentCulture,
    FlowDirection.LeftToRight,
    new Typeface("Arial"),
    100,
    Brushes.Black,
    96);
var geometry = formattedText.BuildGeometry(new Point(0, 0));
var path = geometry.GetFlattenedPathGeometry();
var figures = path.Figures;

 

Then loop over all the figures' segments, casting the segments to PolylineSegment. this will return the coordinates.

GetFlattenedPathGeometry returns an approximation, but you can increase the emSize parameter for better quality.

The origin (0,0) is the top-left corner, positive Y values going down, so you may need to mirror over the X-axis by subtracting Y values from geom.Bounds.Height.

 

0 Kudos
BerendVeldkamp
Frequent Contributor

Thanks, I'll try and post back if it works (or not 😉)

0 Kudos