Select to view content in your preferred language

How to print a widget window on the map?

2030
3
06-14-2011 02:18 PM
LeiZhou
Deactivated User
I need to print the legend widget window when I use Print Widget.  Although the legend widget is open when I send the print job, it does not show up on the final printed map.  Should I use something like printBox.addElement (LegendWidget)? Thanks!
Tags (2)
0 Kudos
3 Replies
LeeAllen
Frequent Contributor
Here is the .mxml for print widget that captures widgets in output.  Added portions are commented.  These are changes found on other forums.
<?xml version="1.0" encoding="utf-8"?>
<!--
     ////////////////////////////////////////////////////////////////////////////////
     //
     // Copyright (c) 2010 ESRI
     //
     // All rights reserved under the copyright laws of the United States.
     // You may freely redistribute and use this software, with or
     // without modification, provided you include the original copyright
     // and use restrictions.  See use restrictions in the file:
     // <install location>/License.txt
     //
     ////////////////////////////////////////////////////////////////////////////////
-->
<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:viewer="com.esri.viewer.*"
                   x="600" y="300"
                   widgetConfigLoaded="init()">

    <fx:Style>
        .PrintBox
        {
            color: #000000; /* for the printed page */
        }
    </fx:Style>

    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.graphics.ImageSnapshot;
            import mx.printing.FlexPrintJob;
            import mx.printing.FlexPrintJobScaleType;

        //added to get widgets to print on map LJA
        import com.esri.viewer.ViewerContainer;
        //end add

            import spark.components.Label;
            import spark.components.VGroup;
            import spark.primitives.BitmapImage;

            [Bindable]
            private var title:String;
            [Bindable]
            private var subtitle:String;
            private var copyright:String = "";

            //labels
            [Bindable]
            private var titleLabel:String;
            [Bindable]
            private var subtitleLabel:String;
            [Bindable]
            private var submitLabel:String;

            private function init():void
            {
                if (configXML)
                {
                    title = configXML.title[0] || getDefaultString("printTitle");
                    subtitle = configXML.subtitle[0] || getDefaultString("printSubtitle");
                    copyright = configXML.copyright[0] || getDefaultString("printCopyright");

                    //labels
                    titleLabel = configXML.labels.titlelabel[0] || getDefaultString("printTitleLabel");
                    subtitleLabel = configXML.labels.subtitlelabel[0] || getDefaultString("printSubtitleLabel");
                    submitLabel = configXML.labels.submitlabel[0] || getDefaultString("printSubmitLabel");
                }
            }

            private function printMap():void
            {
                var zoomSliderVisibleBeforePrint:Boolean;
                if (map.zoomSliderVisible)
                {
                    map.zoomSliderVisible = false;
                    zoomSliderVisibleBeforePrint = true;
                }
                var printJob:FlexPrintJob = new FlexPrintJob();
                printJob.printAsBitmap = false;
                if (printJob.start())
                {
                    var h:Number = printJob.pageHeight;
                    var w:Number = printJob.pageWidth;
                    //VGROUP
                    var printBox:VGroup = new VGroup();
                    printBox.styleName = "PrintBox";
                    printBox.width = map.width;
                    this.addChild(printBox); // add now to workaround https://bugs.adobe.com/jira/browse/SDK-26906
                    try
                    {
                        //TITLE
                        var printTitle:Label = new Label();
                        printTitle.text = txtTitle.text;
                        // TODO: make fontsize specified in stylename overwrite this fontSize
                        printTitle.setStyle("fontSize", h / 12);
                        printTitle.percentWidth = 100;
                        printBox.addElement(printTitle);
                        //SUBTITLE
                        var printSubtitle:Label = new Label();
                        printSubtitle.text = txtSubtitle.text;
                        // TODO: make fontsize specified in stylename overwrite this fontSize
                        printSubtitle.setStyle("fontSize", h / 24);
                        printSubtitle.percentWidth = 100;
                        printBox.addElement(printSubtitle);
                        //MAP
                        //add to capture widgets LJA
                        var bmpMap:BitmapData = ImageSnapshot.captureBitmapData(ViewerContainer.getInstance());
                        //var bmpMap:BitmapData = ImageSnapshot.captureBitmapData(map);
                        var printImg:BitmapImage = new BitmapImage();
                        printImg.smooth = true;
                        printImg.source = bmpMap;
                        printBox.addElement(printImg);
                        //COPYRIGHT
                        var now:Date = new Date();
                        var printCopy:Label = new Label();
                        printCopy.text = copyright + " " + now.toLocaleString() + ".";
                        // TODO: make fontsize specified in stylename overwrite this fontSize
                        printCopy.setStyle("fontSize", h / 48);
                        printCopy.percentWidth = 100;
                        printBox.addElement(printCopy);
                        //PRINT
                        printJob.addObject(printBox, FlexPrintJobScaleType.SHOW_ALL);
                        printJob.send();
                        bmpMap.dispose();
                    }
                    catch (error:Error)
                    {
                        Alert.show(error.toString(), wTemplate.widgetTitle);
                    }
                    finally
                    {
                        this.removeChild(printBox);
                    }
                }

                if (zoomSliderVisibleBeforePrint)
                {
                    map.zoomSliderVisible = true;
                }
            }
        ]]>
    </fx:Script>

    <viewer:WidgetTemplate id="wTemplate"
                           width="300" height="180"
                           minHeight="168"
                           minWidth="280">
        <mx:Form id="frmPrint"
                 width="100%" height="100%"
                 paddingBottom="0"
                 verticalScrollPolicy="off">
            <mx:FormItem width="100%" label="{titleLabel}">
                <s:TextInput id="txtTitle"
                             width="100%"
                             text="{title}"/>
            </mx:FormItem>
            <mx:FormItem width="100%" label="{subtitleLabel}">
                <s:TextInput id="txtSubtitle"
                             width="100%"
                             text="{subtitle}"/>
            </mx:FormItem>
            <s:HGroup width="100%" horizontalAlign="center">
                <s:Button click="printMap()" label="{submitLabel}"/>
            </s:HGroup>
        </mx:Form>
    </viewer:WidgetTemplate>

</viewer:BaseWidget>
0 Kudos
LeiZhou
Deactivated User
Thank you very much! My first try does not work, but I will spend more time on it and let others know the results.

I need to print the legend widget window when I use Print Widget.  Although the legend widget is open when I send the print job, it does not show up on the final printed map.  Should I use something like printBox.addElement (LegendWidget)? Thanks!
0 Kudos
LeiZhou
Deactivated User
The code works now. Just one thing: the print widget will also show up on the printed map unless I did not use print widget and use the "print" function from browser.

Also, I have to open my legend widget first,otherwise it can be printed out. Is there anyway that I can print without open the legend widget in viewer? Thanks a lot!

I need to print the legend widget window when I use Print Widget.  Although the legend widget is open when I send the print job, it does not show up on the final printed map.  Should I use something like printBox.addElement (LegendWidget)? Thanks!
0 Kudos