The IPageLayout interface is the primary interface implemented by the PageLayout object. Use this interface to access the ruler settings, snap grid, snap guides, and page objects. IPageLayout also has methods for zooming the view and changing the map's focus. The following code demonstrates zooming:
[C#]
publicvoid ZoomToPercent(IActiveView docActiveView)
{
IPageLayout docPageLayout;
if (docActiveView is IPageLayout)
{
docPageLayout = docActiveView as IPageLayout;
docPageLayout.ZoomToPercent(50);
}
else
{
MessageBox.Show("This tool only functions in layout view");
}
docActiveView.Refresh();
}
[VB.NET]
PublicSub ZoomToPercent(ByVal docActiveView As IActiveView)
Dim docPageLayout As IPageLayout
IfTypeOf docActiveView Is IPageLayout Then
docPageLayout = docActiveView
docPageLayout.ZoomToPercent(50)
Else
MessageBox.Show("This tool only functions in layout view")
EndIf
docActiveView.Refresh()
EndSub
IGraphicsContainer
IGraphicsContainer provides access to the PageLayout object's graphic elements. Use this interface to add new elements or access existing ones. For example, a title at the top of a layout is a text element stored in the layout's graphics container.
The following code example moves all the elements in the layout 1 inch to the right:
[C#]
publicvoid MoveAllElements(IActiveView docActiveView )
{
IPageLayout docPageLayout;
IGraphicsContainer GContainer;
IElement docElement;
ITransform2D docTransform2D;
docPageLayout = new PageLayoutClass();
if (docActiveView is IPageLayout)
{
docPageLayout = docActiveView as IPageLayout;
GContainer = docPageLayout as IGraphicsContainer;
//Loop through all the elements and move each one inch.
GContainer.Reset();
docElement = GContainer.Next();
while (docElement != null)
{
docTransform2D = docElement as ITransform2D;
docTransform2D.Move(1, 0);
docElement = GContainer.Next();
}
}
else
{
MessageBox.Show("This tool only works in pagelayout view.");
}
//Refresh only the page layout's graphics.
docActiveView.PartialRefresh (esriViewDrawPhase.esriViewGraphics, null, null);
}
[VB.NET]
PublicSub MoveAllElements(ByVal docActiveView As IActiveView)
Dim docPageLayout As IPageLayout
Dim GContainer As IGraphicsContainer
Dim docElement As IElement
Dim docTransform2D As ITransform2D
docPageLayout = New PageLayoutClass()
IfTypeOf docActiveView Is IPageLayout Then
docPageLayout = docActiveView
GContainer = docPageLayout
'Loop through all the elements and move each one inch.
GContainer.Reset()
docElement = GContainer.Next()
WhileNot docElement IsNothing
docTransform2D = docElement
docTransform2D.Move(1, 0)
docElement = GContainer.Next()
EndWhileElse
MessageBox.Show("This tool only works in pagelayout view.")
EndIf'Refresh only the page layout's graphics.
docActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
EndSub
The following code shows one method for adding a new text element onto the page layout. In this example, ITool is used to get a mouse down event so users can place the text element anywhere on the page layout. The script only adds a new element if ArcMap is in layout view. To use this sample, paste the code into the OnMouseDown event in a newly created ITool.
[C#]
publicoverridevoid OnMouseDown(int Button, int Shift, int X, int Y)
{
IPageLayout docPageLayout;
IActiveView docActiveView;
IGraphicsContainer GContainer;
ITextElement docTextElement;
IElement docElement;
IPoint PtPoint;
//Use the hookhelper to obtain the loaded doc's page layout.
docPageLayout = m_hookHelper.PageLayout;
docActiveView = docPageLayout as IActiveView;
GContainer = docPageLayout as IGraphicsContainer;
//Verify ArcMap is in layout view.if (m_hookHelper.ActiveView == m_hookHelper.PageLayout)
{
docTextElement = new TextElementClass();
docElement = docTextElement as IElement;
//Create a point from the x,y coordinate parameters.
PtPoint = docActiveView.ScreenDisplay.DisplayTransformation.ToMaPtPoint(X, Y);
docTextElement.Text = "My Map";
docElement.Geometry = PtPoint;
GContainer.AddElement(docTextElement as IElement, 0);
//Refresh only the page layout's graphics.
docActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
else
{
MessageBox.Show("This tool only works in layout view");
return;
}
}
[VB.NET]
PublicOverridesSub OnMouseDown(ByVal Button AsInteger, ByVal Shift AsInteger, ByVal X AsInteger, ByVal Y AsInteger)
Dim docPageLayout As IPageLayout
Dim docActiveView As IActiveView
Dim GContainer As IGraphicsContainer
Dim docTextElement As ITextElement
Dim docElement As IElement
Dim PtPoint As IPoint
'Use the hookhelper to obtain the loaded doc's page layout.
docPageLayout = m_hookHelper.PageLayout
docActiveView = docPageLayout
GContainer = docPageLayout
'Verify ArcMap is in layout view.IfTypeOf m_hookHelper.ActiveView Is IPageLayout Then
docTextElement = New TextElement
docElement = docTextElement
'Create a point from the x,y coordinate parameters.
PtPoint = docActiveView.ScreenDisplay.DisplayTransformation.ToMaPtPoint(X, Y)
docTextElement.Text = "My Map"
docElement.Geometry = PtPoint
GContainer.AddElement(docTextElement, 0)
'Refresh only the page layout's graphics.
docActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
Else
MessageBox.Show("This tool only works in layout view")
ReturnEndIfEndSub
IGraphicsContainerSelect (selecting graphics)
Most objects that are graphics containers, such as PageLayout and Map, implement the IGraphicsContainerSelect interface to expose additional members for managing their element selection. For example, IGraphicsContainerSelect::UnselectAllElements can be used to clear an object's graphic element selection.
The following example returns the number of elements currently selected in the focus Map and PageLayout object:
[C#]
publicvoid GraphicSelectionCount(MaDocument Doc)
{
IMap docMap;
IPageLayout docPageLayout;
IGraphicsContainerSelect MapGSelect;
IGraphicsContainerSelect docPageLayoutGraphicsSelect;
docMap = new MapClass();
docPageLayout = new PageLayoutClass();
docMap = Doc.ActiveView.FocusMap;
docPageLayout = Doc.PageLayout;
MapGSelect = docMap as IGraphicsContainerSelect;
docPageLayoutGraphicsSelect = docPageLayout as IGraphicsContainerSelect;
MessageBox.Show( "Selected elements in the map: " + MapGSelect.ElementSelectionCount);
MessageBox.Show("Selected elements in the page layout: " + docPageLayoutGraphicsSelect.ElementSelectionCount);
}
[VB.NET]
PublicSub GraphicSelectionCount(ByVal Doc As MaDocument)
Dim docMap As IMap
Dim docPageLayout As IPageLayout
Dim MapGSelect As IGraphicsContainerSelect
Dim docPageLayoutGraphicsSelect As IGraphicsContainerSelect
docMap = New Map
docPageLayout = New PageLayout
docMap = Doc.ActiveView.FocusMap
docPageLayout = Doc.PageLayout
MapGSelect = docMap
docPageLayoutGraphicsSelect = docPageLayout
MessageBox.Show("Selected elements in the map: " + MapGSelect.ElementSelectionCount)
MessageBox.Show("Selected elements in the page layout: " + docPageLayoutGraphicsSelect.ElementSelectionCount)
EndSub
IPage
IPage is the primary interface on the Page object. Use this interface to access all the properties of an ArcMap page, including the page's border, background, background color, orientation, and size.
The following code changes the size and color of the page:
[C#]
publicvoid CheckPageSize(IPageLayout docPageLayout)
{
IPage docPage;
docPage = new PageClass();
Double dHeight;
Double dWidth;
//m_hookHelper is initialized previously to the application's active document.
docPage = docPageLayout.Page;
docPage.QuerySize(out dWidth, out dHeight);
if ((dWidth == 8.5) && (dHeight == 11))
{
docPage.PutCustomSize(5, 5);
}
}
publicvoid ChangePageColor(IPageLayout docPageLayout)
{
//Change the background color of the page.
IPage docPage;
IRgbColor docRgbColor;
docPage = docPageLayout.Page;
docRgbColor = new RgbColorClass();
docRgbColor.Blue = 204;
docRgbColor.Red = 255;
docRgbColor.Green = 255;
docPage.BackgroundColor = docRgbColor;
}
[VB.NET]
PublicSub CheckPageSize(ByVal docPageLayout As IPageLayout)
'If page size is letter, change the page size to 5 by 5.Dim docPage As IPage
docPage = New Page
Dim dHeight AsDoubleDim dWidth AsDouble
docPage = docPageLayout.Page
docPage.QuerySize(dWidth, dHeight)
If ((dWidth = 8.5) And (dHeight = 11)) Then
docPage.PutCustomSize(5, 5)
EndIfEndSubPublicSub ChangePageColor(ByVal docPageLayout As IPageLayout)
'Change the background color of the page.Dim docPage As IPage
Dim docRgbColor As IRgbColor
docPage = docPageLayout.Page
docRgbColor = New RgbColor
docRgbColor.Blue = 204
docRgbColor.Red = 255
docRgbColor.Green = 255
docPage.BackgroundColor = docRgbColor
EndSub
The esriPageFormID enumeration provides a convenient list of preselected page sizes for use by the Page object. For example, to change the layout to standard legal page size, pass in esriPageFormLegal to IPage::FormID. This is much quicker than setting a custom size with IPage::PutCustomSize.
The following code uses the esriPageFormID enumeration to quickly change the page size. It is beneficial if you used the previous code sample to change the page's size and color.
[C#]
publicvoid SetLegalPageSize(IPageLayout docPageLayout)
{
IPage docPage;
docPage = new PageClass();
Double x;
Double y;
docPage = docPageLayout.Page;
docPage.FormID = esriPageFormID.esriPageFormLegal;
docPage.QuerySize(out x, out y);
MessageBox.Show("The page size is now: " + x + " " + y);
}
[VB.NET]
PublicSub SetLegalPageSize(ByVal docPageLayout As IPageLayout)
Dim docPage As IPage
docPage = New Page
Dim x AsDoubleDim y AsDouble
docPage = docPageLayout.Page
docPage.FormID = esriPageFormID.esriPageFormLegal
docPage.QuerySize(x, y)
MessageBox.Show("The page size is now: " & x & " x " & y)
EndSub