Help with finding TextElement in PageLayout?

1156
11
06-09-2011 07:51 AM
AnnCrystal
New Contributor II
Hi Guys:
This may sound simple to you. But I am stuck. I am trying to find whether there is a disclaimer text in the pagelayout. If not, I want to add one. Here is the code:

if (enumElement != null)
                    {
                    findElement = enumElement.Next();
                    while (findElement != null)
                        {
                        if (findElement is IParagraphTextElement)
                            {
                            findTextElement = findElement as ITextElement;
                          
                            if (findTextElement.Text.EndsWith("></CLR>"))
                                {
                                MessageBox.Show("The Disclaimer Text, which autoupdates the time and date, is already placed in the map.");
                                }
                            else
                                {
                                graphicsContainer.AddElement(element, 0);
                                graphicsContainerSelect.UnselectAllElements();
                                activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                                }
                               
                            }
                        findElement = enumElement.Next();
                        }

                    }


My else part is not working. Where am I wrong? How can I write that if there are no ParagraphtextElements, then add my text?

Thanks in advance for your help.
Ann
0 Kudos
11 Replies
LeoDonahue
Occasional Contributor III
I can give you a Java sample:

Your text elements would have to have names, that you defined when creating the element in ArcMap.

            // Find certain pre-labeled graphics in the map and set their text
            IGraphicsContainer igc = whatever code you have that will return a PageLayout
            TextElement textelement;

            igc.reset();
            IElement element = igc.next();
            while(element != null){
                if(element instanceof TextElement){
                    textelement = (TextElement)element;
                    if(textelement.getName().equals("the_name_of_your_graphic_element")){
                        textelement.setText(somevalue);
                    }
                }
                element = igc.next();
            }
            // End Find pre-labeled graphics
0 Kudos
AnnCrystal
New Contributor II
Thanks for the code. However, mine is a paragraphtextelement which doesn't have a name like that of graphics. In that scenario, how can I use a condition to say that if there are no text elements in the layout, add my code? That's where I am confused. I am writing in C#. (However, java or vb.net code will help.) Just trying to understand the logic.
Thanks again
Ann

I can give you a Java sample:

Your text elements would have to have names, that you defined when creating the element in ArcMap.

            // Find certain pre-labeled graphics in the map and set their text
            IGraphicsContainer igc = whatever code you have that will return a PageLayout
            TextElement textelement;

            igc.reset();
            IElement element = igc.next();
            while(element != null){
                if(element instanceof TextElement){
                    textelement = (TextElement)element;
                    if(textelement.getName().equals("the_name_of_your_graphic_element")){
                        textelement.setText(somevalue);
                    }
                }
                element = igc.next();
            }
            // End Find pre-labeled graphics
0 Kudos
JeffreyHamblin
New Contributor III
Hi Ann,

ParagraphTextElement (and TextElement) both implement the IElementProperties interface, which is where you will find the Name property.

So when you create your ParagraphTextElement, cast it to IElementProperties and set the Name. And during your code the searches the elements, when you find an IParagraphTextElement do the same cast and check the Name.

-Jeff
0 Kudos
LeoDonahue
Occasional Contributor III
You have the option to give a name to any Text Element you add to your Layout.

I suppose if you were the one that added the Text Element in the map, you would give different types of text elements a naming convention?  So that you would know there is a text element of the type you are looking for?
0 Kudos
AnnCrystal
New Contributor II
Thank you Leo and Jeff. My problem is not just with the paragraph text element which I place with my code. I have to search for paragraph texts which somebody else may have placed in the pagelayout. That's where it gets little tricky when they might not have set a name. That's why I decided that when there are no paragraph text elements in the layout, put my text.
My confusion is how to put the condition of NOT in the code?
i.e
If there are no paratextelements
{
add my element
}

Anythoughts?
Thanks again for your support!
-Ann
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's an example, although written in VB.NET

        Dim graphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer
        Dim pElement As ESRI.ArcGIS.Carto.IElement
        dim HasParagraph as Boolean = False

        graphicsContainer = CType(pMxDoc.FocusMap, ESRI.ArcGIS.Carto.IGraphicsContainer)
        graphicsContainer.Reset()
        pElement = graphicsContainer.Next
        While Not pElement Is Nothing
            If TypeOf pElement Is ESRI.ArcGIS.Carto.IParagraphTextElement Then HasParagraph = True
            pElement = graphicsContainer.Next
        End While

        If Not HasParagraph then graphicsContainer.AddElement (pParagraphElement, 0)

0 Kudos
JeffreyHamblin
New Contributor III
Ann, if I understand you correctly, there could be other ParagraphTextElements on the layout, and you need to add your disclaimer element if it doesn't exist.

The following method will do that:

public void PerformTest()
{
    const string myParaTextID = "MyDisclaimerTextID";
    string paraText = "Sample disclaimer paragraph text";

    IGraphicsContainer graphicsContainer = ArcMap.Document.PageLayout as IGraphicsContainer;
    IElement element;
    IElementProperties elementProperties;


    // Check if the disclaimer text element already exists in layout
    graphicsContainer.Reset();
    while ((element = graphicsContainer.Next()) != null)
    {
        if (element is IParagraphTextElement)
        {
            elementProperties = element as IElementProperties;
            if (elementProperties.Name == myParaTextID)
            {
                MessageBox.Show("The Disclaimer Text, which autoupdates " + 
                    "the time and date, is already placed in the map.");
                return;

            }
        }
    }


    // No disclaimer found, so make one

    IParagraphTextElement paraTextElement = new ParagraphTextElementClass();
    ITextElement textElement = paraTextElement as ITextElement;
    elementProperties = paraTextElement as IElementProperties;
    element = paraTextElement as IElement;

    // Set this so it can be found if this method called again
    elementProperties.Name = myParaTextID;

    // Set the actual text to display
    textElement.Text = paraText;

    // Set the size and location of the text box
    IEnvelope envelope = new EnvelopeClass();
    envelope.PutCoords(0, 0, 3, 1);
    element.Geometry = envelope;

    // Add the text box to the layout
    graphicsContainer.AddElement(element, 0);

    ArcMap.Document.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}


Note that the "Name" property used to ID the disclaimer element is visible and editable to the user on the properties dialog for the element in ArcMap. If you want more robust tagging of elements (ie, hidden from the user), you need to attach a custom object such as a PropertySet.

-Jeff
0 Kudos
AnnCrystal
New Contributor II
Perfect!!!! Thanks Ken- this is what I was looking for!

Here's an example, although written in VB.NET

        Dim graphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer
        Dim pElement As ESRI.ArcGIS.Carto.IElement
        dim HasParagraph as Boolean = False

        graphicsContainer = CType(pMxDoc.FocusMap, ESRI.ArcGIS.Carto.IGraphicsContainer)
        graphicsContainer.Reset()
        pElement = graphicsContainer.Next
        While Not pElement Is Nothing
            If TypeOf pElement Is ESRI.ArcGIS.Carto.IParagraphTextElement Then HasParagraph = True
            pElement = graphicsContainer.Next
        End While

        If Not HasParagraph then graphicsContainer.AddElement (pParagraphElement, 0)

0 Kudos
AnnCrystal
New Contributor II
Thanks Jeff. I just noticed your post.That's is a really good thought. You're always very helpful!!! Why ESRI stopped MVP? You really deserve a credit for that!!!!
Ann, if I understand you correctly, there could be other ParagraphTextElements on the layout, and you need to add your disclaimer element if it doesn't exist.

The following method will do that:

public void PerformTest()
{
    const string myParaTextID = "MyDisclaimerTextID";
    string paraText = "Sample disclaimer paragraph text";

    IGraphicsContainer graphicsContainer = ArcMap.Document.PageLayout as IGraphicsContainer;
    IElement element;
    IElementProperties elementProperties;


    // Check if the disclaimer text element already exists in layout
    graphicsContainer.Reset();
    while ((element = graphicsContainer.Next()) != null)
    {
        if (element is IParagraphTextElement)
        {
            elementProperties = element as IElementProperties;
            if (elementProperties.Name == myParaTextID)
            {
                MessageBox.Show("The Disclaimer Text, which autoupdates " + 
                    "the time and date, is already placed in the map.");
                return;

            }
        }
    }


    // No disclaimer found, so make one

    IParagraphTextElement paraTextElement = new ParagraphTextElementClass();
    ITextElement textElement = paraTextElement as ITextElement;
    elementProperties = paraTextElement as IElementProperties;
    element = paraTextElement as IElement;

    // Set this so it can be found if this method called again
    elementProperties.Name = myParaTextID;

    // Set the actual text to display
    textElement.Text = paraText;

    // Set the size and location of the text box
    IEnvelope envelope = new EnvelopeClass();
    envelope.PutCoords(0, 0, 3, 1);
    element.Geometry = envelope;

    // Add the text box to the layout
    graphicsContainer.AddElement(element, 0);

    ArcMap.Document.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}


Note that the "Name" property used to ID the disclaimer element is visible and editable to the user on the properties dialog for the element in ArcMap. If you want more robust tagging of elements (ie, hidden from the user), you need to attach a custom object such as a PropertySet.

-Jeff
0 Kudos