Hi
i am working in mvc and and printing map to pdf
Issue i am facing is i am trying to add scale bar on my pdf after printing …
It adds ShapeOverlay, WorldMap and LayersOverLay correctly
But when i try to add adornment layer and and marker over it gives me error
“The given key was not present in the dictionary.”
how can we loop through in memorymarkeroverlay to make them visible when it goes for printing
Here is the print function
public string MapPrint(Map map, GeoCollection<object> args)
{
PdfGeoCanvas pdfGeoCanvas = new PdfGeoCanvas();
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
page.Orientation = PageOrientation.Portrait;
page.Size = PageSize.A4;
string font = "Roboto";
string Area = "";
List<LayerOverlay> lstlayeroverlay = new List<LayerOverlay>();
List<InMemoryMarkerOverlay> lstmarkeroverlay
= new List<InMemoryMarkerOverlay>();
//AdornmentOverlay adornmentoverlay = new
AdornmentOverlay();
List<LegendResult> lstlegend = new List<LegendResult>();
lstlayeroverlay.Add((LayerOverlay)map.CustomOverlays["WorldMap"]);
lstlayeroverlay.Add((LayerOverlay)map.CustomOverlays["LayersOverlay"]);
lstlayeroverlay.Add((LayerOverlay)map.CustomOverlays["ShapeOverlay"]);
//
lstlayeroverlay.Add((LayerOverlay)map.CustomOverlays["ScaleBar"]);
lstmarkeroverlay.Add((InMemoryMarkerOverlay)map.CustomOverlays["GirlsMarkerOverlay"]);
lstmarkeroverlay.Add((InMemoryMarkerOverlay)map.CustomOverlays["BoysMarkerOverlay"]);
lstmarkeroverlay.Add((InMemoryMarkerOverlay)map.CustomOverlays["MixMarkerOverlay"]);
double area = shapeArea;
var popupdata = map.Popups[0];
XGraphics graphics = XGraphics.FromPdfPage(page);
int headerHeight = 70;
string title = args["title"].ToString();
string subTitle = args["subtitle"].ToString();
string legend = args["legend"].ToString();
if (Session["ShapeArea"] != null)
{
Area = Session["ShapeArea"].ToString();
}
if (legend != "undefined")
{
lstlegend = (new JavaScriptSerializer()).Deserialize<List<LegendResult(args["legend"].ToString());
}
try
{
graphics.DrawRectangle(Brushes.White, new Rectangle(0, 0, (int)page.Width, (int)headerHeight));
graphics.DrawLine(new XPen(XColor.FromKnownColor(KnownColor.Black), 1), 0, (int)headerHeight + 2, (int)page.Width, (int)headerHeight + 1);
graphics.DrawImage(XImage.FromFile(Server.MapPath("~/images/MoE-logo.png")), new XRect(0, 0, headerHeight,
headerHeight));
graphics.DrawString(title, new XFont(font, 20, XFontStyle.Bold), XBrushes.Black, new XRect(0, 5, page.Width, (int)headerHeight / 3), XStringFormats.Center);
graphics.DrawString(subTitle, new XFont(font, 16, XFontStyle.Bold), XBrushes.Black, new XRect(0, (int)headerHeight / 3,
page.Width, (int)headerHeight / 3), XStringFormats.Center);
graphics.DrawImage(XImage.FromFile(Server.MapPath("~/images/MoE-logo.png")), new XRect(page.Width -
headerHeight, 0, headerHeight, headerHeight));
}
finally
{
graphics.Dispose();
}
int FooterHeight = 50;
int margin = 5;
headerHeight = headerHeight +
2+margin;
int MapHeight = Convert.ToInt32(page.Height - (headerHeight +
FooterHeight));
pdfGeoCanvas.DrawingArea = new Rectangle(0, headerHeight, (int)page.Width,
MapHeight);
Collection<SimpleCandidate> labelsInLayers = new Collection<SimpleCandidate>();
RectangleShape rect = new RectangleShape(double.Parse(args["left"].ToString()), double.Parse(args["top"].ToString()), double.Parse(args["right"].ToString()), double.Parse(args["bottom"].ToString()));
foreach (LayerOverlay layeroverlay in lstlayeroverlay)
{
foreach (Layer layer in layeroverlay.Layers)
{
layer.Open();
pdfGeoCanvas.BeginDrawing(page, rect, map.MapUnit);
layer.Draw(pdfGeoCanvas,
labelsInLayers);
layer.Close();
pdfGeoCanvas.EndDrawing();
}
}
if (Session["ShapeArea"] != null)
{
try
{
int Xpos =Convert.ToInt32(page.Width-160);
int Ypos = headerHeight+ MapHeight + 5;
graphics = XGraphics.FromPdfPage(page);
Rectangle shapeRect = new Rectangle(Xpos, Ypos,150, 20);
graphics.DrawRectangle(new SolidBrush(ColorTranslator.FromHtml("#3F4B5B")), Rectangle.Round(shapeRect));
graphics.DrawString("Shape Area: " + Area + "km²", new XFont(font, 10,XFontStyle.Regular),XBrushes.White, shapeRect, XStringFormats.TopCenter);
}
finally
{
graphics.Dispose();
}
}
try
{
graphics = XGraphics.FromPdfPage(page);
double xpos = page.Width - 48;
int ypos = headerHeight + 5;
graphics.DrawImage(XImage.FromFile(Server.MapPath("~/images/narrow.png")), new XRect(xpos, ypos,30,30));
}
finally {
graphics.Dispose();
}
headerHeight = headerHeight + 10;
int YFooter = headerHeight + MapHeight+2;
int HFooter = Convert.ToInt32(page.Height - YFooter);
int counter = 1;
if (legend != "undefined")
{
foreach (var data in lstlegend)
{
try
{
graphics = XGraphics.FromPdfPage(page);
double rectX = page.Width /5;
graphics.DrawRectangle(new SolidBrush(ColorTranslator.FromHtml(data.Color)),
new XRect(rectX + counter - 10,
YFooter, 8, 8));
//////////to make
boundaries fo the rows
//graphics.DrawRectangle(new
XPen(XColor.FromKnownColor(KnownColor.Black), .5), new XRect(rectX - 12,
YFooter + counter-2, 100, 27));
graphics.DrawString(data.Start.ToString() + " - " +
data.End.ToString(), new XFont(font, 7, XFontStyle.Regular),
XBrushes.Black, new XRect(rectX + counter,
YFooter, 17, 15), XStringFormats.TopLeft);
////////////to display
in table decrease increment in counter that is 60
counter = counter + 10;
}
finally
{
graphics.Dispose();
}
}
}
return VirtualPathUtility.ToAbsolute(filepath);
}
Please mention if i am wrong somewhere
Regards