using ESRI.ArcGIS.Client;
using CMC_Viewer.Utils;
public interface IParentDialog
{
void SetStatus(string sStatus);
void TaskCompleted(bool bSuccessful);
}
namespace CMC_Viewer.Views
{
public partial class ExportPDF : ChildWindow, IParentDialog
{
private PDFExporter _Exporter;
private bool _bTwoStage = false;
private bool _bFirst = false;
private const string sOKMsg = "OK";
private const string sFailedMsg = "Failed";
private const string sStartMsg = "Click 'Start' to generate image.";
private const string sStartLabel = "Start";
private const string sFinishMsg = "Click 'Finish' to create PDF.";
private const string sFinishLabel = "Finish";
public ExportPDF()
{
InitializeComponent();
_Exporter = new PDFExporter();
_Exporter.SetParent(this);
lblStatus.Content = sOKMsg;
btnOK.Content = sStartLabel;
btnOK.Click += btnOK_Click;
chkExportOption.Checked += chkExportOption_Checked;
chkExportOption.Unchecked += chkExportOption_Unchecked;
}
// Methods called by the MainPage
public void SetMap(Map TheMap)
{
_Exporter.SetMap(TheMap);
}
public void AddElement(UIElement TheElement)
{
_Exporter.AddElement(TheElement);
}<controls:ChildWindow x:Class="CMC_Viewer.Views.ExportPDF" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" mc:Ignorable="d" d:DesignHeight="60" d:DesignWidth="280" >
private void Map2PDF_Click(object sender, RoutedEventArgs e)
{
/* // Don't need the ExportPDF dialog anymore
CMC_Viewer.Views.ExportPDF map2pdf = new CMC_Viewer.Views.ExportPDF();
map2pdf.SetMap(this.Map);
map2pdf.Show();
* */
// Display save-as dialog
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "PDF|*.pdf";
if (!dialog.ShowDialog().Value)
{
return;
}
// Create a new dynamic layer from the same map service as current tiled layer
ArcGISTiledMapServiceLayer tiled = this.Map.Layers[0] 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.Map.Extent,
(int)this.Map.ActualWidth,
(int)this.Map.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.Map.ActualHeight, XGraphicsUnit.Presentation),
Width = new XUnit(this.Map.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();
}