using DTS.Common.Base; using DTS.Common.Events; using DTS.Common.Interface; using System; using System.Threading; using System.Threading.Tasks; using System.IO; using C1.WPF.Pdf; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Media; using DTS.Common.Utilities.Logging; using Prism.Events; using Prism.Ioc; // ReSharper disable CheckNamespace namespace DTS.Viewer { /// /// Interaction logic for ViewerMainViewGrid.xaml /// public partial class ExportMainViewGrid : IExportMainViewGrid { public ExportMainViewGrid() { InitializeComponent(); Loaded += ExportMainViewGrid_Loaded; } private IEventAggregator _eventAggregator { get; set; } private void SetFocus() { } //FB 14797 Subscribe to event after the page loaded to make sure eventAggregator is available private void ExportMainViewGrid_Loaded(object sender, System.Windows.RoutedEventArgs e) { _eventAggregator = ContainerLocator.Container.Resolve(); _eventAggregator?.GetEvent().Subscribe(OnGraphLoadedCountNotification); } //FB 14797 Set the focus to first tab (chartOptTab) after the graph is fully loaded (3 sec) private void OnGraphLoadedCountNotification(GraphLoadedCountNotificationArg arg) { if (null != DataContext && (IBaseViewModel)DataContext != arg.ParentVM) return; Task.Run(() => { Thread.Sleep(TimeSpan.FromSeconds(3)); Dispatcher.BeginInvoke((System.Action)SetFocus); }); } /// /// Returns > 0 if successful, < 0 if failure, or 0 if the MainChart is not ready yet /// /// /// /// public int SaveToPDF(string directory, string pdfFileName) { try { //FB 29493 Error message in log before success message if (MainShell.DesiredSize.Height == 0 || MainShell.DesiredSize.Width == 0) return 0; if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(pdfFileName) || !pdfFileName.EndsWith(".pdf")) return -1; var bounds = VisualTreeHelper.GetDescendantBounds(MainShell); // desired resolution: double dpiX = 300, dpiY = 300; var bmp = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0), (int)(bounds.Height * dpiY / 96.0), dpiX, dpiY, PixelFormats.Pbgra32); bmp.Render(MainShell); var wBmp = new WriteableBitmap(bmp); var pdf = new C1PdfDocument { Landscape = true, PageSize = new Size(MainShell.ActualWidth, MainShell.ActualHeight) }; pdf.DrawImage(wBmp, new Rect(0, 0, MainShell.ActualWidth, MainShell.ActualHeight)); pdf.Save(Path.Combine(directory, pdfFileName)); DTS.Common.Utils.PNGImageUtil.SaveImage(MainShell, System.IO.Path.Combine(directory, pdfFileName.Replace(".pdf", ".png"))); return 1; } catch (Exception ex) { APILogger.LogException(ex); } return -1; } } }