FrmFilePreview.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Core.Mes.Client.Comm.Control;
  2. using Core.Mes.Client.Comm.Server;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using System.Windows.Xps.Packaging;
  8. using Forms = System.Windows.Forms;
  9. using Office = Core.Mes.Client.Comm.Office;
  10. namespace Core.StlMes.Client.Qcm
  11. {
  12. public partial class FrmFilePreview : Form
  13. {
  14. private Core.Mes.Client.Comm.WPF.DocumentViewer documentViewer1;
  15. private System.Windows.Forms.Integration.ElementHost elementHost1;
  16. private string filePath;
  17. public string FilePath
  18. {
  19. get { return filePath; }
  20. set { filePath = value; }
  21. }
  22. public FrmFilePreview()
  23. {
  24. InitializeComponent();
  25. }
  26. private void LoadData()
  27. {
  28. string str = filePath.Substring(filePath.LastIndexOf("."));
  29. if (str.Contains("xps") || str.Contains("doc") || str.Contains("docx") || str.Contains("txt"))
  30. {
  31. pictureBox1.Visible = false;
  32. documentViewer1 = new Core.Mes.Client.Comm.WPF.DocumentViewer();
  33. elementHost1 = new System.Windows.Forms.Integration.ElementHost();
  34. this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
  35. this.elementHost1.Location = new System.Drawing.Point(0, 0);
  36. this.elementHost1.Name = "elementHost1";
  37. this.elementHost1.Size = new System.Drawing.Size(552, 413);
  38. this.elementHost1.TabIndex = 5;
  39. this.elementHost1.Text = "elementHost1";
  40. this.elementHost1.Child = this.documentViewer1;
  41. this.panel1.Controls.Add(elementHost1);
  42. }
  43. }
  44. private void FrmFilePreview_Load(object sender, EventArgs e)
  45. {
  46. LoadData();
  47. string str = filePath.Substring(filePath.LastIndexOf("."));
  48. if (str.Contains("xps") || str.Contains("doc") || str.Contains("docx") || str.Contains("txt"))
  49. {
  50. this.WindowState = FormWindowState.Maximized;
  51. FileWatch();
  52. }
  53. else
  54. PictureWatch();
  55. }
  56. private void PictureWatch()
  57. {
  58. string pathName = "PhyRStd/" + this.FilePath;
  59. FileBean bean = null;
  60. List<FileBean> list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
  61. if (list.Count > 0)
  62. bean = list[0];
  63. if (bean != null)
  64. PictureBoxHelper.LoadThumbnailImage(bean.getFile(), pictureBox1);
  65. }
  66. private void FileWatch()
  67. {
  68. string pathName = "PhyRStd/" + this.FilePath;
  69. FileBean bean = null;
  70. string filename = "";
  71. List<FileBean> list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
  72. if (list.Count > 0)
  73. bean = list[0];
  74. if (bean != null)
  75. filename = ByteConvertWord(bean.getFile(), this.FilePath.Substring(0, FilePath.LastIndexOf(".")));
  76. this.FilePath = filename;
  77. DocumentPreview();
  78. }
  79. private void DocumentPreview()
  80. {
  81. string fileName = this.FilePath;
  82. if (!string.IsNullOrEmpty(fileName))
  83. {
  84. XpsDocument document = null;
  85. if (fileName.Substring(fileName.LastIndexOf(".")).Contains("xps"))
  86. {
  87. //显示xps文档
  88. document = new XpsDocument(fileName, FileAccess.Read);
  89. documentViewer1.Viewer.Document = document.GetFixedDocumentSequence();
  90. }
  91. else
  92. {
  93. //显示word文档
  94. string tempPath = Forms.Application.StartupPath + @"\detectTemp";//xps缓存文件路径
  95. if (!Directory.Exists(tempPath))
  96. {
  97. Directory.CreateDirectory(tempPath);
  98. }
  99. //word先转换xps再显示
  100. document = Office.DocumentViewerHelper.ConvertWordToXps(fileName, tempPath + @"\temp.xps");
  101. documentViewer1.Viewer.Document = document.GetFixedDocumentSequence();
  102. }
  103. //关闭流
  104. document.Close();
  105. }
  106. }
  107. /// <summary>
  108. /// 二进制转化为Word
  109. /// </summary>
  110. /// <param name="data">二进制数组</param>
  111. /// <param name="fileName">文件名</param>
  112. /// <returns>word存放的位置</returns>
  113. public string ByteConvertWord(byte[] data, string fileName)
  114. {
  115. string savePath = @"\SystemWord\" + FormatNowTime(2) + @"\";
  116. if (!System.IO.Directory.Exists(GetPath() + savePath))
  117. {
  118. Directory.CreateDirectory(GetPath() + savePath);
  119. }
  120. savePath += fileName + ".doc";
  121. string filePath = GetPath() + savePath;
  122. FileStream fs;
  123. if (System.IO.File.Exists(filePath))
  124. {
  125. fs = new FileStream(filePath, FileMode.Truncate);
  126. }
  127. else
  128. {
  129. fs = new FileStream(filePath, FileMode.CreateNew);
  130. }
  131. BinaryWriter br = new BinaryWriter(fs);
  132. br.Write(data, 0, data.Length);
  133. br.Close();
  134. fs.Close();
  135. return filePath;
  136. }
  137. /// <summary>
  138. ///项目所在目录
  139. /// </summary>
  140. /// <returns></returns>
  141. public string GetPath()
  142. {
  143. return Forms.Application.StartupPath;
  144. }
  145. /// <summary>
  146. ///格式化当前时间:
  147. /// 1:yyMMddHHmmss; 2:yyyy-MM\dd\
  148. /// </summary>
  149. /// <returns></returns>
  150. public string FormatNowTime(int num)
  151. {
  152. if (num == 1)
  153. {
  154. return DateTime.Now.ToString("yyMMddHHmmss");
  155. }
  156. else if (num == 2)
  157. {
  158. return DateTime.Now.ToString("yyyy-MM") + @"\" + DateTime.Now.Day;
  159. }
  160. return "";
  161. }
  162. }
  163. }