Select to view content in your preferred language

How do I include the Scalebar in a PDF export

563
3
09-15-2010 02:54 PM
GregoryDillon
Occasional Contributor
I've been trying to enhance the PDFExporter.cs sample.   How can I export the a scalebar?

Greg
0 Kudos
3 Replies
DeminHu
Deactivated User
Hi,

Where you get PDFExporter.cs file? can yout let me know, I forget where it is.

Thanks
0 Kudos
GregoryDillon
Occasional Contributor
This is the link I found.  http://www.pierssen.com/arcgis10/silverlight.htm

BTW: The Unblended sample does not use pdfexporter.cs, but there is a link to it on this same page.   This is the only example of a PDF export I could find.

If somebody knows of another one that implements a scalebar, please let me know

Does anybody have some code how to get the image from the scalebar and stream it to the pdfexporter.cs?
0 Kudos
GregoryDillon
Occasional Contributor
Okay I figured this out - Basically I added a new method called "setScaleBar" modeled after the "setMap" method.


public class PDFExporter
{

    private IParentDialog _Parent = null;
    private Map _Map = null;
    private ScaleBar _ScaleBar = null;
    private Stream _streamOutPDF = null;
    private WriteableBitmap _wbm;
    private bool _bAsync = false;
    private int _iLayer;
    private double _dOpacity;
    private List<UIElement> _ElementList = new List<UIElement>();

    public void SetParent(IParentDialog TheParent)
    {
        _Parent = TheParent;
    }

    public void SetMap(Map TheMap)
    {
        _Map = TheMap;
    }

   public void SetScaleBar(ScaleBar TheScaleBar)
    {
        _ScaleBar = TheScaleBar;
    }

...

Next  I modified the "Bitmap2Stream" method so that it passed in the WriteableBitMap and returned the stream  (this made this routine more reusable):

private Stream Bitmap2Stream(ref WriteableBitmap wbmp)
    {

        // Convert bitmap to byte array used by encoder
        Stream bmpstream;

        int iWidth = wbmp.PixelWidth;
        int iRowLen = (iWidth * 4) + 1;
        int iHeight = wbmp.PixelHeight;
        int[] pixels = wbmp.Pixels;
        byte[] bytes = new byte[iRowLen * iHeight];
        int iRow, iCol, i, j, pixel;
        byte R, G, B, A;
        for (iRow = 0; iRow < iHeight; iRow++)
        {
            bytes[iRow * iRowLen] = 0; // Filter bit
            for (iCol = 0; iCol < iWidth; iCol++)
            {
                i = (iRow * iWidth) + iCol;
                pixel = pixels;
                R = (byte)((pixel >> 16) & 0xFF);
                G = (byte)((pixel >> 😎 & 0xFF);
                B = (byte)(pixel & 0xFF);
                A = (byte)((pixel >> 24) & 0xFF);
                j = (iRow * iRowLen) + (iCol * 4) + 1;
                bytes = R;
                bytes[j + 1] = G;
                bytes[j + 2] = B;
                bytes[j + 3] = A;
            }
        }

        // Encode to stream

        bmpstream = Silverlight.Samples.PngEncoder.Encode(bytes, iWidth, iHeight);
        wbmp = null;
        return bmpstream;

    }


In the "FinishExport" routine, I modified the call to Bitmap2Stream for the map as so:

_stream = Bitmap2Stream(ref _wbm)

and then added the following logic (bolded):

if (_ScaleBar != null)
            {
                ScaleTransform transform = new ScaleTransform();
                transform.ScaleX = transform.ScaleY = 1.0;

                WriteableBitmap wbmpScaleBar = new WriteableBitmap(_ScaleBar, transform);
                Stream sbstream = Bitmap2Stream(ref wbmpScaleBar);
                XImage sbimg = XImage.FromStream(sbstream);
                double sbY = img.PointHeight - sbimg.PointHeight - 5;
                gfx.DrawImage(sbimg, 5, sbY, sbimg.PointWidth, sbimg.PointHeight);
            }


            document.Save(_streamOutPDF);
            document.Close();
            document = null;
            mapstream.Close();
            mapstream = null;
            _streamOutPDF = null;
            MessageBox.Show("PDF created.");
0 Kudos