HOW TO

Add a title to an ArcMap page layout using ArcObjects

Last Published: April 25, 2020

Summary

This article contains sample code that adds a title to your page layout. The code uses the name of the first layer in the map document for the title.

Procedure



  1. Open ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in ThisDocument code module will only run in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module.

  4. Paste the following code into the code module.

    Code:
    Sub AddTitleToLayout()
    Dim pApp As IMxApplication
    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Dim pLayer As ILayer
    Dim pPageLayout As IPageLayout
    Dim pGc As IGraphicsContainer
    Dim pDisplay As IScreenDisplay
    Dim pTxtElem As ITextElement
    Dim pTxtSym As ITextSymbol

    'Get the first layer in the map
    Set pApp = Application
    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap
    Set pLayer = pMap.Layer(0)

    'Get the layer name to use for layout title
    Dim sStrName As String
    sStrName = pLayer.Name
    Set pPageLayout = pDoc.PageLayout
    Set pDisplay = pApp.Display
    Set pGc = pPageLayout
    pGc.Reset

    'Set the font and color properties
    'for the title
    Dim myFont As IFontDisp
    Set myFont = New StdFont
    myFont.Name = "Courier New"
    myFont.Size = 24
    Dim myColor As IRgbColor
    Set myColor = New RgbColor
    myColor.Red = 150
    myColor.Green = 0
    myColor.Blue = 0

    'Create a text element
    Set pTxtElem = New TextElement

    'Create a text symbol
    Set pTxtSym = New TextSymbol
    With pTxtSym
    .Color = myColor
    .Font = myFont
    End With

    'Set symbol property
    pTxtElem.Symbol = pTxtSym

    'set the text property to be the layer's name (Uppercase)
    pTxtElem.Text = UCase(sStrName)

    'Create an envelope for the TextElements Geometry
    Dim pEnv As IEnvelope
    Set pEnv = New Envelope
    Dim pPoint As IPoint
    Set pPoint = New Point

    pPoint.X = 2
    pPoint.Y = 8
    pEnv.LowerLeft = pPoint
    pPoint.X = 7
    pPoint.Y = 10
    pEnv.UpperRight = pPoint
    'set the text elements geomtery
    Dim pElem As IElement
    Set pElem = pTxtElem
    pElem.Geometry = pEnv

    'Add the element to the graphics container
    pGc.AddElement pElem, 0
    pDoc.ActiveView.Refresh
    End Sub

  5. Close the Visual Basic Editor.
  6. Add data to ArcMap and switch to layout view.
  7. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.

Article ID:000004979

Software:
  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic