Select to view content in your preferred language

Export Map Graphics with Legend Toolbar to PDF Document

4049
7
07-12-2012 08:14 AM
SaurabhDasgupta
Deactivated User
Hi,
    I am developed a Silverlight application where I am using a BaseMap service and on the top it I am adding some graphics on it. I want to export this Map image along with the Graphics and Legend Toolbar to a PDF document. I have successfully managed to export the map image to the PDF document where only the BaseMap is exported to the PDF docuement(As the code written to export only PDF).Now I want to export the graphics and Legend toolbar as well as earlier mentioned but not able to get any idea as to how to do it. Attached screen shot provides the map view with the graphics and legend toolbar.

Using ArcGIS Server10, Silverlight 4 with VS2010 SP1.

Any code help to start with will greatly benefit me.

~Saurabh.
0 Kudos
7 Replies
SaurabhDasgupta
Deactivated User
Can antbody provide me some quick idea on this. I am using the following lines of code to export only the BaseMap to PDF Document. Need to export with Map graphics and the legend toolbar.

private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Display save-as dialog
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "PDF|*.pdf |JPEG Files (*.jpeg)|*.jpeg";

            if (dialog.ShowDialog() == true)
            {
                //    return;
                //}

                // Create a new dynamic layer from the same map service as current tiled layer

                ArcGISTiledMapServiceLayer tiled = new ArcGISTiledMapServiceLayer();
                tiled = this.TheMap.Layers["MyLayer"] as ArcGISTiledMapServiceLayer;
                ArcGISDynamicMapServiceLayer dynamic = new ArcGISDynamicMapServiceLayer()
                {
                    Url = tiled.Url,
                    ImageFormat = ArcGISDynamicMapServiceLayer.RestImageFormat.JPG
                };
               

                // When the dynamic layer has initialized create the in-memory PDF document
                dynamic.Initialized += (a, b) =>
                {
                    dynamic.GetUrl(this.TheMap.Extent,
                                   (int)this.TheMap.ActualWidth,
                                   (int)this.TheMap.ActualHeight,
                                   delegate(string url, int width, int height, Envelope extent)
                                   {
                                       // Download a new image of identical to what is currently displayed in the map
                                       WebClient webClient = new WebClient();
                                       webClient.OpenReadCompleted += (c, f) =>
                                       {
                                           // Use the dispatcher to force execution in the UI thread
                                           Dispatcher.BeginInvoke(delegate()
                                           {
                                               // Create the PDF document, set document information properties
                                               PdfDocument document = new PdfDocument();
                                               document.Info.Title = "Map";
                                               // Create a new page with the same dimensions as the browser map
                                               PdfPage page = new PdfPage(document)
                                               {
                                                   Height = new XUnit(this.TheMap.ActualHeight, XGraphicsUnit.Presentation),
                                                   Width = new XUnit(this.TheMap.ActualWidth, XGraphicsUnit.Presentation)
                                               };
                                               document.Pages.Add(page);

                                               // Create a graphics object for writing to the page
                                               XGraphics graphics = XGraphics.FromPdfPage(page);
                                               // Add the map image to the page
                                               XImage image = XImage.FromStream(f.Result);
                                               graphics.DrawImage(image, 0d, 0d);
                                               // Save the PDF document to the user specified filename
                                               document.Save(dialog.OpenFile());
                                               // Notify the user that we're done
                                               MessageBox.Show("Map saved to '" + dialog.SafeFileName + "'");
                                           });
                                       };
                                       webClient.OpenReadAsync(new Uri(url));
                                   });
                };
                dynamic.Initialize();
}
}

Thanks in advance.
~Saurabh.
0 Kudos
SaurabhDasgupta
Deactivated User
Let me know if anyone having any idea of this implementation on this. I am still struggling to find a solution for this one.

~Saurabh.
0 Kudos
JoeHershman
MVP Alum
Take a look at this sample in the Map Gallery.  I believe it does exactly what you need

http://www.arcgis.com/home/item.html?id=efcc15ef279f4f73a39bf06fb117ec68
Thanks,
-Joe
0 Kudos
SaurabhDasgupta
Deactivated User
Hi minerjoe,
               Thanks for your reply and sorry for being late in replying. I tried the code provided by you but this throws me a exception saying that "UnathorizedAcessException was unhadled by user code". Not sure why this says so. If you can throw some light on it this will be great. The exception throws after the following chunk of code. Attached is the screen shot of the error as well.

BitmapImage bmi = new BitmapImage();
bmi.SetSource(s);
WriteableBitmap wbm = new WriteableBitmap(bmi);
AddBitmap(wbm);


~Saurabh.
0 Kudos
BrentStevener
Deactivated User
The short answer is you can't print to PDF the way you are doing. You can send an image to the printer using a PrintDoc (which the user can save as PDF if they have a PDF printer) but you can't save as a PDF because of Silverlight's built-in security, which are the errors you are getting.

Past thread of a similar issue I had:
http://forums.arcgis.com/threads/41663-Exporting-to-Image-PDF-%28running-in-circles%29

The printing task in 10.1 allows you to get a PDF from the server...
0 Kudos
SaurabhDasgupta
Deactivated User
Hi,
    Thanks for the reply. I tried to do a print task from a sample that I find in the link
http://www.arcgis.com/home/item.html?id=e361c2cc69784fcba34358d0458f66e3
On further study I have found that this application uses Expression Blend, from where 2 dlls are used as namely
Systems.Windows.Interactivity & Microsoft.Expression.Interactions. I am currently using ArcGIS Sever 10.0, Silverlight 2.2 api with VS2010. I dont want go for ArcGIS Server 10.1 with Expression Blend.

Is it possible to go for a print task where I can print using PDF printer to print the map layer with legend and graphics on map following the above scenario. Stuck here for quite sometime, any help will be appreciated.

~Saurabh.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Systems.Windows.Interactivity & Microsoft.Expression.Interactions are blend SDK dll's that are needed for ArcGIS API for Silverlight.  They can be downloded here.

I guess there is a way to tweak the sample for not using these dlls but I never tried that.
0 Kudos