Now for the other PrintTask for the Web ADF. I have referred to this as the main course; it is our turkey. But, I will do the best I can to make it tasty.
I read this post http://www.arcgisserverblog.com/2008/12/print-map-with-arcgis-server.html and decided to do the same thing that I do when I buy a new pc; take it apart and change it. In this example, we are going to export the image of the map, obtain the name of the new image file and then perform a page redirect from there just like in the ESRI JSAPI 2.1 Print to PDF example.
The screen print I planned to share with you is not available right now, but I do have a screen print of the MapResourceManager. With the main course like this, what does that say about my side dishes?
To define your map, we don't use the REST API in this example, so set you MapResource Manager as displayed in the image below.
It is the same resource. We are just not using the REST API in this one. Now for the one thing that separates these two examples; the code.
The following is plain text, but this way you can copy it for your own application. You do need a license for ESRI ArcGIS Server to use this code. Nothing to worry about; there will be plenty of other map examples that will cost you nothing to use. But if you are unable to move away from the Web ADF right now, then this you may find this code helpful for two reasons. The first is that you will be able print to pdf and the second is that the page that you redirect to, that creates the pdf is the exact same page. So, you can migrate this PrintTask to the JSAPI 2.1 if that is your API of choice. If not, you may be interested in another such as Silverlight or Flex. We aren't serving those this weekend. But if you want to bring a code plate and serve one of those up, let me know. I will link to it as this will free up time for me to share additional code sooner.
Here is your export function for exporting your image:
Public Shared Function ExportMapImage(ByVal ADFMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map, ByVal OutputPath As String) As String
Dim bitmaps As New System.Collections.Generic.List(Of System.Drawing.Image)()
For Each mf As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality In ADFMap.GetFunctionalities()
If mf IsNot Nothing Then
Dim mapImage As ESRI.ArcGIS.ADF.Web.MapImage = mf.DrawExtent(ADFMap.Extent)
mapImage = mf.DrawExtent(ADFMap.Extent)
If mapImage Is Nothing Then
Continue For
End If
If mapImage.GetImage() IsNot Nothing Then
bitmaps.Add(mapImage.GetImage())
End If
End If
Next
Dim newImg As New System.Drawing.Bitmap(Convert.ToInt16(ADFMap.Width.Value), Convert.ToInt16(ADFMap.Height.Value))
Dim imgGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImg)
Dim i As Integer = bitmaps.Count - 1
While i >= 0
imgGraphics.DrawImage(bitmaps(i), 0, 0, Convert.ToInt16(ADFMap.Width.Value), Convert.ToInt16(ADFMap.Height.Value))
i += -1
End While
Dim Ticks As Long = DateTime.Now.Ticks
Dim ImagePath As String = OutputPath & "mapimage" & Ticks & ".png"
newImg.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Png)
Return "mapimage" & Ticks & ".png"
End Function
Once you export your image, you need call the above function. We are going to use a button click event.
Protected Sub btnPrintMap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintMap.Click
'The following is how you should write out the path variables
'TextBox1.Text = "http://YourWebSite/Images/" & ExportMapImage(Map1, "http:///YourWebSite/Images/").ToString
Response.Redirect("PrintTaskResults.aspx?sender=" + TextBox1.Text.ToString)
End Sub
Although we are redirecting to another page, the embedded pdf page has something that you did not see in the last in the JSAPI 2.1. An image is created, but once the pdf is created, we have the code delete the image. So, here is the Page_Load event.
Since the image has been used for the pdf and it does not contain a unique name, we don't want our Images folder to constantly grow, so we check to see the image file used in the pdf still exists. If it does, it is deleted. In this way we have automated the clean up of our Images folder.
That concludes this sample, so now feel free to download the Print to PDF in ESRI ArcGIS Server 9.3.1 using the Web ADF in .NET now.



No comments:
Post a Comment
I have had the opportunity to share this with you and I encourage you to share your comments with me.