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.Mes.Client.Comm.Test { public partial class FrmDocumentViewer : Form { private Core.Mes.Client.Comm.WPF.DocumentViewer documentViewer1; private System.Windows.Forms.Integration.ElementHost elementHost1; public FrmDocumentViewer() { InitializeComponent(); 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.panel2.Controls.Add(elementHost1); } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog(this) == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void button2_Click(object sender, EventArgs e) { string fileName = textBox1.Text.Trim(); 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(); } } } }