Example of Print and Export in ASP.NET MVC Diagram Control
Print and Export: save the diagram as a picture (PNG or JPG) or as SVG, or print it. Pictures from other sites in a diagram are left out of PNG and JPG.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Print and Export: save the diagram as a picture (PNG or JPG) or as SVG, or print it. Pictures from other sites in a diagram are left
out of PNG and JPG.
</p>
<div class="d-flex flex-wrap gap-2 align-items-center mb-2">
<label class="d-flex align-items-center gap-1">Format
<select class="form-select form-select-sm" id="exportFormat"><option>PNG</option><option>JPG</option><option>SVG</option></select>
</label>
<label class="d-flex align-items-center gap-1">File name
<input class="form-control form-control-sm" id="exportName" value="Diagram" style="width:10em">
</label>
<button type="button" class="btn btn-sm btn-outline-primary" id="exportButton">Export</button>
<button type="button" class="btn btn-sm btn-outline-primary" id="printButton">Print</button>
</div>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
function diagram() { return Lumen.get('exportDiagram'); }
document.addEventListener('click', function (e) {
if (!e.target || !e.target.id) { return; }
if (e.target.id === 'exportButton') {
var format = document.getElementById('exportFormat').value;
var name = document.getElementById('exportName').value || 'Diagram';
diagram().exportDiagram({ format: format, fileName: name, mode: 'Download' })
.then(function () { status('Exported as ' + format + '.'); })
.catch(function (err) { status(err.message); });
} else if (e.target.id === 'printButton') {
diagram().print().then(function () { status('Sent to the printer.'); });
}
});
return {};
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DiagramController : Controller
{
// The page is Views/Diagram/PrintAndExport.cshtml; it draws what ViewData holds.
public ActionResult PrintAndExport()
{
UiDiagramOptions o = FlowChart();
o.Id = "exportDiagram";
o.Height = "460px";
o.AriaLabel = "Diagram to print and export";
o.OnSelectionChange = null;
ViewData["diagram"] = o;
return View();
}
private static UiDiagramNode Node(string id, double x, double y, string text, UiDiagramShapeType type = UiDiagramShapeType.Flow, string shape = "Process",
double width = 120, double height = 54, string fill = null)
{
var node = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = width, Height = height,
Shape = new UiDiagramShape { Type = type, Shape = shape },
Style = new UiDiagramStyle { Fill = fill }
};
if (text != null) { node.Annotations.Add(new UiDiagramAnnotation { Content = text }); }
return node;
}
private static UiDiagramConnector Link(string id, string from, string to, string label = null,
UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal)
{
var c = new UiDiagramConnector { Id = id, SourceID = from, TargetID = to, Type = type };
if (label != null) { c.Annotations.Add(new UiDiagramAnnotation { Content = label, Offset = new UiDiagramPoint { X = 0.5, Y = 0 } }); }
return c;
}
/// <summary>An order process, as a flow chart.</summary>
private static UiDiagramOptions FlowChart()
{
return new UiDiagramOptions
{
Id = "flowChart",
Height = "620px",
AriaLabel = "Order process flow chart",
Nodes = new List<UiDiagramNode>
{
Node("start", 300, 50, "New order", UiDiagramShapeType.Flow, "Terminator", fill: "#d9f2e6"),
Node("check", 300, 150, "Check stock", fill: "#e8f0fe"),
Node("inStock", 300, 270, "In stock?", UiDiagramShapeType.Flow, "Decision", 130, 80, "#fff4ce"),
Node("reorder", 520, 270, "Reorder from supplier", fill: "#e8f0fe"),
Node("pack", 300, 390, "Pack and ship", fill: "#e8f0fe"),
Node("invoice", 300, 490, "Send invoice", UiDiagramShapeType.Flow, "Document", fill: "#f3e8fd"),
Node("end", 300, 580, "Done", UiDiagramShapeType.Flow, "Terminator", fill: "#d9f2e6")
},
Connectors = new List<UiDiagramConnector>
{
Link("c1", "start", "check"), Link("c2", "check", "inStock"), Link("c3", "inStock", "pack", "Yes"), Link("c4", "inStock", "reorder", "No"),
Link("c5", "reorder", "check"), Link("c6", "pack", "invoice"), Link("c7", "invoice", "end")
},
OnSelectionChange = "DiagramSamples.selected"
};
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>