This document is archived and information here might be outdated.  Recommended version.


How to save PageLayoutControl contents to a map document (ArcObjects .NET 10.4 SDK)

How to save PageLayoutControl contents to a map document


Summary
This topic shows how to save the contents of a PageLayoutControl to a map document (.mxd file). The contents of a PageLayoutControl can be changed by toggling layer visibility or by drawing graphic elements on the page. Changes made to the PageLayout can be saved to the existing map document, or they can be saved to a new map document by using a System.Windows.Forms.SaveFileDialog control to name the document.

In this topic


Saving to an existing map document 

To save an object of type IMapDocument, use the SaveDocument method as shown in the following code example.

Saving to a new map document

To save PageLayoutControl contents to a new map document, perform the following steps:
  1. Create a System.Windows.Forms.SaveFileDialog control by dragging the control from the Visual Studio toolbox onto your application's form. This dialog box allows users to search and select map documents.
  2. Implement its OnClick events.
  3. Open the newly created document using OpenDocument. The Open method of a new MapDocument object is passed the file name of the document selected on the Browse dialog box. The IPageLayoutControl.PageLayout property is set to the IMapDocument.PageLayout property.
See the following code example:
[C#]
private void OpenDocument(string sFilePath)
{
    if (m_MapDocument != null)
        m_MapDocument.Close();
    //Create a new map document.
    m_MapDocument=new MapDocumentClass();
    //Open the selected map document.
    m_MapDocument.Open(sFilePath, "");
    //Set the PageLayoutControl page layout to the map document page layout.
    axPageLayoutControl1.PageLayout=m_MapDocument.PageLayout;
    txtMapDocument.Text=m_MapDocument.DocumentFilename;
}

private void SaveDocument()
{
    //Check that the document is not read only.
    if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) == true)
    {
        MessageBox.Show("This map document is read only!");
        return ;
    }
    //Save with the current relative path setting.
    m_MapDocument.Save(m_MapDocument.UsesRelativePaths, true);
    MessageBox.Show("Changes saved successfully!");
}

private void cmdSaveAs_Click(object sender, System.EventArgs e)
{
    //Open a file dialog for saving map documents.
    saveFileDialog1.Title="Save Map Document As";
    saveFileDialog1.Filter="Map Documents (*.mxd)|*.mxd";
    saveFileDialog1.ShowDialog();
    //Exit if no map document is selected.
    string sFilePath=saveFileDialog1.FileName;
    if (sFilePath == "")
    {
        return ;
    }
    if (sFilePath == m_MapDocument.DocumentFilename)
    {
        //Save changes to the current document.
        SaveDocument();
    }
    else
    {
        //SaveAs a new document with relative paths.
        m_MapDocument.SaveAs(sFilePath, true, true);
        //Open the document.
        OpenDocument((sFilePath));
        MessageBox.Show("Document saved successfully!");
    }
}
[VB.NET]
Public Sub OpenDocument(ByRef sFilePath As String)
    If Not m_pMapDocument Is Nothing Then m_pMapDocument.Close()
    'Create a new map document.
    m_pMapDocument=New MapDocumentClass
    'Open the selected map document.
    m_pMapDocument.Open(sFilePath)
    'Set the PageLayoutControl page layout to the map document page layout.
    AxPageLayoutControl1.PageLayout=m_pMapDocument.PageLayout
    txtMapDocument.Text=m_pMapDocument.DocumentFilename
End Sub

Public Sub SaveDocument()
    'Check that the document is not read only.
    If m_pMapDocument.IsReadOnly(m_pMapDocument.DocumentFilename)=True Then
        MsgBox("This map document is read only!", , "Save Failed")
        Exit Sub
    End If
    'Save with the current relative path setting.
    m_pMapDocument.Save(m_pMapDocument.UsesRelativePaths)
    MsgBox("Changes saved successfully!", , "Saved Document")
End Sub

Private Sub cmdSaveAs_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSaveAs.Click
    'Open a file dialog for saving map documents.
    SaveFileDialog1.Title="Save Map Document As"
    SaveFileDialog1.Filter="Map Documents (*.mxd)|*.mxd"
    SaveFileDialog1.ShowDialog()
    'Exit if no map document is selected.
    Dim sFilePath As String
    sFilePath=SaveFileDialog1.FileName
    If sFilePath="" Then Exit Sub
    If sFilePath=m_pMapDocument.DocumentFilename Then
        'Save changes to the current document.
        SaveDocument()
    Else
        'SaveAs a new document with relative paths.
        m_pMapDocument.SaveAs(sFilePath, True)
        'Open the document.
        OpenDocument((sFilePath))
        MsgBox("Document saved successfully!", , "Saved Document")
    End If
End Sub


See Also:

PageLayoutControl class
IPageLayoutControl interface
MapDocument class
IMapDocument interface




Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced