FrmDocViewer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using Forms = System.Windows.Forms;
  11. using System.Windows.Xps.Packaging;
  12. using Office = Core.Mes.Client.Comm.Office;
  13. namespace Core.StlMes.Client.Mcp.Control
  14. {
  15. public partial class FrmDocViewer : Form
  16. {
  17. private string _fileName ="";
  18. private DocType _docType = DocType.Picture;
  19. private Core.Mes.Client.Comm.WPF.DocumentViewer documentViewer1;
  20. private System.Windows.Forms.Integration.ElementHost elementHost1;
  21. private PictureBox pictureBox1;
  22. public FrmDocViewer(string fileName)
  23. {
  24. InitializeComponent();
  25. _fileName = fileName;
  26. if (!string.IsNullOrEmpty(fileName))
  27. {
  28. string extensions = fileName.Substring(_fileName.LastIndexOf(".")).ToUpper();
  29. if (extensions.Contains("JPG") || extensions.Contains("BMP"))
  30. {
  31. _docType = DocType.Picture;
  32. }
  33. else if(extensions.Contains("DOC"))
  34. {
  35. _docType = DocType.Word;
  36. }
  37. else if (extensions.Contains("XPS"))
  38. {
  39. _docType = DocType.XPS;
  40. }
  41. else if (extensions.Contains("PDF"))
  42. {
  43. _docType = DocType.PDF;
  44. }
  45. }
  46. documentViewer1 = new Core.Mes.Client.Comm.WPF.DocumentViewer();
  47. elementHost1 = new System.Windows.Forms.Integration.ElementHost();
  48. this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
  49. this.elementHost1.Location = new System.Drawing.Point(0, 0);
  50. this.elementHost1.Name = "elementHost1";
  51. this.elementHost1.Size = new System.Drawing.Size(552, 413);
  52. this.elementHost1.TabIndex = 5;
  53. this.elementHost1.Text = "elementHost1";
  54. this.elementHost1.Child = this.documentViewer1;
  55. pictureBox1 = new PictureBox();
  56. pictureBox1.Dock = DockStyle.Fill;
  57. pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
  58. switch (_docType)
  59. {
  60. case DocType.Word:
  61. case DocType.XPS:
  62. this.Controls.Add(elementHost1);
  63. break;
  64. case DocType.Picture:
  65. this.Controls.Add(pictureBox1);
  66. break;
  67. case DocType.PDF:
  68. break;
  69. }
  70. }
  71. private void FrmDocViewer_Load(object sender, EventArgs e)
  72. {
  73. ShowDoc();
  74. }
  75. private void ShowDoc()
  76. {
  77. if (!string.IsNullOrEmpty(_fileName))
  78. {
  79. switch (_docType)
  80. {
  81. case DocType.Word:
  82. case DocType.XPS:
  83. XpsDocument document = null;
  84. if (_docType == DocType.XPS)
  85. {
  86. //显示xps文档
  87. document = new XpsDocument(_fileName, FileAccess.Read);
  88. documentViewer1.Viewer.Document = document.GetFixedDocumentSequence();
  89. }
  90. else
  91. {
  92. //显示word文档
  93. string tempPath = Forms.Application.StartupPath + @"\cache";//xps缓存文件路径
  94. if (!Directory.Exists(tempPath))
  95. {
  96. Directory.CreateDirectory(tempPath);
  97. }
  98. //word先转换xps再显示
  99. document = Office.DocumentViewerHelper.ConvertWordToXps(_fileName, tempPath + @"\tempDoc.xps");
  100. documentViewer1.Viewer.Document = document.GetFixedDocumentSequence();
  101. }
  102. //关闭流
  103. document.Close();
  104. break;
  105. case DocType.Picture:
  106. pictureBox1.Image = Image.FromFile(_fileName);
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. public enum DocType
  113. {
  114. Picture = 0,
  115. Word = 1,
  116. PDF = 2,
  117. XPS = 3
  118. }
  119. }