How to change Background color of rectangular envelope containing a text in the Layout of ArcGIS Pro?

328
3
Jump to solution
02-25-2021 09:38 AM
GyanendraGurung
New Contributor III

1. I am trying to change the background of the rectangular area containing a text in ArcGIS Pro Layout. I am trying to figure out WHERE to insert WHAT in the following code to make that work.

2. The following code was my attempt to change the color of the envelope containing the text, but it actually changed the color of the text instead. 

 

 

                var theLayout = await QueuedTask.Run<Layout>(() =>
                {
                    //Set up a page
                    CIMPage newPage = new CIMPage
                    {
                        Width = 420,
                        Height = 297,
                        Units = LinearUnit.Millimeters
                    };
                    Layout layout = LayoutFactory.Instance.CreateLayout(newPage);

                    #region Table
                    // Caption 
                    title = "CAPTION";
                    plyCoords = new List<Coordinate2D>();
                    plyCoords.Add(new Coordinate2D(286, 271));
                    plyCoords.Add(new Coordinate2D(286, 277));
                    plyCoords.Add(new Coordinate2D(400, 277));
                    plyCoords.Add(new Coordinate2D(400, 271));
                    poly = PolygonBuilder.CreatePolygon(plyCoords);

                    CIMStroke outline = SymbolFactory.Instance.ConstructStroke(ColorFactory.Instance.BlackRGB, 0.5, SimpleLineStyle.Solid);
                    polySym = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.WhiteRGB, SimpleFillStyle.Solid, outline);
                    sym = SymbolFactory.Instance.ConstructTextSymbol(polySym, 10, "Tahoma", "Bold");
                    sym.HorizontalAlignment = HorizontalAlignment.Center; 
                    
                    polyTxtElm = LayoutElementFactory.Instance.CreatePolygonParagraphGraphicElement(layout, poly, title, sym);

                    return layout;
                });

 

 

 

0 Kudos
1 Solution

Accepted Solutions
KirkKuykendall1
Occasional Contributor III

It might be better to start from Esri's sample here.

This works for me:

await QueuedTask.Run(() =>
{
    //Build 2D polygon geometry
    List<Coordinate2D> plyCoords = new List<Coordinate2D>();
    plyCoords.Add(new Coordinate2D(3.5, 7));
    plyCoords.Add(new Coordinate2D(4.5, 7));
    plyCoords.Add(new Coordinate2D(4.5, 6.7));
    plyCoords.Add(new Coordinate2D(5.5, 6.7));
    plyCoords.Add(new Coordinate2D(5.5, 6.1));
    plyCoords.Add(new Coordinate2D(3.5, 6.1));
    Polygon poly = PolygonBuilder.CreatePolygon(plyCoords);

    //Set symbolology, create and add element to layout
    //Also notice how formatting tags are using within the text string.
    CIMTextSymbol sym = SymbolFactory.Instance
        .ConstructTextSymbol
        (ColorFactory.Instance.WhiteRGB, 10, "Arial", "Regular");
    string text = @"Some Text String that is really long and is <BOL>forced to wrap to other lines</BOL> so that we can see the effects." as String;
    GraphicElement polyTxtElm = LayoutElementFactory.Instance
        .CreatePolygonParagraphGraphicElement(layout, poly, text, sym);
    polyTxtElm.SetName("New Polygon Text");

    //(Optionally) Modify paragraph border 
    CIMGraphic polyTxtGra = polyTxtElm.GetGraphic();
    CIMParagraphTextGraphic cimPolyTxtGra = polyTxtGra as CIMParagraphTextGraphic;
    cimPolyTxtGra.Frame.BorderSymbol = new CIMSymbolReference();
    cimPolyTxtGra.Frame.BorderSymbol.Symbol = SymbolFactory.Instance
        .ConstructLineSymbol(ColorFactory.Instance.GreyRGB, 1.0, SimpleLineStyle.Solid);
    cimPolyTxtGra.Frame.BackgroundSymbol = new CIMSymbolReference();
    cimPolyTxtGra.Frame.BackgroundSymbol.Symbol = SymbolFactory.Instance
        .ConstructPolygonSymbol(ColorFactory.Instance.GreyRGB);
    polyTxtElm.SetGraphic(polyTxtGra);
});

 

View solution in original post

3 Replies
KirkKuykendall1
Occasional Contributor III

Did you try creating a graphic and assigning it to the element?

 

var polyTxtElm = LayoutElementFactory.Instance
    .CreatePolygonParagraphGraphicElement(layout, poly, title, sym);
// assign a graphic to the element:
var g = new CIMPolygonGraphic();
g.Polygon = poly;
g.Symbol = polySym.MakeSymbolReference();
polyTxtElm.SetGraphic(g);

 

GyanendraGurung
New Contributor III

The solution you provided creates a rectangle, which is good. So, the only way I can achieve my result is to create another polyTxtElm and overlay on top of that rectangle you provided. THIS IS NOT THE BEST SOLUTION, but does the job. 

	// Create Rectangle using polygon //
	CIMPolygonSymbol polySym = SymbolFactory.Instance
		.ConstructPolygonSymbol(ColorFactory.Instance.WhiteRGB, 
			SimpleFillStyle.Solid);
	GraphicElement polyTxtElm = LayoutElementFactory.Instance
		.CreatePolygonGraphicElement(layout, poly);
	var g = new CIMPolygonGraphic
	{
		Polygon = poly,
		Symbol = polySym.MakeSymbolReference()                        
	};
	polyTxtElm.SetGraphic(g);					

	// Overlay Text over SAME polygon //
	CIMStroke outline = SymbolFactory.Instance
		.ConstructStroke(ColorFactory.Instance.BlackRGB, 0.5, 
			SimpleLineStyle.Solid);
	polySym = SymbolFactory.Instance
		.ConstructPolygonSymbol(ColorFactory.Instance.WhiteRGB, 
			SimpleFillStyle.Solid, outline);
	CIMTextSymbol sym = SymbolFactory.Instance
		.ConstructTextSymbol(polySym, 10, "Tahoma", "Bold");
	sym.HorizontalAlignment = HorizontalAlignment.Center; 

	polyTxtElm = LayoutElementFactory.Instance
		.CreatePolygonParagraphGraphicElement(layout, 
                    poly, title, sym);

 

0 Kudos
KirkKuykendall1
Occasional Contributor III

It might be better to start from Esri's sample here.

This works for me:

await QueuedTask.Run(() =>
{
    //Build 2D polygon geometry
    List<Coordinate2D> plyCoords = new List<Coordinate2D>();
    plyCoords.Add(new Coordinate2D(3.5, 7));
    plyCoords.Add(new Coordinate2D(4.5, 7));
    plyCoords.Add(new Coordinate2D(4.5, 6.7));
    plyCoords.Add(new Coordinate2D(5.5, 6.7));
    plyCoords.Add(new Coordinate2D(5.5, 6.1));
    plyCoords.Add(new Coordinate2D(3.5, 6.1));
    Polygon poly = PolygonBuilder.CreatePolygon(plyCoords);

    //Set symbolology, create and add element to layout
    //Also notice how formatting tags are using within the text string.
    CIMTextSymbol sym = SymbolFactory.Instance
        .ConstructTextSymbol
        (ColorFactory.Instance.WhiteRGB, 10, "Arial", "Regular");
    string text = @"Some Text String that is really long and is <BOL>forced to wrap to other lines</BOL> so that we can see the effects." as String;
    GraphicElement polyTxtElm = LayoutElementFactory.Instance
        .CreatePolygonParagraphGraphicElement(layout, poly, text, sym);
    polyTxtElm.SetName("New Polygon Text");

    //(Optionally) Modify paragraph border 
    CIMGraphic polyTxtGra = polyTxtElm.GetGraphic();
    CIMParagraphTextGraphic cimPolyTxtGra = polyTxtGra as CIMParagraphTextGraphic;
    cimPolyTxtGra.Frame.BorderSymbol = new CIMSymbolReference();
    cimPolyTxtGra.Frame.BorderSymbol.Symbol = SymbolFactory.Instance
        .ConstructLineSymbol(ColorFactory.Instance.GreyRGB, 1.0, SimpleLineStyle.Solid);
    cimPolyTxtGra.Frame.BackgroundSymbol = new CIMSymbolReference();
    cimPolyTxtGra.Frame.BackgroundSymbol.Symbol = SymbolFactory.Instance
        .ConstructPolygonSymbol(ColorFactory.Instance.GreyRGB);
    polyTxtElm.SetGraphic(polyTxtGra);
});

 

View solution in original post