| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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<FileBean> 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<FileBean> 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();
- }
- }
- /// <summary>
- /// 二进制转化为Word
- /// </summary>
- /// <param name="data">二进制数组</param>
- /// <param name="fileName">文件名</param>
- /// <returns>word存放的位置</returns>
- 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;
- }
- /// <summary>
- ///项目所在目录
- /// </summary>
- /// <returns></returns>
- public string GetPath()
- {
- return Forms.Application.StartupPath;
- }
- /// <summary>
- ///格式化当前时间:
- /// 1:yyMMddHHmmss; 2:yyyy-MM\dd\
- /// </summary>
- /// <returns></returns>
- 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 "";
- }
- }
- }
|