/// <summary> /// Creates an image to export. /// </summary> /// <param name="exportItem"></param> /// <param name="fileinfo"></param> /// <returns>Image</returns> public static Image GetImage(MapExportItem exportItem) { MapExporter MapExporter = new MapExporter(exportItem); MapExporter.AddWMSLayers(exportItem.wmsLayers); MapExporter.AddVectorLayers(exportItem.vectorLayers); double left = exportItem.bbox[0]; double right = exportItem.bbox[1]; double bottom = exportItem.bbox[2]; double top = exportItem.bbox[3]; Envelope envelope = new Envelope(left, right, bottom, top); MapExporter.map.ZoomToBox(envelope); var width = Math.Abs(left - right); var scale = MapExporter.map.GetMapScale(exportItem.resolution); Image i = MapExporter.map.GetMap(exportItem.resolution); Bitmap src = new Bitmap(i); src.SetResolution(exportItem.resolution, exportItem.resolution); Bitmap target = new Bitmap(src.Size.Width, src.Size.Height); target.SetResolution(exportItem.resolution, exportItem.resolution); Graphics g = Graphics.FromImage(target); g.FillRectangle(new SolidBrush(Color.White), 0, 0, target.Width, target.Height); g.DrawImage(src, 0, 0); return target; }
public string TIFF([System.Web.Http.FromBody] UploadData uploadData) { MapExportItem exportItem = JsonConvert.DeserializeObject <MapExportItem>(uploadData.data); TIFFCreator tiffCreator = new TIFFCreator(); Image img = tiffCreator.Create(exportItem); MemoryStream outStream = new MemoryStream(); ZipOutputStream zipStream = new ZipOutputStream(outStream); zipStream.SetLevel(3); ZipEntry imageEntry = new ZipEntry(ZipEntry.CleanName("kartexport.tiff")); imageEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(imageEntry); MemoryStream imageStream = new MemoryStream(imgToByteArray(img)); byte[] buffer = new byte[4096]; StreamUtils.Copy(imageStream, zipStream, buffer); imageStream.Close(); zipStream.CloseEntry(); ZipEntry worldFileEntry = new ZipEntry(ZipEntry.CleanName("kartexport.tfw")); worldFileEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(worldFileEntry); MemoryStream worldFileStream = new MemoryStream(MapImageCreator.CreateWorldFile(exportItem)); byte[] buffer2 = new byte[4096]; StreamUtils.Copy(worldFileStream, zipStream, buffer2); worldFileStream.Close(); zipStream.CloseEntry(); zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream. zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream. outStream.Position = 0; outStream.ToArray(); string[] fileInfo = byteArrayToFileInfo(outStream.ToArray(), "zip"); if (exportItem.proxyUrl != "") { return(exportItem.proxyUrl + "/Temp/" + fileInfo[1]); } else { return(Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/" + fileInfo[1]); } }
/// <summary> /// Create map and append the document with an image. /// </summary> /// <param name="chapter"></param> /// <returns>Path to created map image</returns> private string AppendMap(Chapter chapter) { MapSettings mapSettings = chapter.mapSettings; chapter.baseLayer = this.baseLayer; string[] layers = chapter.layers; if (layers != null && chapter.mapSettings.extent != null && layers.Length > 0) { if (chapter.baseLayer != null) { layers = (new string[] { chapter.baseLayer }).Union(chapter.layers).ToArray(); } LayerConfig layerConfig = this.LookupLayers(layers); MapExportItem mapExportItem = new MapExportItem(); int mapHeight = 500; int mapWidth = (int)(mapHeight * ((1 + Math.Sqrt(5)) / 2)); mapExportItem.size = new int[] { mapWidth, mapHeight }; mapExportItem.resolution = 90; double[] mapExtent = chapter.mapSettings.extent; mapExportItem.bbox = new double[] { mapExtent[0], mapExtent[2], mapExtent[1], mapExtent[3] }; mapExportItem.arcgisLayers = layerConfig.arcgislayers.Select(layer => layer.AsInfo(layer.zIndex == null ? 0 : (int)layer.zIndex) ).ToList(); mapExportItem.wmsLayers = layerConfig.wmslayers.Select(layer => layer.AsInfo(layer.zIndex == null ? 0 : (int)layer.zIndex) ).ToList(); mapExportItem.vectorLayers = layerConfig.vectorlayers.Select(layer => layer.AsInfo(layer.zIndex == null ? 0 : (int)layer.zIndex, new double[] { mapExtent[1], mapExtent[0], mapExtent[3], mapExtent[2] }) ).ToList(); try { System.Drawing.Image i = MapImageCreator.GetImage(mapExportItem); string mapImageGuid = Guid.NewGuid().ToString(); string path = String.Format("C:\\tmp\\{0}.png", mapImageGuid); i.Save(path, System.Drawing.Imaging.ImageFormat.Png); (new Task(() => { Thread.Sleep(20000); File.Delete(path); })).Start(); return(string.Format("<div><img src='{0}'/></div>", path)); } catch (Exception ex) { return(String.Empty); } } else { return(String.Empty); } }
private PageSize GetPageSize(MapExportItem exportItem) { PageSize p; if (Enum.TryParse(exportItem.format, true, out p)) { return(p); } throw new ApplicationException("Unknown page size"); }
public ActionResult PDF(string json) { MapExportItem exportItem = JsonConvert.DeserializeObject <MapExportItem>(json); AsyncManager.OutstandingOperations.Increment(); PDFCreator pdfCreator = new PDFCreator(); byte[] blob = pdfCreator.Create(exportItem); return(File(blob, "application/pdf", "kartutskrift.pdf")); }
public string PDF(string json) { MapExportItem exportItem = JsonConvert.DeserializeObject <MapExportItem>(json); AsyncManager.OutstandingOperations.Increment(); PDFCreator pdfCreator = new PDFCreator(); byte[] blob = pdfCreator.Create(exportItem); string[] fileInfo = byteArrayToFileInfo(blob, "pdf"); return(Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/" + fileInfo[1]); //return File(blob, "application/pdf", "kartutskrift.pdf"); }
public ActionResult TIFF(string json) { MapExportItem exportItem = JsonConvert.DeserializeObject <MapExportItem>(json); TIFFCreator tiffCreator = new TIFFCreator(); Image img = tiffCreator.Create(exportItem); MemoryStream outStream = new MemoryStream(); ZipOutputStream zipStream = new ZipOutputStream(outStream); zipStream.SetLevel(3); ZipEntry imageEntry = new ZipEntry(ZipEntry.CleanName("kartexport.tiff")); imageEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(imageEntry); MemoryStream imageStream = new MemoryStream(imgToByteArray(img)); byte[] buffer = new byte[4096]; StreamUtils.Copy(imageStream, zipStream, buffer); imageStream.Close(); zipStream.CloseEntry(); ZipEntry worldFileEntry = new ZipEntry(ZipEntry.CleanName("kartexport.tfw")); worldFileEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(worldFileEntry); MemoryStream worldFileStream = new MemoryStream(MapImageCreator.CreateWorldFile(exportItem)); byte[] buffer2 = new byte[4096]; StreamUtils.Copy(worldFileStream, zipStream, buffer2); worldFileStream.Close(); zipStream.CloseEntry(); zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream. zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream. outStream.Position = 0; return(File(outStream.ToArray(), "application/octet-stream", "kartexport.zip")); }
public string PDF(string json) { try { _log.DebugFormat("Received json: {0}", json); // try to decode input string to see if it is base64 encoded try { byte[] decoded = Convert.FromBase64String(json); json = System.Text.Encoding.UTF8.GetString(decoded); _log.DebugFormat("json after decode: {0}", json); } catch (Exception e) { _log.DebugFormat("Could not decode base64. Will treat as non-base64 encoded: {0}", e.Message); } MapExportItem exportItem = JsonConvert.DeserializeObject <MapExportItem>(json); AsyncManager.OutstandingOperations.Increment(); PDFCreator pdfCreator = new PDFCreator(); _log.Debug("Inited pdfcreator"); byte[] blob = pdfCreator.Create(exportItem); _log.Debug("created blob in pdfcreator"); string[] fileInfo = byteArrayToFileInfo(blob, "pdf"); _log.DebugFormat("Created fileinfo: {0}", fileInfo[1]); if (exportItem.proxyUrl != "") { return(exportItem.proxyUrl + "/Temp/" + fileInfo[1]); } else { return(Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/" + fileInfo[1]); } //return File(blob, "application/pdf", "kartutskrift.pdf"); } catch (Exception e) { _log.ErrorFormat("Unable to create PDF: {0}", e.Message); throw e; } }
public string PDF([System.Web.Http.FromBody] UploadData uploadData) { // try to decode input string to see if it is base64 encoded //try //{ // byte[] decoded = Convert.FromBase64String(json); // json = System.Text.Encoding.UTF8.GetString(decoded); // _log.DebugFormat("json after decode: {0}", json); //} //catch (Exception e) //{ // _log.DebugFormat("Could not decode base64. Will treat as non-base64 encoded: {0}", e.Message); //} string fontName = string.IsNullOrEmpty(ConfigurationManager.AppSettings["exportFontName"]) ? "Verdana" : ConfigurationManager.AppSettings["exportFontName"]; MapExportItem exportItem = new MapExportItem(); if (uploadData.json != null) { exportItem = JsonConvert.DeserializeObject <MapExportItem>(uploadData.json); } if (uploadData.data != null) { exportItem = JsonConvert.DeserializeObject <MapExportItem>(uploadData.data); } AsyncManager.OutstandingOperations.Increment(); PDFCreator pdfCreator = new PDFCreator(); byte[] blob = pdfCreator.Create(exportItem, fontName); string[] fileInfo = byteArrayToFileInfo(blob, "pdf"); if (!String.IsNullOrEmpty(exportItem.proxyUrl)) { return(exportItem.proxyUrl + "/Temp/" + fileInfo[1]); } else { return(Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/" + fileInfo[1]); } }
/// <summary> /// Create a worldfile for georeferencing. /// </summary> /// <param name="filename"></param> /// <param name="exportItem"></param> public static string createWorldFile(string filename, MapExportItem exportItem) { filename = filename.Replace(".tiff", ".tfw"); if (!File.Exists(filename)) { double left = exportItem.bbox[0]; double right = exportItem.bbox[1]; double bottom = exportItem.bbox[2]; double top = exportItem.bbox[3]; using (StreamWriter sw = File.CreateText(filename)) { /* Line 1: A: pixel size in the x-direction in map units/pixel Line 2: D: rotation about y-axis Line 3: B: rotation about x-axis Line 4: E: pixel size in the y-direction in map units, almost always negative[3] Line 5: C: x-coordinate of the center of the upper left pixel Line 6: F: y-coordinate of the center of the upper left pixel */ double mapWidth = Math.Abs(left - right); double mapHeight = Math.Abs(top - bottom); double pixelSizeX = mapWidth / exportItem.size[0]; double pixelSizeY = (-1) * (mapHeight / exportItem.size[1]); double x = exportItem.bbox[0]; double y = exportItem.bbox[3]; sw.WriteLine(pixelSizeX.ForceDecimalPoint()); sw.WriteLine(0); sw.WriteLine(0); sw.WriteLine(pixelSizeY.ForceDecimalPoint()); sw.WriteLine(x.ForceDecimalPoint()); sw.WriteLine(y.ForceDecimalPoint()); } } return filename; }
private string createPdf(Image img, string path, MapExportItem exportItem) { string filename = Guid.NewGuid() + ".pdf"; string localPdf = path + "\\" + filename; PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); page.Size = exportItem.format == "A4" ? PdfSharp.PageSize.A4 : PdfSharp.PageSize.A3; page.Orientation = exportItem.orientation == "L" ? PdfSharp.PageOrientation.Landscape : PdfSharp.PageOrientation.Portrait; XGraphics gfx = XGraphics.FromPdfPage(page); int scale = int.Parse(exportItem.scale); double length = (1.0 / scale); double unitLength = (length * 2.82e3); Dictionary<int, string> scaleBarTexts = new Dictionary<int, string>() { {250, "25 m"}, {500, "50 m"}, {1000, "50 m"}, {2500, "100 m"}, {5000, "200 m"}, {10000, "500 m"}, {25000, "1 km"}, {50000, "2 km"}, {100000, "5 km"}, {250000, "10 km"} }; Dictionary<int, int> scaleBarLengths = new Dictionary<int, int>() { {250, 25}, {500, 50}, {1000, 50}, {2500, 100}, {5000, 200}, {10000, 500}, {25000, 1000}, {50000, 2000}, {100000, 5000}, {250000, 10000} }; int displayLength = (int)(unitLength * scaleBarLengths.FirstOrDefault(a => a.Key == scale).Value); string displayText = scaleBarTexts.FirstOrDefault(a => a.Key == scale).Value; this.drawImage(gfx, img, 0, 0, page); List<string> copyrights = ConfigurationManager.AppSettings["exportCopyrightText"].Split(',').ToList(); string infoText = String.Empty; if (ConfigurationManager.AppSettings["exportInfoText"] != null) { infoText = ConfigurationManager.AppSettings["exportInfoText"]; } int height = 45 + copyrights.Count * 10; Point[] points = new Point[] { new Point(12, 12), new Point(12, height), new Point(55 + displayLength, height), new Point(55 + displayLength, 12), new Point(12, 12) }; gfx.DrawPolygon(XBrushes.White, points, XFillMode.Winding); this.drawText(gfx, String.Format("Skala 1:{0}", exportItem.scale), 15, 25); gfx.DrawLine(XPens.Black, new Point(15, 32), new Point(15 + displayLength, 32)); gfx.DrawLine(XPens.Black, new Point(15, 28), new Point(15, 36)); gfx.DrawLine(XPens.Black, new Point(15 + displayLength, 28), new Point(15 + displayLength, 36)); this.drawText(gfx, displayText, 20 + displayLength, 35); var y = (int)page.Height.Point - 15; this.drawText(gfx, infoText, 15, y); int i = 0; copyrights.ForEach(copyright => { int start = 50; this.drawText(gfx, String.Format("© {0}", copyright), 15, start + i * 10); i++; }); XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); gfx.DrawImage(logo, (gfx.PageSize.Width - logo.PixelWidth / 2) - 12, 12, logo.PixelWidth / 2, logo.PixelHeight / 2); document.Save(localPdf); return filename; }
/// <summary> /// Create a new ExportMap object. /// </summary> /// <param name="exportItem"></param> public MapExporter(MapExportItem exportItem) { this.exportItem = exportItem; var size = new Size(exportItem.size[0], exportItem.size[1]); this.map = new Map(size); }
public static void GetImageAsync(MapExportItem exportItem, Action<MapExportCallback> callback) { MapExporter mapExporter = new MapExporter(exportItem); mapExporter.AddWMSLayers(exportItem.wmsLayers); mapExporter.AddVectorLayers(exportItem.vectorLayers); mapExporter.AddWMTSLayers(exportItem.wmtsLayers, () => { Image img = mapExporter.map.GetMap(exportItem.resolution); Bitmap src = new Bitmap(img); src.SetResolution(exportItem.resolution, exportItem.resolution); Bitmap target = new Bitmap(src.Size.Width, src.Size.Height); target.SetResolution(exportItem.resolution, exportItem.resolution); Graphics g = Graphics.FromImage(target); g.FillRectangle(new SolidBrush(Color.White), 0, 0, target.Width, target.Height); g.DrawImage(src, 0, 0); callback.Invoke(new MapExportCallback() { image = img }); }); double left = exportItem.bbox[0]; double right = exportItem.bbox[1]; double bottom = exportItem.bbox[2]; double top = exportItem.bbox[3]; Envelope envelope = new Envelope(left, right, bottom, top); mapExporter.map.ZoomToBox(envelope); var width = Math.Abs(left - right); var scale = mapExporter.map.GetMapScale(exportItem.resolution); mapExporter.map.GetMap(exportItem.resolution); }
/// <summary> /// /// </summary> /// <param name="dataSet"></param> /// <returns></returns> public byte[] Create(MapExportItem exportItem) { return(this.createPdf(MapImageCreator.GetImage(exportItem), exportItem)); }
/// <summary> /// /// </summary> /// <param name="dataSet"></param> /// <returns></returns> public Image Create(MapExportItem exportItem) { return(MapImageCreator.GetImage(exportItem)); }
public byte[] Create(MapExportItem exportItem, string fontName) { return(this.createPdf(MapImageCreator.GetImage(exportItem), exportItem, fontName)); }
/// <summary> /// /// </summary> /// <param name="img"></param> /// <param name="path"></param> /// <param name="exportItem"></param> /// <returns>byte[]</returns> private byte[] createPdf(Image img, MapExportItem exportItem, string fontName) { PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); page.Size = GetPageSize(exportItem); page.Orientation = exportItem.orientation == "L" ? PdfSharp.PageOrientation.Landscape : PdfSharp.PageOrientation.Portrait; XGraphics gfx = XGraphics.FromPdfPage(page); int scale = int.Parse(exportItem.scale); double length = (1.0 / scale); double unitLength = (length * 2.82e3); Dictionary <int, string> scaleBarTexts = new Dictionary <int, string>() { { 250, "25 m" }, { 500, "50 m" }, { 1000, "50 m" }, { 2500, "100 m" }, { 5000, "200 m" }, { 10000, "500 m" }, { 25000, "1 km" }, { 50000, "2 km" }, { 100000, "5 km" }, { 250000, "10 km" } }; Dictionary <int, int> scaleBarLengths = new Dictionary <int, int>() { { 250, 25 }, { 500, 50 }, { 1000, 50 }, { 2500, 100 }, { 5000, 200 }, { 10000, 500 }, { 25000, 1000 }, { 50000, 2000 }, { 100000, 5000 }, { 250000, 10000 } }; int displayLength = GetDisplayLength(unitLength, scaleBarLengths, scale); string displayText = GetDisplayText(unitLength, scaleBarTexts, scale); // adding support for different layouts int layout = ConfigurationManager.AppSettings["exportLayout"] != null?int.Parse(ConfigurationManager.AppSettings["exportLayout"]) : 1; if (layout == 1)//original layout { //origina code from github this.drawImage(gfx, img, 0, 0, page); List <string> copyrights = new List <string>(); if (ConfigurationManager.AppSettings["exportCopyrightText"] != null) { copyrights = ConfigurationManager.AppSettings["exportCopyrightText"].Split(',').ToList(); } string infoText = String.Empty; if (ConfigurationManager.AppSettings["exportInfoText"] != null) { infoText = ConfigurationManager.AppSettings["exportInfoText"]; } int height = 45 + copyrights.Count * 10; XPoint[] points = new XPoint[] { new XPoint(12, 12), new XPoint(12, height), new XPoint(55 + displayLength, height), new XPoint(55 + displayLength, 12), new XPoint(12, 12) }; gfx.DrawPolygon(XBrushes.White, points, XFillMode.Winding); this.drawText(gfx, fontName, String.Format("Skala 1:{0}", exportItem.scale), 15, 25); gfx.DrawLine(XPens.Black, new XPoint(15, 32), new XPoint(15 + displayLength, 32)); gfx.DrawLine(XPens.Black, new XPoint(15, 28), new XPoint(15, 36)); gfx.DrawLine(XPens.Black, new XPoint(15 + displayLength, 28), new XPoint(15 + displayLength, 36)); this.drawText(gfx, fontName, displayText, 20 + displayLength, 35); var y = (int)page.Height.Point - 15; this.drawText(gfx, fontName, infoText, 15, y); int i = 0; copyrights.ForEach(copyright => { int start = 50; this.drawText(gfx, fontName, String.Format("© {0}", copyright), 15, start + i * 10); i++; }); XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); gfx.DrawImage(logo, (gfx.PageSize.Width - logo.PixelWidth / 2) - 12, 12, logo.PixelWidth / 2, logo.PixelHeight / 2); byte[] bytes; using (MemoryStream ms = new MemoryStream()) { document.Save(ms); bytes = ReadFully(ms); } return(bytes); } else if (layout == 2)//new layout { // x and y 0 0(top left corner?)-> change this.drawImage(gfx, img, 33, 33, page); List <string> copyrights = new List <string>(); if (ConfigurationManager.AppSettings["exportCopyrightText"] != null) { copyrights = ConfigurationManager.AppSettings["exportCopyrightText"].Split(',').ToList(); } string infoText = String.Empty; if (ConfigurationManager.AppSettings["exportInfoText"] != null) { infoText = ConfigurationManager.AppSettings["exportInfoText"]; } int height = 1; XPoint[] points = new XPoint[] { new XPoint(12, 12), new XPoint(12, height), new XPoint(55 + displayLength, height), new XPoint(55 + displayLength, 12), new XPoint(12, 12) }; gfx.DrawPolygon(XBrushes.White, points, XFillMode.Winding); // x y this.drawText(gfx, fontName, String.Format("Skala 1:{0}", exportItem.scale), 33, (int)page.Height.Point - 23, 8); gfx.DrawLine(XPens.Black, new XPoint(33, (int)page.Height.Point - 18), new XPoint(33 + displayLength, (int)page.Height.Point - 18)); gfx.DrawLine(XPens.Black, new XPoint(33, (int)page.Height.Point - 15), new XPoint(33, (int)page.Height.Point - 21)); gfx.DrawLine(XPens.Black, new XPoint(33 + displayLength / 2, (int)page.Height.Point - 17), new XPoint(33 + displayLength / 2, (int)page.Height.Point - 19)); gfx.DrawLine(XPens.Black, new XPoint(33 + displayLength, (int)page.Height.Point - 15), new XPoint(33 + displayLength, (int)page.Height.Point - 21)); this.drawText(gfx, fontName, displayText, 38 + displayLength, (int)page.Height.Point - 16, 8); var y = (int)page.Height.Point - 2; this.drawText(gfx, fontName, infoText, 33, y, 8); int i = 0; copyrights.ForEach(copyright => { int start = (int)page.Height.Point - 15; this.drawText(gfx, fontName, String.Format("© {0}", copyright), (int)page.Width.Point - 100, start + i * 10, 8); i++; }); XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); gfx.DrawImage(logo, (gfx.PageSize.Width - logo.PixelWidth / 5) - 33, 3.5, logo.PixelWidth / 5, logo.PixelHeight / 5); byte[] bytes; using (MemoryStream ms = new MemoryStream()) { document.Save(ms); bytes = ReadFully(ms); } return(bytes); } return(null); }
/// <summary> /// /// </summary> /// <param name="img"></param> /// <param name="path"></param> /// <param name="exportItem"></param> /// <returns>byte[]</returns> private byte[] createPdf(Image img, MapExportItem exportItem) { PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); page.Size = exportItem.format == "A4" ? PdfSharp.PageSize.A4 : PdfSharp.PageSize.A3; page.Orientation = exportItem.orientation == "L" ? PdfSharp.PageOrientation.Landscape : PdfSharp.PageOrientation.Portrait; XGraphics gfx = XGraphics.FromPdfPage(page); int scale = int.Parse(exportItem.scale); double length = (1.0 / scale); double unitLength = (length * 2.82e3); Dictionary <int, string> scaleBarTexts = new Dictionary <int, string>() { { 250, "25 m" }, { 500, "50 m" }, { 1000, "50 m" }, { 2500, "100 m" }, { 5000, "200 m" }, { 10000, "500 m" }, { 25000, "1 km" }, { 50000, "2 km" }, { 100000, "5 km" }, { 250000, "10 km" } }; Dictionary <int, int> scaleBarLengths = new Dictionary <int, int>() { { 250, 25 }, { 500, 50 }, { 1000, 50 }, { 2500, 100 }, { 5000, 200 }, { 10000, 500 }, { 25000, 1000 }, { 50000, 2000 }, { 100000, 5000 }, { 250000, 10000 } }; int displayLength = (int)(unitLength * scaleBarLengths.FirstOrDefault(a => a.Key == scale).Value); string displayText = scaleBarTexts.FirstOrDefault(a => a.Key == scale).Value; this.drawImage(gfx, img, 0, 0, page); List <string> copyrights = new List <string>(); if (ConfigurationManager.AppSettings["exportCopyrightText"] != null) { copyrights = ConfigurationManager.AppSettings["exportCopyrightText"].Split(',').ToList(); } string infoText = String.Empty; if (ConfigurationManager.AppSettings["exportInfoText"] != null) { infoText = ConfigurationManager.AppSettings["exportInfoText"]; } int height = 45 + copyrights.Count * 10; XPoint[] points = new XPoint[] { new XPoint(12, 12), new XPoint(12, height), new XPoint(55 + displayLength, height), new XPoint(55 + displayLength, 12), new XPoint(12, 12) }; gfx.DrawPolygon(XBrushes.White, points, XFillMode.Winding); this.drawText(gfx, String.Format("Skala 1:{0}", exportItem.scale), 15, 25); gfx.DrawLine(XPens.Black, new XPoint(15, 32), new XPoint(15 + displayLength, 32)); gfx.DrawLine(XPens.Black, new XPoint(15, 28), new XPoint(15, 36)); gfx.DrawLine(XPens.Black, new XPoint(15 + displayLength, 28), new XPoint(15 + displayLength, 36)); this.drawText(gfx, displayText, 20 + displayLength, 35); var y = (int)page.Height.Point - 15; this.drawText(gfx, infoText, 15, y); int i = 0; copyrights.ForEach(copyright => { int start = 50; this.drawText(gfx, String.Format("© {0}", copyright), 15, start + i * 10); i++; }); XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); gfx.DrawImage(logo, (gfx.PageSize.Width - logo.PixelWidth / 2) - 12, 12, logo.PixelWidth / 2, logo.PixelHeight / 2); byte[] bytes; using (MemoryStream ms = new MemoryStream()) { document.Save(ms); bytes = ReadFully(ms); } return(bytes); }
/// <summary> /// /// </summary> /// <param name="img"></param> /// <param name="path"></param> /// <param name="exportItem"></param> /// <returns>byte[]</returns> private byte[] createPdf(Image img, MapExportItem exportItem, string fontName) { PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); page.Size = GetPageSize(exportItem); page.Orientation = exportItem.orientation == "L" ? PdfSharp.PageOrientation.Landscape : PdfSharp.PageOrientation.Portrait; XGraphics gfx = XGraphics.FromPdfPage(page); int scale = int.Parse(exportItem.scale); double length = (1.0 / scale); double unitLength = (length * 2.82e3); Dictionary <int, string> scaleBarTexts = new Dictionary <int, string>() { { 250, "25 m" }, { 500, "50 m" }, { 1000, "50 m" }, { 2500, "100 m" }, { 5000, "200 m" }, { 10000, "500 m" }, { 25000, "1 km" }, { 50000, "2 km" }, { 100000, "5 km" }, { 250000, "10 km" } }; Dictionary <int, int> scaleBarLengths = new Dictionary <int, int>() { { 250, 25 }, { 500, 50 }, { 1000, 50 }, { 2500, 100 }, { 5000, 200 }, { 10000, 500 }, { 25000, 1000 }, { 50000, 2000 }, { 100000, 5000 }, { 250000, 10000 } }; int displayLength = GetDisplayLength(unitLength, scaleBarLengths, scale); string displayText = GetDisplayText(unitLength, scaleBarTexts, scale); // adding support for different layouts int layout = ConfigurationManager.AppSettings["exportLayout"] != null?int.Parse(ConfigurationManager.AppSettings["exportLayout"]) : 1; if (layout == 1)//original layout { //origina code from github this.drawImage(gfx, img, 0, 0, page); List <string> copyrights = new List <string>(); if (ConfigurationManager.AppSettings["exportCopyrightText"] != null) { copyrights = ConfigurationManager.AppSettings["exportCopyrightText"].Split(',').ToList(); } string infoText = String.Empty; if (ConfigurationManager.AppSettings["exportInfoText"] != null) { infoText = ConfigurationManager.AppSettings["exportInfoText"]; } int height = 45 + copyrights.Count * 10; XPoint[] points = new XPoint[] { new XPoint(12, 12), new XPoint(12, height), new XPoint(55 + displayLength, height), new XPoint(55 + displayLength, 12), new XPoint(12, 12) }; gfx.DrawPolygon(XBrushes.White, points, XFillMode.Winding); this.drawText(gfx, fontName, String.Format("Skala 1:{0}", exportItem.scale), 15, 25); gfx.DrawLine(XPens.Black, new XPoint(15, 32), new XPoint(15 + displayLength, 32)); gfx.DrawLine(XPens.Black, new XPoint(15, 28), new XPoint(15, 36)); gfx.DrawLine(XPens.Black, new XPoint(15 + displayLength, 28), new XPoint(15 + displayLength, 36)); this.drawText(gfx, fontName, displayText, 20 + displayLength, 35); var y = (int)page.Height.Point - 15; this.drawText(gfx, fontName, infoText, 15, y); int i = 0; copyrights.ForEach(copyright => { int start = 50; this.drawText(gfx, fontName, String.Format("© {0}", copyright), 15, start + i * 10); i++; }); XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); gfx.DrawImage(logo, (gfx.PageSize.Width - logo.PixelWidth / 2) - 12, 12, logo.PixelWidth / 2, logo.PixelHeight / 2); byte[] bytes; using (MemoryStream ms = new MemoryStream()) { document.Save(ms); bytes = ReadFully(ms); } return(bytes); } else if (layout == 2)//new layout { // x and y 0 0(top left corner?)-> change this.drawImage(gfx, img, 33, 33, page); List <string> copyrights = new List <string>(); if (ConfigurationManager.AppSettings["exportCopyrightText"] != null) { copyrights = ConfigurationManager.AppSettings["exportCopyrightText"].Split(',').ToList(); } string infoText = String.Empty; if (ConfigurationManager.AppSettings["exportInfoText"] != null) { infoText = ConfigurationManager.AppSettings["exportInfoText"]; } int height = 1; XPoint[] points = new XPoint[] { new XPoint(12, 12), new XPoint(12, height), new XPoint(55 + displayLength, height), new XPoint(55 + displayLength, 12), new XPoint(12, 12) }; gfx.DrawPolygon(XBrushes.White, points, XFillMode.Winding); // x y this.drawText(gfx, fontName, String.Format("Skala 1:{0}", exportItem.scale), 33, (int)page.Height.Point - 23, 8); gfx.DrawLine(XPens.Black, new XPoint(33, (int)page.Height.Point - 18), new XPoint(33 + displayLength, (int)page.Height.Point - 18)); gfx.DrawLine(XPens.Black, new XPoint(33, (int)page.Height.Point - 15), new XPoint(33, (int)page.Height.Point - 21)); gfx.DrawLine(XPens.Black, new XPoint(33 + displayLength / 2, (int)page.Height.Point - 17), new XPoint(33 + displayLength / 2, (int)page.Height.Point - 19)); gfx.DrawLine(XPens.Black, new XPoint(33 + displayLength, (int)page.Height.Point - 15), new XPoint(33 + displayLength, (int)page.Height.Point - 21)); this.drawText(gfx, fontName, displayText, 38 + displayLength, (int)page.Height.Point - 16, 8); var y = (int)page.Height.Point - 2; this.drawText(gfx, fontName, infoText, 33, y, 8); int i = 0; copyrights.ForEach(copyright => { int start = (int)page.Height.Point - 15; this.drawText(gfx, fontName, String.Format("© {0}", copyright), (int)page.Width.Point - 100, start + i * 10, 8); i++; }); XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); gfx.DrawImage(logo, (gfx.PageSize.Width - logo.PixelWidth / 5) - 33, 3.5, logo.PixelWidth / 5, logo.PixelHeight / 5); byte[] bytes; using (MemoryStream ms = new MemoryStream()) { document.Save(ms); bytes = ReadFully(ms); } return(bytes); } else if (layout == 3) { List <string> copyrights = new List <string>(); if (ConfigurationManager.AppSettings["exportCopyrightText"] != null) { copyrights = ConfigurationManager.AppSettings["exportCopyrightText"].Split(',').ToList(); } string infoText = String.Empty; if (ConfigurationManager.AppSettings["exportInfoText"] != null) { infoText = ConfigurationManager.AppSettings["exportInfoText"]; } // Get logoimage XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); // Set positions, areas, brushes and pens XRect margins = new XRect(33, 33, 33, 33); XSize stampSize = new XSize(200, 120); // Calculate positions and areas XRect drawingArea = new XRect(margins.Left, margins.Top, page.Width - (margins.Left + margins.Width), page.Height - (margins.Top + margins.Height)); XRect stampArea = new XRect(new XPoint(drawingArea.Left + 15, drawingArea.Bottom - stampSize.Height - 15), stampSize); XRect logoArea = new XRect(new XPoint(stampArea.Left + 10, stampArea.Top + 10), new XPoint(stampArea.Right - 10, stampArea.Top + 50)); XPoint signingLine = new XPoint(stampArea.Left + 10, logoArea.Bottom + 30); XPoint copyrightPosition = new XPoint(drawingArea.Right - 3, drawingArea.Bottom - 3); XPoint scalLegendPosition = new XPoint(stampArea.Left + 10, signingLine.Y + 20); double scaling; var scalingY = logo.PointHeight / logoArea.Height; var scalingX = logo.PointWidth / logoArea.Width; if (scalingX < scalingY) { scaling = scalingX; } else { scaling = scalingY; } // Pens, Brushes and colors XColor mainColor = XColor.FromArgb(0, 119, 188); XPen thickPen = new XPen(mainColor, 1); XPen thinPen = new XPen(mainColor, 0.5); // Draw map this.drawImage(gfx, img, drawingArea.Left, drawingArea.Top, page); // Put a border around map gfx.DrawRectangle(thickPen, drawingArea); // Draw a white "stamparea" gfx.DrawRectangle(thickPen, XBrushes.White, stampArea); // Draw logo gfx.DrawImage(logo, logoArea.Left, logoArea.Top, logo.PointWidth / scaling, logo.PointHeight / scaling); // Draw "signing line" gfx.DrawLine(thinPen, signingLine, new XPoint(stampArea.Right - 10, signingLine.Y)); // Draw scale legend gfx.DrawLine(XPens.Black, new XPoint(scalLegendPosition.X, scalLegendPosition.Y), new XPoint(scalLegendPosition.X + displayLength, scalLegendPosition.Y)); gfx.DrawLine(XPens.Black, new XPoint(scalLegendPosition.X, scalLegendPosition.Y - 3), new XPoint(scalLegendPosition.X, scalLegendPosition.Y + 3)); gfx.DrawLine(XPens.Black, new XPoint(scalLegendPosition.X + displayLength / 2, scalLegendPosition.Y - 2), new XPoint(scalLegendPosition.X + displayLength / 2, scalLegendPosition.Y + 2)); gfx.DrawLine(XPens.Black, new XPoint(scalLegendPosition.X + displayLength, scalLegendPosition.Y - 3), new XPoint(scalLegendPosition.X + displayLength, scalLegendPosition.Y + 3)); XFont font = new XFont(fontName, 6); gfx.DrawString(String.Format("Skala 1:{0}", exportItem.scale), font, XBrushes.Black, new XRect(new XPoint(scalLegendPosition.X, scalLegendPosition.Y - 12), new XPoint(scalLegendPosition.X + displayLength, scalLegendPosition.Y - 2)), XStringFormats.TopLeft); gfx.DrawString(displayText, font, XBrushes.Black, (int)scalLegendPosition.X + displayLength + 10, (int)scalLegendPosition.Y + 2); // Draw infotext this.drawText(gfx, fontName, infoText, (int)stampArea.Left + 10, (int)stampArea.Bottom - 5, 5); // Draw copyright notes int i = 0; copyrights.ForEach(copyright => { if (i > 0) { copyrightPosition.Offset(0, -10); } gfx.DrawString(String.Format("© {0}", copyright), font, XBrushes.Black, copyrightPosition, XStringFormats.BottomRight); i++; }); byte[] bytes; using (MemoryStream ms = new MemoryStream()) { document.Save(ms); bytes = ReadFully(ms); } return(bytes); } return(null); }
public string ExportPDF(MapExportItem exportItem) { string tempPath = "/Temp"; string path = HttpContext.Current.Server.MapPath(tempPath); string filePath = ""; if (exportItem.wmtsLayers == null || exportItem.wmtsLayers.Count == 0) { Image img = MapImageCreator.GetImage(exportItem); filePath = tempPath + "/" + this.createPdf(img, path, exportItem); } else { MapImageCreator.GetImageAsync(exportItem, (data) => { Image img = (Image)data.image.Clone(); try { filePath = tempPath + "/" + this.createPdf(img, path, exportItem); } catch (Exception ex) { filePath = ex.Message; } finally { stopWaitHandle.Set(); } }); stopWaitHandle.WaitOne(); } return filePath; }
public string ExportPDF(MapExportItem exportItem) { string tempPath = "/Temp"; string path = HttpContext.Current.Server.MapPath(tempPath); string filename = Guid.NewGuid() + ".pdf"; string localPdf = path + "\\" + filename; Image img = MapImageCreator.GetImage(exportItem); PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); page.Size = exportItem.format == "A4" ? PdfSharp.PageSize.A4 : PdfSharp.PageSize.A3; page.Orientation = exportItem.orientation == "L" ? PdfSharp.PageOrientation.Landscape : PdfSharp.PageOrientation.Portrait; XGraphics gfx = XGraphics.FromPdfPage(page); int scale = int.Parse(exportItem.scale); double length = (1.0 / scale); double unitLength = (length * 2.82e3); Dictionary <int, string> scaleBarTexts = new Dictionary <int, string>() { { 1000, "50 m" }, { 2000, "100 m" }, { 5000, "200 m" }, { 10000, "500 m" }, { 20000, "1 km" }, { 50000, "2 km" }, { 100000, "5 km" }, { 250000, "10 km" } }; Dictionary <int, int> scaleBarLengths = new Dictionary <int, int>() { { 1000, 50 }, { 2000, 100 }, { 5000, 200 }, { 10000, 500 }, { 20000, 1000 }, { 50000, 2000 }, { 100000, 5000 }, { 250000, 10000 } }; int displayLength = (int)(unitLength * scaleBarLengths.FirstOrDefault(a => a.Key == scale).Value); string displayText = scaleBarTexts.FirstOrDefault(a => a.Key == scale).Value; this.drawImage(gfx, img, 10, 10, page); Point[] points = new Point[] { new Point(12, 12), new Point(12, 55), new Point(55 + displayLength, 55), new Point(55 + displayLength, 12), new Point(12, 12) }; gfx.DrawPolygon(XBrushes.White, points, XFillMode.Winding); gfx.DrawLine(XPens.Black, new Point(15, 47), new Point(15 + displayLength, 47)); gfx.DrawLine(XPens.Black, new Point(15, 44), new Point(15, 50)); gfx.DrawLine(XPens.Black, new Point(15 + displayLength, 44), new Point(15 + displayLength, 50)); string copyright = ConfigurationManager.AppSettings["exportCopyrightText"]; this.drawText(gfx, String.Format("© {0}", copyright), 15, 25); this.drawText(gfx, String.Format("Skala 1:{0}", exportItem.scale), 15, 40); this.drawText(gfx, displayText, 20 + displayLength, 50); XImage logo = XImage.FromFile(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "assets", "logo.png")); gfx.DrawImage(logo, gfx.PageSize.Width - 212, 12, 200, 67); document.Save(localPdf); return(tempPath + "/" + filename); }
public string TIFF(string json) { _log.DebugFormat("Received json: {0}", json); // try to decode input string to see if it is base64 encoded try { byte[] decoded = Convert.FromBase64String(json); json = System.Text.Encoding.UTF8.GetString(decoded); _log.DebugFormat("json after decode: {0}", json); } catch (Exception e) { _log.DebugFormat("Could not decode base64. Will treat as non-base64 encoded: {0}", e.Message); } MapExportItem exportItem = JsonConvert.DeserializeObject <MapExportItem>(json); TIFFCreator tiffCreator = new TIFFCreator(); Image img = tiffCreator.Create(exportItem); MemoryStream outStream = new MemoryStream(); ZipOutputStream zipStream = new ZipOutputStream(outStream); zipStream.SetLevel(3); ZipEntry imageEntry = new ZipEntry(ZipEntry.CleanName("kartexport.tiff")); imageEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(imageEntry); MemoryStream imageStream = new MemoryStream(imgToByteArray(img)); byte[] buffer = new byte[4096]; StreamUtils.Copy(imageStream, zipStream, buffer); imageStream.Close(); zipStream.CloseEntry(); ZipEntry worldFileEntry = new ZipEntry(ZipEntry.CleanName("kartexport.tfw")); worldFileEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(worldFileEntry); MemoryStream worldFileStream = new MemoryStream(MapImageCreator.CreateWorldFile(exportItem)); byte[] buffer2 = new byte[4096]; StreamUtils.Copy(worldFileStream, zipStream, buffer2); worldFileStream.Close(); zipStream.CloseEntry(); zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream. zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream. outStream.Position = 0; outStream.ToArray(); string[] fileInfo = byteArrayToFileInfo(outStream.ToArray(), "zip"); if (exportItem.proxyUrl != "") { return(exportItem.proxyUrl + "/Temp/" + fileInfo[1]); } else { return(Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/" + fileInfo[1]); } }