using PdfSharpCore.Drawing; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using PdfiumViewer; namespace XGLFinishPro.Views { /// /// FlowInfoWin.xaml 的交互逻辑 /// public partial class SopInfoWin : Window { DataTable _dt; Image imgSop = new Image(); private System.Windows.Forms.WebBrowser webBrowser; public SopInfoWin() { InitializeComponent(); } public SopInfoWin(DataTable dt) { InitializeComponent(); _dt = dt; } private void Back_Click(object sender, RoutedEventArgs e) { this.Close(); } private void Window_Loaded(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Maximized; if (_dt == null || _dt.Rows.Count == 0) return; string address = _dt.Rows[0][0].ToString(); if (address.ToLower().Contains(".png") || address.ToLower().Contains(".jpg")) { windowsFormsHost.Visibility = Visibility.Collapsed; pdfViewer.Visible = false; System.Windows.Media.Imaging.BitmapImage src = new System.Windows.Media.Imaging.BitmapImage(); src.BeginInit(); src.UriSource = new Uri(address, UriKind.Relative); src.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; src.EndInit(); imgSop.Stretch = System.Windows.Media.Stretch.Fill; imgSop.Source = src; Window2.Content = new Frame { Content = imgSop }; } if (address.ToLower().Contains(".doc") || address.ToLower().Contains(".docx") || address.ToLower().Contains(".xls") || address.ToLower().Contains(".xlxs")) { imgSop.Visibility = Visibility.Collapsed; pdfViewer.Visible = false; windowsFormsHost.Visibility = Visibility.Visible; InitializeOfficeViewer(address); } if (address.ToLower().Contains(".pdf")) { windowsFormsHost.Visibility = Visibility.Collapsed; imgSop.Visibility = Visibility.Collapsed; pdfViewer.Visible = true; LoadPdfFile(address); } } private void LoadPdfFile(string filePath) { try { PdfDocument pdfDocument = PdfDocument.Load(filePath); pdfViewer.Document = pdfDocument; } catch (Exception ex) { // 处理加载 PDF 文件的异常 System.Windows.MessageBox.Show("无法加载 PDF 文件:" + ex.Message); } } private void InitializeOfficeViewer(string filePath) { webBrowser = new System.Windows.Forms.WebBrowser(); // Add WebBrowser control to WindowsFormsHost windowsFormsHost.Child = webBrowser; // Load and display the Office document LoadOfficeDocument("path_to_your_office_document.docx"); } private void LoadOfficeDocument(string filePath) { try { webBrowser.Navigate(filePath); } catch (Exception ex) { System.Windows.MessageBox.Show($"Error loading Office document: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } private void dgFlow_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.Header = (e.Row.GetIndex() + 1).ToString(); } } }