I have some code that generates PDF reports for users.
The PDF files are used to send field soil sample data to customers. Each field will be shown on the PDF 5 times (1 full-page map and 4 quarter-page maps).
I use a PdfGeoCanvas and draw the my feature layer along with a BingMapsLayer.
The problem I have is that the Bing layers results in significantly higher file sizes that those without any Bing backgrounds.
Is there any way to reduce the size of the files or the quality of the Bing background?
Thanks
Simon
Bing map layer in PDF file
Hi Simon,
Would you please give us a screenshot or sample to show what you are using currently in your side? You mentioned you are using PdfGeoCanvas, if you are following one of our samples in our application? I want to have a full understanding how you are using.
Thanks,
Troy
Hi Troy
I’ve extracted some of the code and added it below. There is more to it in terms of creating a feature layer, setting projection etc. but I don’t think that you need all that.
private void drawMap()
{
Map tempMap = new Map();
addBingOverlayToMap(ref tempMap);
PdfGeoCanvas pdfGeoCanvas = new PdfGeoCanvas();
//Create the rectangle that the map can be drawn within
System.Drawing.Rectangle mapRectangle = new System.Drawing.Rectangle(25, 25, (int)page.Width - 50, (int)page.Height - 220);
pdfGeoCanvas.DrawingArea = mapRectangle;
pdfGeoCanvas.BeginDrawing(page, tempMap.CurrentExtent, GeographyUnit.Meter);
LayerOverlay bingOverlay = (LayerOverlay)tempMap.CustomOverlays[“Bing Map”];
BingMapsLayer bingLayer = (BingMapsLayer)bingOverlay.Layers[0];
bingLayer.Open();
bingLayer.Draw(pdfGeoCanvas, labelsInLayers);
bingLayer.Close();
pdfGeoCanvas.EndDrawing();
}
public void addBingOverlayToMap(ref Map Map1)
{
BingMapsLayer bingLayer = new BingMapsLayer(“MYBINGKEY”, BingMapsMapType.Aerial);
LayerOverlay bingOverlay = new LayerOverlay(“Bing Map”);
bingOverlay.Layers.Add(bingLayer);
Map1.CustomOverlays.Add(bingOverlay);
}
Hi Simon,
Thanks for the codes.
Would you please try to set DrawingQuality as HighSpeed on Canvas to see if helps? Also, how about reduce the DPI on PdfGeoCanvas? Anyway, I will try to find more options for you.
Thanks,
Troy
Hi Troy
Neither of these appear to make any difference.
Simon
Simon,
Would you please try the below custom Canvas? I just think if we can reduce the image size before drawing on the canvas.
public class ExtensionPdfGeoCanvas : PdfGeoCanvas
{
protected override void DrawScreenImageWithoutScalingCore(GeoImage image, float centerXInScreen, float centerYInScreen, DrawingLevel drawingLevel, float xOffset, float yOffset, float rotateAngle)
{
double sizeScale= 0.2;
Bitmap bitmap = new Bitmap(image.GetImageStream(this));
Bitmap tempBitmap = new Bitmap((int)Math.Ceiling(bitmap.Width * sizeScale), (int)Math.Ceiling(bitmap.Height * sizeScale));
Graphics g = Graphics.FromImage(tempBitmap);
g.DrawImage(bitmap, 0, 0, tempBitmap.Width, tempBitmap.Height);
g.Dispose();
Bitmap actuallyDrawingBitmap = new Bitmap(bitmap.Width, bitmap.Height);
actuallyDrawingBitmap.SetResolution(Dpi, Dpi);
Graphics actuallyDrawingGraphics = Graphics.FromImage(actuallyDrawingBitmap);
actuallyDrawingGraphics.DrawImage(tempBitmap, 0, 0, bitmap.Width, bitmap.Height);
actuallyDrawingGraphics.Dispose();
tempBitmap.Dispose();
MemoryStream stream = new MemoryStream();
actuallyDrawingBitmap.Save(stream, ImageFormat.Png);
GeoImage geoImage = new GeoImage(stream);
base.DrawScreenImageWithoutScalingCore(geoImage, centerXInScreen, centerYInScreen, drawingLevel, xOffset, yOffset, rotateAngle);
bitmap.Dispose();
}
}
Please let us know if any questions.
Thanks,
Troy
Hi Troy
I tried this but when I call the Draw method on the binglayer I receive the error message "The remote server returned an error: (406) Not Acceptable."
Simon
Simon,
We tried this classes, but we didn’t have such exception, would you please try the sample?
Thanks,
Troy
post12845.zip (93.3 KB)
Hi Troy
I’m afraid this didn’t help much either.
I have instead managed to write some code that generates a JPEG image from a map and insert that into the PDF file. The processing time is unfortunately a little longer but the end result is a significantly smaller file size.
Simon
Hi Simon,
That’s really the implementation of MapPrinterLayer in MapSuiteCore, there we will print the map or something to a JPEG image with a configurable quality, then we will print it to Printer or PDF. Yes, in that case, it will be a bit slower, but I guess that’s the only that we can do now, because following the documentation from BingMaps REST Service msdn.microsoft.com/en-us/library/ff701724.aspx, which is used in Map Suite, it doesn’t support changing the quality of requested image.
Thanks,
Johnny