using System.Collections.ObjectModel; using System.Web; using System.Web.Mvc; using PdfSharp; using PdfSharp.Pdf; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.MvcEdition; using System.Drawing; using System.IO; using System.Drawing.Imaging; namespace CSharp_HowDoISamples { public partial class MiscellaneousController : Controller { // // GET: /UsePdfExtension/ public ActionResult UsePdfExtension() { Map map = new Map("Map1", new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage)); map.MapUnit = GeographyUnit.Meter; map.CurrentExtent = new RectangleShape(-10000000, 10000000, 10000000, -10000000); map.MapBackground = new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"))); map.StaticOverlay.Layers.Add(new GoogleMapsLayer()); SimpleMarkerOverlay markerOV = new SimpleMarkerOverlay(); Marker marker = new Marker(-8922952.93266, 2984101.58384); marker.WebImage = new WebImage("../content/theme/default/img/marker_blue.gif"); markerOV.Markers.Add(marker); map.CustomOverlays.Add(markerOV); return View(map); } [MapActionFilter] public string ToPdf(Map map, GeoCollection args) { PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); string selectedItemText = args[0].ToString(); if (selectedItemText == "Landscape") { page.Orientation = PageOrientation.Landscape; } PdfGeoCanvas pdfGeoCanvas = new PdfGeoCanvas(); RectangleShape rect = new RectangleShape(double.Parse(args["left"].ToString()), double.Parse(args["top"].ToString()), double.Parse(args["right"].ToString()), double.Parse(args["bottom"].ToString())); map.CurrentExtent = rect; pdfGeoCanvas.BeginDrawing(page, rect, GeographyUnit.DecimalDegree); Bitmap bitmap = map.GetBitmap(); MemoryStream stream = new MemoryStream(); bitmap.Save(stream, ImageFormat.Png); PointShape centerPoint = pdfGeoCanvas.CurrentWorldExtent.GetCenterPoint(); pdfGeoCanvas.DrawWorldImageWithoutScaling(new GeoImage(stream), centerPoint.X, centerPoint.Y, DrawingLevel.LabelLevel); pdfGeoCanvas.EndDrawing(); string relativePath = string.Format("~/Controllers/{0}/{1}", ControllerContext.RouteData.Values["Controller"], "MapSuite PDF Map.pdf"); string filename = Server.MapPath(relativePath); document.Save(filename); return VirtualPathUtility.ToAbsolute(relativePath); } } }