Search results

Print and Export in ASP.NET Core Maps control

Print

To use the print functionality, we should set the allowPrint property to true. The rendered maps can be printed directly from the browser by calling the method print.

tagHelper
print.cs
@using Syncfusion.EJ2;
<div id="map">
    <button id="export">Export</button>
    <ejs-maps id="maps" load="onMapLoad"  allowPrint="true">
        <e-maps-layers>
        <e-maps-layer shapeData="ViewBag.MapsData">
        </e-maps-layer>
        </e-maps-layers>
    </ejs-maps>
</div>

<script>
    function onMapLoad(args) {
        window.maps = args.maps;
    }
    window.onload = function () {
        document.getElementById("export").onclick = () => {
            window.maps.print();
        };
    };
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewBag.shapeData = this.WorldMap();
            ViewBag.dataSource = this.getUNOData();
            ViewBag.MapsData = GetWorldMap();
            return View();
        }     
        public object GetWorldMap()
        {
            string allText = System.IO.File.ReadAllText("./wwwroot/scripts/MapsData/world_map.js");
            return JsonConvert.DeserializeObject(allText);
        }   
    }
}

Export

Image Export

To use the image export functionality, we should set the allowImageExport property to true. The rendered maps can be exported as an image using the export method. The method requires two parameters: image type and file name. The maps can be exported as an image in the following formats.

  • JPEG
  • PNG
  • SVG
tagHelper
export.cs
@using Syncfusion.EJ2;
<div id="map">
    <button id="export">Export</button>
    <ejs-maps id="maps" load="onMapLoad"  allowImageExport="true">
        <e-maps-layers>
        <e-maps-layer shapeData="ViewBag.MapsData">
        </e-maps-layer>
        </e-maps-layers>
    </ejs-maps>
</div>

<script>
    function onMapLoad(args) {
        window.maps = args.maps;
    }
    window.onload = function () {
        document.getElementById("export").onclick = () => {
            window.maps.export('PNG', 'Maps');
        };
    };
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewBag.shapeData = this.WorldMap();
            ViewBag.dataSource = this.getUNOData();
            ViewBag.MapsData = GetWorldMap();
            return View();
        }     
        public object GetWorldMap()
        {
            string allText = System.IO.File.ReadAllText("./wwwroot/scripts/MapsData/world_map.js");
            return JsonConvert.DeserializeObject(allText);
        }   
    }
}

We can get the image file as base64 string for the JPEG and PNG formats. The maps can be exported to image as a base64 string using the export method. There are four parameters required: image type, file name, orientation of the exported PDF document which must be set as null for image export and finally allowDownload which should be set as false to return base64 string.

tagHelper
export.cs
@using Syncfusion.EJ2;
<div id="map">
    <button id="export">Export</button>
    <ejs-maps id="maps" load="onMapLoad"  allowImageExport="true">
        <e-maps-layers>
        <e-maps-layer shapeData="ViewBag.MapsData">
        </e-maps-layer>
        </e-maps-layers>
    </ejs-maps>
</div>

<script>
    function onMapLoad(args) {
        window.maps = args.maps;
    }
    window.onload = function () {
        document.getElementById("export").onclick = () => {
            window.maps.export('PNG', 'Maps');
        };
    };
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewBag.shapeData = this.WorldMap();
            ViewBag.dataSource = this.getUNOData();
            ViewBag.MapsData = GetWorldMap();
            return View();
        }     
        public object GetWorldMap()
        {
            string allText = System.IO.File.ReadAllText("./wwwroot/scripts/MapsData/world_map.js");
            return JsonConvert.DeserializeObject(allText);
        }   
    }
}

PDF Export

To use the PDF export functionality, we should set the allowPdfExport property to true. The rendered maps can be exported as PDF using the export method. The export method requires three parameters: file type, file name and orientation of the PDF document. The orientation setting is optional and “0” indicates portrait and “1” indicates landscape.

tagHelper
export.cs
@using Syncfusion.EJ2;
<div id="map">
    <button id="export">Export</button>
    <ejs-maps id="maps" load="onMapLoad"  allowImageExport="true">
        <e-maps-layers>
        <e-maps-layer shapeData="ViewBag.MapsData">
        </e-maps-layer>
        </e-maps-layers>
    </ejs-maps>
</div>

<script>
    function onMapLoad(args) {
        window.maps = args.maps;
    }
    window.onload = function () {
        document.getElementById("export").onclick = () => {
            window.maps.export('PNG', 'Maps');
        };
    };
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewBag.shapeData = this.WorldMap();
            ViewBag.dataSource = this.getUNOData();
            ViewBag.MapsData = GetWorldMap();
            return View();
        }     
        public object GetWorldMap()
        {
            string allText = System.IO.File.ReadAllText("./wwwroot/scripts/MapsData/world_map.js");
            return JsonConvert.DeserializeObject(allText);
        }   
    }
}

Export the tile maps

The rendered map with providers such as OSM, Bing and Google static maps can be exported using the export method. It supports the following export formats.

  • JPEG
  • PNG
  • PDF
tagHelper
export-tile.cs
@using Syncfusion.EJ2;
<div id="map">
    <button id="export">Export</button>
    <ejs-maps id="maps" load="onMapLoad"  allowImageExport="true">
        <e-maps-layers>
        <e-maps-layer layerType="@Syncfusion.EJ2.Maps.ShapeLayerType.OSM">
        </e-maps-layer>
        </e-maps-layers>
    </ejs-maps>
</div>

<script>
    function onMapLoad(args) {
        window.maps = args.maps;
    }
    window.onload = function () {
        document.getElementById("export").onclick = () => {
            window.maps.export('PNG', 'Maps');
        };
    };
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewBag.shapeData = this.WorldMap();
            ViewBag.dataSource = this.getUNOData();
            ViewBag.MapsData = GetWorldMap();
            return View();
        }     
        public object GetWorldMap()
        {
            string allText = System.IO.File.ReadAllText("./wwwroot/scripts/MapsData/world_map.js");
            return JsonConvert.DeserializeObject(allText);
        }   
    }
}