Created
May 12, 2011 20:18
-
-
Save odoe/969352 to your computer and use it in GitHub Desktop.
ESRI Flexviewer Print Widget modified to save PNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.events.FlexEvent; | |
import mx.graphics.ImageSnapshot; | |
import mx.graphics.codec.PNGEncoder; | |
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 var printData:ByteArray; | |
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 | |
{ | |
trace("i clicked on the print button"); | |
var zoomSliderVisibleBeforePrint:Boolean; | |
if (map.zoomSliderVisible) | |
{ | |
map.zoomSliderVisible = false; | |
zoomSliderVisibleBeforePrint = true; | |
} | |
var h:Number = map.height; | |
var w:Number = map.width; | |
//VGROUP | |
var printBox:VGroup = new VGroup(); | |
printBox.styleName = "PrintBox"; | |
printBox.width = map.width; | |
printBox.height = map.height; | |
printBox.addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void { | |
trace("added to stage"); | |
printBox.addElement(printImg); | |
}); | |
printBox.addEventListener(FlexEvent.CREATION_COMPLETE, function(e:FlexEvent):void { | |
trace("creation complete"); | |
var bd:BitmapData = new BitmapData(printBox.width, printBox.height); | |
var m:Matrix = new Matrix(); | |
bd.draw(printBox, m); | |
printBox.parent.removeChild(printBox); | |
var pgEnc:PNGEncoder = new PNGEncoder(); | |
var fr:FileReference = new FileReference(); | |
printData = pgEnc.encode(bd); | |
var loader:Loader = new Loader(); | |
var lc:LoaderContext = new LoaderContext(); | |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void { | |
prvImg.source = loader.content; | |
}, false, 0, true); | |
loader.loadBytes(printData, lc); | |
}); | |
try | |
{ | |
trace("print try block"); | |
//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 | |
var bmpMap:BitmapData = ImageSnapshot.captureBitmapData(map, new Matrix()); | |
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); | |
} | |
catch (error:Error) | |
{ | |
Alert.show(error.toString(), wTemplate.widgetTitle); | |
} | |
this.addChild(printBox); | |
if (zoomSliderVisibleBeforePrint) | |
{ | |
map.zoomSliderVisible = true; | |
} | |
} | |
protected function button1_clickHandler(event:MouseEvent):void | |
{ | |
var fr:FileReference = new FileReference(); | |
fr.save(printData, "TIGER.png"); | |
} | |
]]> | |
</fx:Script> | |
<viewer:WidgetTemplate id="wTemplate" | |
width="300" | |
height="300" | |
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="Preview" /> | |
</s:HGroup> | |
<mx:FormItem width="100%" | |
label="Preview"> | |
<s:VGroup gap="2"> | |
<s:BitmapImage id="prvImg" | |
width="150" | |
height="150" | |
smooth="true" /> | |
<s:Button click="button1_clickHandler(event)" | |
label="Save" /> | |
</s:VGroup> | |
</mx:FormItem> | |
</mx:Form> | |
</viewer:WidgetTemplate> | |
</viewer:BaseWidget> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment