SuperMap iObjects .NET 11i 知识库
地图排版打印  

7     使用组件产品如何实现

7.1     示范程序介绍

根据第6章介绍的地图打印配置流程,我们了解了利用SuperMap组件产品实现地图排版打印的基本步骤。下面就给出使用SuperMap组件产品实现地图排版打印的示范程序。

该示范程序实现的功能如下:

1.     布局操作:保存布局、加载布局、保存布局为模板、加载布局模板、清空布局窗口;

2.     绘制布局元素:添加带复杂边框的地图对象、添加地图标题、添加方向标、添加比例尺、添加图例;

3.     编辑功能:添加点状元素、添加线状元素及面状填充;

4.     输出为图片:将布局导出为PDFEMFEPSBMPJPGPNG格式;

5.     布局浏览:布局的放大、缩小、平移、全幅显示,以及锁定地图;

6.     布局预览及布局打印。

7.     该示范程序,所使用的示范数据路径为:SuperMap组件产品安装目录\SampleData\China

7.2     初始化相关变量并打开布局窗口

初始化窗体,添加布局窗口,并连接工作空间,效果如图 7‑1所示:

    private void Initialize()

    {

           try

           {

                  // 打开工作空间

                   WorkspaceConnectionInfo conInfo = new  WorkspaceConnectionInfo(

                                 @"../../Project/SampleData/China/China400.smwu");

                  workspaceConnectionInfo.Type = WorkspaceType.SMWU;

                  m_workspace.Open(workspaceConnectionInfo);

                  m_mapLayoutControl.MapLayout.Workspace=m_workspace;

 

                  // 打开布局窗口水平、垂直滚动条

                  m_mapLayoutControl.IsHorizontalScrollbarVisible = true;

                  m_mapLayoutControl.IsVerticalScrollbarVisible = true;

           }

           catch(Exception ex)

           {

                  Trace.WriteLine(ex.Message); 

           }

    }

 

71 界面初始化示意图

7.3     布局操作相关功能实现

对布局的保存、加载、清空以及将布局保存为模板、加载布局模板等功能在“布局操作”菜单以及“清空布局窗口”按钮中,如图 7‑2所示:

72 示范程序的“布局操作”相关菜单

保存布局:

    public void SaveLayout()

    {

           //获取可用的布局名称、布局的 XML 描述信息

           string layoutName=m_workspace.Layouts.GetAvailableLayoutName("layout");

           string xml = m_mapLayoutControl.MapLayout.ToXML();

 

           //将布局添加到布局集合中

           m_workspace.Layouts.Add(layoutName, xml);

           m_workspace.Save();

    }

加载布局,由于示范数据中本身并无布局,因此该功能仅在“保存布局”后可用:

    public void LoadLayout()

    {

           string layoutName = m_workspace.Layouts[0];

 

           MapLayout mapLayout = m_mapLayoutControl.MapLayout;

           mapLayout.Workspace = m_workspace;

           mapLayout.Open(layoutName);

    }

布局保存为模板

    public void SaveAsTemplate()

    {

           string layoutTemplateName = @"../../Project/SampleData/China/template.ult";

           m_mapLayoutControl.MapLayout.SaveAsTemplate(layoutTemplateName);

    }

加载布局模板,由于示范数据中本身并无布局模板,因此该功能仅在“保存为模板”后可用:

    public void LoadTemplate()

    {

           string layoutTemplateName = @"../../Project/SampleData/China/template.ult";

           m_mapLayoutControl.MapLayout.LoadTemplate(layoutTemplateName);

    }

清空布局窗口,布局清空后,应将锁定地图、打印预览设置为非选中状态:

    public void ClearLayout()

    {

           m_mapLayoutControl.MapLayout.Elements.DeleteAll();

           m_mapLayoutControl.MapLayout.ZoomToPaper();

           m_mapLayoutControl.MapLayout.Refresh();

    }

7.4     布局元素相关功能实现

添加各种布局元素的功能集成在“绘制布局元素”菜单中,如图 7‑3所示:

73 示范程序“绘制布局元素”菜单

添加带复杂边框地图对象:

    public void AddGeoMap()

    {

           GeoMap geoMap = new GeoMap();

           geoMap.MapName = "2000年各省人口数分段专题图";

 

           //设置GeoMap对象的外切矩形

           Rectangle2D rectangle2D = new Rectangle2D(new Point2D(850,1250),new Size2D(1500,1500));

           GeoRectangle geoRectangle = new GeoRectangle(rectangle2D, 0);

           geoMap.Shape = geoRectangle;

 

           //设置GeoMap对象的MapBorder

           GeoMapBorder geoMapBorder=new GeoMapBorder();

           geoMapBorder.BorderType = GeoMapBorderType.Complex;

 

           //设置MapBorder的内框

           geoMapBorder.InFrameColor = Color.White;

           geoMapBorder.InFrameWidth = 0.5;

           geoMapBorder.InFrameInterval = 5;

 

           //设置MapBorder的内线

           geoMapBorder.InLineColor = Color.FromArgb(115, 115, 115);

           geoMapBorder.InLineWidth = 0.5;

           geoMapBorder.InLineInterval = 5;

 

           //设置MapBorder的外线

           geoMapBorder.OutLineColor = Color.White;

           geoMapBorder.OutLineWidth = 0.5;

           geoMapBorder.OutLineInterval = 30;

 

           //设置MapBorder的外框

           geoMapBorder.OutFrameColor = Color.FromArgb(115, 115, 115);

           geoMapBorder.OutFrameWidth = 1;

           geoMapBorder.OutFrameInterval = 2;

 

           //设置填充文本的风格

           TextStyle fillTextStyle = new TextStyle();

           fillTextStyle.Shadow = true;

           fillTextStyle.Alignment = TextAlignment.TopCenter;

           fillTextStyle.BackColor = Color.White;

           fillTextStyle.ForeColor = Color.FromArgb(135, 171, 217);

           fillTextStyle.BackOpaque = true;

           fillTextStyle.Bold = false;

           fillTextStyle.FontName = "宋体";

           fillTextStyle.FontHeight = 10.0;

           fillTextStyle.FontWidth = 10.0;

           fillTextStyle.IsSizeFixed = false;

           fillTextStyle.Weight = 100;

 

           //设置MapBorder的填充

           geoMapBorder.FillType = GeoMapBorderFillType.Text;

           geoMapBorder.FillDirection = FillDirectionType.Inner;

           geoMapBorder.FillText = " * ";

           geoMapBorder.FillTextStyle = fillTextStyle;

 

           //设置MapBorder的转角填充

           geoMapBorder.CornerFillType = GeoMapBorderFillType.Image;

           geoMapBorder.CornerFillImageFile =

                         @"../../Project/SampleData/China/WeatherReport/a18.bmp";

           geoMapBorder.CornerFillStartMode = CornerFillStartMode.LeftBottom;

 

           geoMap.MapBorder = geoMapBorder;

           geoMap.IsBorderVisible = true;

           m_mapLayoutControl.MapLayout.Elements.AddNew(geoMap);

    }     

添加标题:

    public void AddTitle()

    {

           TextStyle textStyle = new TextStyle();

 

           textStyle.Shadow=true;

           textStyle.Alignment=TextAlignment.TopCenter;

           textStyle.BackColor =Color.FromArgb(65, 65, 65);

           textStyle.ForeColor = Color.FromArgb(174, 241, 176);

           textStyle.BackOpaque=false;

           textStyle.Bold=true;

           textStyle.FontName="楷体";

           textStyle.FontHeight=50.0;

           textStyle.FontWidth=30.0;

           textStyle.IsSizeFixed=false;

           textStyle.Italic = true;

           textStyle.Outline=true;

           textStyle.Weight=500;

 

           TextPart textPart = new TextPart("2000年各省人口数分段专题图",new Point2D(850,2150),0);

           

           GeoText geoText = new GeoText(textPart, textStyle);

           m_mapLayoutControl.MapLayout.Elements.AddNew(geoText);

    }

在布局窗口中,由于添加地图比例尺、地图图例、地图指南针以及锁定地图操作均是针对当前选中的某一地图而言,因此在实现这些功能时,需要首先返回当前被选中的地图的ID

返回选中地图对象的ID,用于返回锁定地图、添加图例、比例尺、指南针等功能所对应的地图对象:

    public int GetMapID()

    {

           int mapID = -1;

           int count = 0;

 

           LayoutSelection layoutSelection = m_mapLayoutControl.MapLayout.Selection;

           LayoutElements layoutElements = m_mapLayoutControl.MapLayout.Elements;

           layoutElements.Refresh();

 

           for (int i = 0; i < layoutSelection.Count; i++)

           {

                  int ID = layoutSelection[i];

                  layoutElements.SeekID(ID);

                  Geometry geometry = layoutElements.GetGeometry();

                  if (geometry.Type == GeometryType.GeoMap)

                  {

                         mapID = ID;

                         count++;

                  }

                  if (count > 1)

                  {

                         mapID = -1;

                  }

           }

           return mapID;

    }

添加指南针,该功能仅在某一地图对象处于选中状态才可用:

    public void AddNorthArrow()

    {

           Rectangle2D rectangle2D=new Rectangle2D (

                         new Point2D(200,1900),new Size2D(150,150));

 

           GeoNorthArrow geoNorthArrow = new GeoNorthArrow(

                         NorthArrowStyleType.ArrowWithShadow, rectangle2D, 0);

 

           geoNorthArrow.BindingGeoMapID = GetMapID();

 

           m_mapLayoutControl.MapLayout.Elements.AddNew(geoNorthArrow);

    }

添加比例尺,该功能仅在某一地图对象处于选中状态才可用:

    public void AddScale()

    {

           GeoMapScale geoMapScale = new GeoMapScale(

                         GetMapID(),new Point2D(1050,515),550,18);

           geoMapScale.ScaleUnit = Unit.Kilometer;

           geoMapScale.LeftDivisionCount = 2;

           geoMapScale.SegmentCount = 2;

           geoMapScale.ScaleType = GeoMapScaleType.RailwayMidSplit;

 

           m_mapLayoutControl.MapLayout.Elements.AddNew(geoMapScale);

    }

添加图例,该功能仅在某一地图对象处于选中状态才可用:

    public void AddGeoLegend()

    {

           LayoutElements layoutElements = m_mapLayoutControl.MapLayout.Elements;

           layoutElements.SeekID(GetMapID());

           GeoMap geoMap=(GeoMap)layoutElements.GetGeometry();

           string geoMapName=geoMap.MapName;

 

           GeoLegend geoLegend=new GeoLegend (geoMapName ,m_workspace);

 

           geoLegend.Height = 175;

           geoLegend.Width = 950;

           geoLegend.Center = new Point2D(575,655);

 

           GeoStyle geoLegendStyle = new GeoStyle();

           geoLegendStyle.FillForeColor = Color.FromArgb(255,235,175);

           geoLegendStyle.FillOpaqueRate = 30;

           geoLegendStyle.LineWidth = 0.5;

           geoLegendStyle.LineColor = Color.FromArgb(65, 65, 65);

           geoLegend.BackGroundStyle = geoLegendStyle;

           geoLegend.ColumnCount = 3;

 

           //设置图例项和图例子项的说明文本的风格

           TextStyle geoLegendtextStyle = new TextStyle();

           geoLegendtextStyle.BackColor = Color.Yellow;

           geoLegendtextStyle.ForeColor = Color.Blue;

           geoLegendtextStyle.FontName = "宋体";

           geoLegendtextStyle.FontHeight = 20.0;

           geoLegendtextStyle.FontWidth = 12.0;

           geoLegendtextStyle.IsSizeFixed = false;

 

           geoLegend.ItemTextStyle = geoLegendtextStyle;

           geoLegend.SubItemTextStyle = geoLegendtextStyle;

 

           //设置图例标题风格

           TextStyle titleTextStyle = new TextStyle();

           titleTextStyle.BackColor = Color.Yellow;

           titleTextStyle.ForeColor = Color.Blue;

           titleTextStyle.FontName = "宋体";

           titleTextStyle.FontHeight = 40.0;

           titleTextStyle.FontWidth = 25.0;

           titleTextStyle.Italic = true;

           titleTextStyle.Bold = true;

           titleTextStyle.IsSizeFixed = false;

           titleTextStyle.Weight = 500;

 

           geoLegend.Title = "图例";

           geoLegend.TitleStyle = titleTextStyle;

           //将图例添加到布局图层,而非屏幕图层。

           geoLegend.Load(false);

 

           m_mapLayoutControl.MapLayout.Elements.AddNew(geoLegend);

    }

通过上述功能,实现了地图对象、比例尺、图例、指南针、地图标题的添加,效果如图 7‑4所示:

74 布局排版效果图

7.5     布局编辑相关功能实现

添加点状符号、线状符号、面状填充等功能集成在“编辑”菜单中,如图 7‑5所示:

75 示范程序“编辑”菜单

点状符号:

    public void CreatPoint()

    {

           m_mapLayoutControl.LayoutAction = Action.CreatePoint;

    }

线状符号:

    public void CreateLine()

    {

           m_mapLayoutControl.LayoutAction = Action.CreateLine;

    }

面状填充:

    public void CreatePolygon()

    {

           m_mapLayoutControl.LayoutAction = Action.CreatePolygon;

    }

7.6     输出为图片相关功能实现

输出为PDF等文件相关功能都集成在"输出为图片"菜单中,如图 7‑6所示:

76 示范程序“输出为图片”菜单

输出为PDF

    public void ExportToPDF()

    {

           //设置PDF文件输出项

           PDFOptions pdfOptions = new PDFOptions();

           pdfOptions.IsEntire = true;

           pdfOptions.IsLineStyleRetained = true;

           pdfOptions.IsPointStyleRetained = true;

           pdfOptions.IsRegionStyleRetained = true;

           pdfOptions.IsVector = true;

 

           SaveFileDialog saveFileDialog= new SaveFileDialog();

           saveFileDialog.Filter = "导出为PDF(*.pdf)|*.pdf";

           saveFileDialog.Title = "保存";

           

           if (saveFileDialog.ShowDialog()== DialogResult.OK)

           {

                  string pdfName = saveFileDialog.FileName;

                  m_mapLayoutControl.MapLayout.OutputLayoutToPDF(pdfName, pdfOptions);

           }

    }

输出为EMF

    public void ExportToEMF()

    {

           string emfName = @"../../Project/SampleData/China/maplayout.emf";

           m_mapLayoutControl.MapLayout.OutputLayoutToEMF(emfName);

    }

输出为EPS

    public void ExportToEPS()

    {

           string epsName = @"../../Project/SampleData/China/maplayout.eps";

           m_mapLayoutControl.MapLayout.OutputLayoutToEPS(epsName);

    }

输出为PNG

    public void ExportToPNG()

    {

           string pngName = @"../../Project/SampleData/China/maplayout.png";

           m_mapLayoutControl.MapLayout.OutputLayoutToPNG(pngName, true);

    }

输出为BMP

    public void ExportToBMP()

    {

           int bmpDPI = 500;

           string bmpName = @"../../Project/SampleData/China/maplayout.bmp";

           m_mapLayoutControl.MapLayout.OutputLayoutToBMP(bmpName, bmpDPI);

    }

输出为JPG

    public void ExportToJPG()

    {

           string jpgName = @"../../Project/SampleData/China/maplayout.jpg";

           m_mapLayoutControl.MapLayout.OutputLayoutToJPG(jpgName);

    }

7.7     布局浏览相关功能及实现

锁定地图等布局浏览功能在菜单中的位置,如图 7‑7所示:

77 示范程序布局浏览功能

由于放大、缩小、平移、全幅显示功能,在是否“锁定地图”时,作用对象不同,当地图没有锁定时,作用对象为整个布局,当地图锁定时,作用对象为锁定的地图。此外,“锁定地图”功能,需在某一地图处于选中状态才可用。

锁定地图,该功能仅在某一地图对象处于选中状态才可用,代码实现如下:

    public void LockMap(bool islock)

    {

           int mapID = -1;

           //islock 表示地图是否锁定

           if (islock)

           {

                  mapID = GetMapID();

           }

           m_mapLayoutControl.ActiveGeoMapID = mapID;

    }

放大功能实现代码:

    public void ZoomIn(bool isLock)

    {

           //islock 表示地图是否锁定

           if (isLock)

           {

                  m_mapLayoutControl.MapAction = Action.ZoomIn;

           }

           else

           {

                  m_mapLayoutControl.LayoutAction = Action.ZoomIn;

           }

    }

缩小功能实现代码:

    public void ZoomOut(bool isLock)

    {

           //islock 表示地图是否锁定

           if (isLock)

           {

                  m_mapLayoutControl.MapAction = Action.ZoomOut;

           }

           else

           {

                  m_mapLayoutControl.LayoutAction = Action.ZoomOut;

           }

    }

平移功能实现代码:

    public void Pan(bool isLock)

    {

           //islock 表示地图是否锁定

           if (isLock)

           {

                  m_mapLayoutControl.MapAction = Action.Pan;

           }

           else

           {

                  m_mapLayoutControl.LayoutAction = Action.Pan;

           }

    }

全幅显示功能实现代码:

    public void ViewEntire(bool isLock)

    {

           //islock 表示地图是否锁定

           if (isLock)

           {

                  Map map=m_mapLayoutControl.ActiveMap;

                  map.ViewEntire();

                  m_mapLayoutControl.MapLayout.Refresh();

           }

           else

           {

                  m_mapLayoutControl.MapLayout.ZoomToPaper();

           }

    }

7.8     打印相关功能及实现

布局预览及布局打印功能在菜单中的位置,如图 7‑8所示:

78 示范程序布局打印功能

打印预览功能实现代码:

    public void PrintPreview(bool check)

    {

           if (check == true)

           {

                  m_mapLayoutControl.MapLayout.PrintPreview=true ;

                  m_mapLayoutControl.MapLayout.Refresh();

           }

           else

           {

                  m_mapLayoutControl.MapLayout.PrintPreview = false;

                  m_mapLayoutControl.MapLayout.Refresh();

           }

    }

打印布局功能实现代码:

    public void PrintLayout()

    {

           Printer printer = m_mapLayoutControl.MapLayout.Printer;

           printer.PaperSize = PaperSize.A4;

           printer.Orientation = PaperOrientation.Landscape;

 

           //设置页边距

           PaperMargin paperMargin = new PaperMargin(70);

           paperMargin.Left = 100;

           paperMargin.Right = 100;

           printer.Margin = paperMargin;

           

           //打印机设置

           printer.DeviceDPI = 72;

           printer.IsVectorPrint = true;

           printer.PrinterName = "Microsoft XPS Document Writer";

 

           //打印布局

           if (printer.IsValidPrinter)

           {

                  MessageBox.Show("可以打印!");

           }

           else

           {

                  MessageBox.Show("打印机不合法!");

           }

 

    }

 

 


  版权所有© 北京超图软件股份有限公司。保留所有权利。