private void btnToPrintPreview_Click(object sender, RoutedEventArgs e) { PrinterInteractiveOverlay printerInteractiveOverlay = (PrinterInteractiveOverlay)Map1.InteractiveOverlays["PrintPreviewOverlay"]; PagePrinterLayer pagePrinterLayer = printerInteractiveOverlay.PrinterLayers["PageLayer"] as PagePrinterLayer; PrintDialog printDialog = new PrintDialog(); PrintDocument printDocument = new PrintDocument(); printDocument.DefaultPageSettings.Landscape = true; if (pagePrinterLayer.Orientation == PrinterOrientation.Portrait) { printDocument.DefaultPageSettings.Landscape = false; } printDocument.DefaultPageSettings.PaperSize = GetPrintPreviewPaperSize(pagePrinterLayer); PrinterGeoCanvas printerGeoCanvas = new PrinterGeoCanvas(); printerGeoCanvas.DrawingArea = printDocument.DefaultPageSettings.Bounds; printerGeoCanvas.BeginDrawing(printDocument, pagePrinterLayer.GetBoundingBox(), Map1.MapUnit); // Loop through all of the PrintingLayer in the PrinterInteractiveOverlay and print all of the // except for the PagePrinterLayer Collection <SimpleCandidate> labelsInAllLayers = new Collection <SimpleCandidate>(); foreach (PrinterLayer printerLayer in printerInteractiveOverlay.PrinterLayers) { printerLayer.IsDrawing = true; if (!(printerLayer is PagePrinterLayer)) { printerLayer.Draw(printerGeoCanvas, labelsInAllLayers); } printerLayer.IsDrawing = false; } printerGeoCanvas.Flush(); System.Windows.Forms.PrintPreviewDialog printPreviewDialog = new System.Windows.Forms.PrintPreviewDialog(); printPreviewDialog.Document = printDocument; System.Windows.Forms.DialogResult dialogResult = printPreviewDialog.ShowDialog(); }
public static PrintDocument GetPrintDocument(GisEditorWpfMap printMap, PrinterGeoCanvas printerGeoCanvas) { var printerOverlay = printMap.InteractiveOverlays.OfType <PrinterInteractiveOverlay>().FirstOrDefault(); var pagePrinterLayer = printerOverlay.PrinterLayers.OfType <PagePrinterLayer>().FirstOrDefault(); PrintDocument printDocument = new PrintDocument(); printDocument.DefaultPageSettings.PaperSize = GetPrintPreviewPaperSize(pagePrinterLayer); printDocument.DefaultPageSettings.Landscape = true; var printHeight = printDocument.DefaultPageSettings.PaperSize.Height; var printWidth = printDocument.DefaultPageSettings.PaperSize.Width; var drawingArea = new Rectangle(0, 0, printHeight, printWidth); if (pagePrinterLayer.Orientation == PrinterOrientation.Portrait) { printDocument.DefaultPageSettings.Landscape = false; drawingArea = new Rectangle(0, 0, printWidth, printHeight); } printerGeoCanvas.DrawingArea = drawingArea; printerGeoCanvas.BeginDrawing(printDocument, pagePrinterLayer.GetBoundingBox(), printMap.MapUnit); try { SetLegendIsPrinting(true, printerOverlay); SetDescriptionLabelVisible(false, printerOverlay); Collection <SimpleCandidate> labelsInAllLayers = new Collection <SimpleCandidate>(); foreach (PrinterLayer printerLayer in printerOverlay.PrinterLayers.Where(l => !(l is PagePrinterLayer))) { printerLayer.IsDrawing = true; if (printerLayer is DatePrinterLayer) { string currentDate = DateTime.Now.ToString(((DatePrinterLayer)printerLayer).DateFormat); ((DatePrinterLayer)printerLayer).DateString = currentDate; } if (printerLayer is ProjectPathPrinterLayer) { string projectPath = ((ProjectPathPrinterLayer)printerLayer).ProjectPath; Uri uri = GisEditor.ProjectManager.ProjectUri; if (!File.Exists(projectPath) && File.Exists(uri.LocalPath)) { ((ProjectPathPrinterLayer)printerLayer).ProjectPath = uri.LocalPath; } } SimplifyMapPrinterLayer mapPrinterLayer = null; if (printerLayer.GetType() == typeof(SimplifyMapPrinterLayer)) { printerLayer.IsDrawing = false; mapPrinterLayer = printerLayer as SimplifyMapPrinterLayer; if (mapPrinterLayer.BackgroundMask != null) { mapPrinterLayer.BackgroundMask.DrawingLevel = DrawingLevel.LevelOne; } mapPrinterLayer.DrawingMode = MapPrinterDrawingMode.Vector; } printerLayer.Draw(printerGeoCanvas, labelsInAllLayers); printerLayer.IsDrawing = false; printerGeoCanvas.Flush(); if (printerLayer is DatePrinterLayer) { ((DatePrinterLayer)printerLayer).DateString = DateElementViewModel.DefaultDate.ToString(((DatePrinterLayer)printerLayer).DateFormat); } } foreach (var item in printerOverlay.PrinterLayers.Where(l => l.GetType() == typeof(SimplifyMapPrinterLayer))) { (item as SimplifyMapPrinterLayer).DrawingMode = MapPrinterDrawingMode.Raster; } return(printDocument); } catch (Exception ex) { GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex)); return(printDocument); } finally { SetLegendIsPrinting(false, printerOverlay); SetDescriptionLabelVisible(true, printerOverlay); } }