using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using Forms = System.Windows.Forms; using System.Windows.Xps.Packaging; using Office = Core.Mes.Client.Comm.Office; namespace Core.StlMes.Client.Mcp.Control { public partial class FrmDocViewer : Form { private string _fileName =""; private DocType _docType = DocType.Picture; private Core.Mes.Client.Comm.WPF.DocumentViewer documentViewer1; private System.Windows.Forms.Integration.ElementHost elementHost1; private PictureBox pictureBox1; public FrmDocViewer(string fileName) { InitializeComponent(); _fileName = fileName; if (!string.IsNullOrEmpty(fileName)) { string extensions = fileName.Substring(_fileName.LastIndexOf(".")).ToUpper(); if (extensions.Contains("JPG") || extensions.Contains("BMP")) { _docType = DocType.Picture; } else if(extensions.Contains("DOC")) { _docType = DocType.Word; } else if (extensions.Contains("XPS")) { _docType = DocType.XPS; } else if (extensions.Contains("PDF")) { _docType = DocType.PDF; } } 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; pictureBox1 = new PictureBox(); pictureBox1.Dock = DockStyle.Fill; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; switch (_docType) { case DocType.Word: case DocType.XPS: this.Controls.Add(elementHost1); break; case DocType.Picture: this.Controls.Add(pictureBox1); break; case DocType.PDF: break; } } private void FrmDocViewer_Load(object sender, EventArgs e) { ShowDoc(); } private void ShowDoc() { if (!string.IsNullOrEmpty(_fileName)) { switch (_docType) { case DocType.Word: case DocType.XPS: XpsDocument document = null; if (_docType == DocType.XPS) { //显示xps文档 document = new XpsDocument(_fileName, FileAccess.Read); documentViewer1.Viewer.Document = document.GetFixedDocumentSequence(); } else { //显示word文档 string tempPath = Forms.Application.StartupPath + @"\cache";//xps缓存文件路径 if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } //word先转换xps再显示 document = Office.DocumentViewerHelper.ConvertWordToXps(_fileName, tempPath + @"\tempDoc.xps"); documentViewer1.Viewer.Document = document.GetFixedDocumentSequence(); } //关闭流 document.Close(); break; case DocType.Picture: pictureBox1.Image = Image.FromFile(_fileName); break; } } } } public enum DocType { Picture = 0, Word = 1, PDF = 2, XPS = 3 } }