using Core.Mes.Client.Comm.Control; using Core.Mes.Client.Comm.Server; using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Windows.Xps.Packaging; using Forms = System.Windows.Forms; using Office = Core.Mes.Client.Comm.Office; namespace Core.StlMes.Client.Qcm { public partial class FrmFilePreview : Form { private Core.Mes.Client.Comm.WPF.DocumentViewer documentViewer1; private System.Windows.Forms.Integration.ElementHost elementHost1; private string filePath; public string FilePath { get { return filePath; } set { filePath = value; } } public FrmFilePreview() { InitializeComponent(); } private void LoadData() { string str = filePath.Substring(filePath.LastIndexOf(".")); if (str.Contains("xps") || str.Contains("doc") || str.Contains("docx") || str.Contains("txt")) { pictureBox1.Visible = false; documentViewer1 = new Core.Mes.Client.Comm.WPF.DocumentViewer(); elementHost1 = new System.Windows.Forms.Integration.ElementHost(); this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill; this.elementHost1.Location = new System.Drawing.Point(0, 0); this.elementHost1.Name = "elementHost1"; this.elementHost1.Size = new System.Drawing.Size(552, 413); this.elementHost1.TabIndex = 5; this.elementHost1.Text = "elementHost1"; this.elementHost1.Child = this.documentViewer1; this.panel1.Controls.Add(elementHost1); } } private void FrmFilePreview_Load(object sender, EventArgs e) { LoadData(); string str = filePath.Substring(filePath.LastIndexOf(".")); if (str.Contains("xps") || str.Contains("doc") || str.Contains("docx") || str.Contains("txt")) { this.WindowState = FormWindowState.Maximized; FileWatch(); } else PictureWatch(); } private void PictureWatch() { string pathName = "PhyRStd/" + this.FilePath; FileBean bean = null; List list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName); if (list.Count > 0) bean = list[0]; if (bean != null) PictureBoxHelper.LoadThumbnailImage(bean.getFile(), pictureBox1); } private void FileWatch() { string pathName = "PhyRStd/" + this.FilePath; FileBean bean = null; string filename = ""; List list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName); if (list.Count > 0) bean = list[0]; if (bean != null) filename = ByteConvertWord(bean.getFile(), this.FilePath.Substring(0, FilePath.LastIndexOf("."))); this.FilePath = filename; DocumentPreview(); } private void DocumentPreview() { string fileName = this.FilePath; if (!string.IsNullOrEmpty(fileName)) { XpsDocument document = null; if (fileName.Substring(fileName.LastIndexOf(".")).Contains("xps")) { //显示xps文档 document = new XpsDocument(fileName, FileAccess.Read); documentViewer1.Viewer.Document = document.GetFixedDocumentSequence(); } else { //显示word文档 string tempPath = Forms.Application.StartupPath + @"\detectTemp";//xps缓存文件路径 if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } //word先转换xps再显示 document = Office.DocumentViewerHelper.ConvertWordToXps(fileName, tempPath + @"\temp.xps"); documentViewer1.Viewer.Document = document.GetFixedDocumentSequence(); } //关闭流 document.Close(); } } /// /// 二进制转化为Word /// /// 二进制数组 /// 文件名 /// word存放的位置 public string ByteConvertWord(byte[] data, string fileName) { string savePath = @"\SystemWord\" + FormatNowTime(2) + @"\"; if (!System.IO.Directory.Exists(GetPath() + savePath)) { Directory.CreateDirectory(GetPath() + savePath); } savePath += fileName + ".doc"; string filePath = GetPath() + savePath; FileStream fs; if (System.IO.File.Exists(filePath)) { fs = new FileStream(filePath, FileMode.Truncate); } else { fs = new FileStream(filePath, FileMode.CreateNew); } BinaryWriter br = new BinaryWriter(fs); br.Write(data, 0, data.Length); br.Close(); fs.Close(); return filePath; } /// ///项目所在目录 /// /// public string GetPath() { return Forms.Application.StartupPath; } /// ///格式化当前时间: /// 1:yyMMddHHmmss; 2:yyyy-MM\dd\ /// /// public string FormatNowTime(int num) { if (num == 1) { return DateTime.Now.ToString("yyMMddHHmmss"); } else if (num == 2) { return DateTime.Now.ToString("yyyy-MM") + @"\" + DateTime.Now.Day; } return ""; } } }