Print and Export in ASP.NET MVC Maps component
17 Feb 20229 minutes to read
The rendered maps can be printed directly from the browser by calling the print
method. To use the print functionality, set the AllowPrint
property to true.
@using Syncfusion.EJ2;
@Html.EJS().Button("togglebtn").IconCss("e-icons e-play-icon").Content("Print").CssClass("e-flat").Render()
@Html.EJS().Maps("maps").Height("450px").AllowPrint(true).Width("650px").Layers(l =>
{
l.TooltipSettings(ts => ts.ValuePath("name").Visible(true))
.DataLabelSettings(ds => ds.Visible(true).LabelPath("name").SmartLabelMode(Syncfusion.EJ2.Maps.SmartLabelMode.Trim))
.ShapeSettings(ss => ss.Autofill(true)).ShapeData(ViewBag.ShapeData).Add();
}).Render()
<script>
document.getElementById('togglebtn').onclick = function () {
var map = document.getElementById('maps').ej2_instances[0];
map.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.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
@using Syncfusion.EJ2;
@Html.EJS().Button("togglebtn").IconCss("e-icons e-play-icon").Content("Export").CssClass("e-flat").Render()
@Html.EJS().Maps("maps").Height("450px").AllowImageExport(true).Width("650px").Layers(l => {
l.TooltipSettings(ts => ts.ValuePath("name").Visible(true))
.DataLabelSettings(ds => ds.Visible(true).LabelPath("name").SmartLabelMode(Syncfusion.EJ2.Maps.SmartLabelMode.Trim))
.ShapeSettings(ss =>ss.Autofill(true)).ShapeData(ViewBag.ShapeData).Add();
}).Render()
<script>
document.getElementById('togglebtn').onclick = function () {
var map = document.getElementById('maps').ej2_instances[0];
map.export('PNG', 'Export');
};
</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.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.
@using Syncfusion.EJ2;
@Html.EJS().Button("togglebtn").IconCss("e-icons e-play-icon").Content("Export").CssClass("e-flat").Render()
@Html.EJS().Maps("maps").Height("450px").AllowImageExport(true).Width("650px").Layers(l => {
l.TooltipSettings(ts => ts.ValuePath("name").Visible(true))
.DataLabelSettings(ds => ds.Visible(true).LabelPath("name").SmartLabelMode(Syncfusion.EJ2.Maps.SmartLabelMode.Trim))
.ShapeSettings(ss =>ss.Autofill(true)).ShapeData(ViewBag.ShapeData).Add();
}).Render()
<script>
document.getElementById('togglebtn').onclick = function () {
var map = document.getElementById('maps').ej2_instances[0];
map.export('PNG', 'Export');
};
</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.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.
@using Syncfusion.EJ2;
@Html.EJS().Button("togglebtn").IconCss("e-icons e-play-icon").Content("Export").CssClass("e-flat").Render()
@Html.EJS().Maps("maps").Height("450px").AllowImageExport(true).Width("650px").Layers(l => {
l.TooltipSettings(ts => ts.ValuePath("name").Visible(true))
.DataLabelSettings(ds => ds.Visible(true).LabelPath("name").SmartLabelMode(Syncfusion.EJ2.Maps.SmartLabelMode.Trim))
.ShapeSettings(ss =>ss.Autofill(true)).ShapeData(ViewBag.ShapeData).Add();
}).Render()
<script>
document.getElementById('togglebtn').onclick = function () {
var map = document.getElementById('maps').ej2_instances[0];
map.export('PNG', 'Export');
};
</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.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
@using Syncfusion.EJ2;
@Html.EJS().Button("togglebtn").IconCss("e-icons e-play-icon").Content("Export").CssClass("e-flat").Render()
@Html.EJS().Maps("maps").AllowImageExport(true).Layers(l=> {
l.UrlTemplate("https://tile.openstreetmap.org/level/tileX/tileY.png").Add();
}).Render()
<script>
document.getElementById('togglebtn').onclick = function () {
var map = document.getElementById('maps').ej2_instances[0];
map.export('PNG', 'Export');
};
</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.GetWorldMap();
return View();
}
public object GetWorldMap()
{
string allText = System.IO.File.ReadAllText("./wwwroot/scripts/MapsData/world_map.js");
return JsonConvert.DeserializeObject(allText);
}
}
}