Hi guys,
I'm trying to create a function for my customers so they can export their maps to a pdf file. Basically, the user would click a button to export a pdf file containing the map with the current extent, a title at the top of the page and a scalebar somewhere at the bottom. Here's my code: (questions follow)
PdfDocument pdfDocument = new PdfDocument();
PdfPage pdfPage = pdfDocument.AddPage();
pdfPage.Size = PageSize.Letter;
pdfPage.Orientation = PageOrientation.Landscape;
float marginTop = 20.0f;
float marginLeft = 20.0f;
float drawingWidth = (float)pdfPage.Width.Point - marginLeft * 2;
float drawingHeight = (float)pdfPage.Height.Point - marginTop * 2;
RectangleShape printExtent = ExtentHelper.GetDrawingExtent(Map1.CurrentExtent, drawingWidth, drawingHeight);
RectangleShape backupExtent = Map1.CurrentExtent;
Map1.CurrentExtent = printExtent;
Bitmap bitmap = Map1.GetBitmap((int)drawingWidth, (int)drawingHeight);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Png);
// Draw the map image
PdfGeoCanvas pdfGeoCanvas = new PdfGeoCanvas();
pdfGeoCanvas.BeginDrawing(pdfPage, printExtent, GeographyUnit.Meter);
pdfGeoCanvas.DrawScreenImage(new GeoImage(stream), drawingWidth * .5f, drawingHeight * .5f, drawingWidth, drawingHeight, DrawingLevel.LabelLevel, marginLeft, marginTop, 0f);
pdfGeoCanvas.EndDrawing();
// Draw title
pdfGeoCanvas.BeginDrawing(pdfPage, printExtent, GeographyUnit.Meter);
string titleText = String.Format("This is my map - {0}", DateTime.Now.ToString("D", CultureInfo.InvariantCulture));
DrawingRectangleF titleExtent = pdfGeoCanvas.MeasureText(titleText, new GeoFont("Arial", 16));
float titleLeft = (pdfGeoCanvas.Width - titleExtent.Width) / 2.0f + marginLeft;
float titleTop = 10;
pdfGeoCanvas.DrawTextWithScreenCoordinate(titleText, new GeoFont("Arial", 16), new GeoSolidBrush(GeoColor.StandardColors.Black), titleLeft, titleTop, DrawingLevel.LabelLevel);
pdfGeoCanvas.EndDrawing();
string targetFilePath = String.Format("{0}\\test.pdf", Server.MapPath("~/SHPDepot"));
pdfDocument.Save(targetFilePath);
Map1.CurrentExtent = backupExtent;
Questions:
1- When I set Map1.CurrentExtent = printExtent to get a good image scale for the pdf page format, all the markers disappear (They're not visible in the resulting image/pdf). If I don't set Map1.CurrentExtent I can see the markers...but lose the scaling!
2- When I try to center the title horizontally the pdfGeoCanvas seems to shift the text depending on it's length (randomly?). I've also tried to align it to the left with fixed x and y coordinated but it still shifts the text!
3- What about adding a scale bar over the map? The GetBitmap function of the map control does not include the scalebar control in its snapshot.
I use MapSuite WebEdition 5.5.0.32. The map includes an OpenStreetMapLayer, ShapefileFeatureLayers and InMemoryMarkerOverlays.
Any help would be appreciated,
Thanks
Marc