FrmDocumentViewer.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Mes.Client.Comm.Test
  14. {
  15. public partial class FrmDocumentViewer : Form
  16. {
  17. private Core.Mes.Client.Comm.WPF.DocumentViewer documentViewer1;
  18. private System.Windows.Forms.Integration.ElementHost elementHost1;
  19. public FrmDocumentViewer()
  20. {
  21. InitializeComponent();
  22. documentViewer1 = new Core.Mes.Client.Comm.WPF.DocumentViewer();
  23. elementHost1 = new System.Windows.Forms.Integration.ElementHost();
  24. this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
  25. this.elementHost1.Location = new System.Drawing.Point(0, 0);
  26. this.elementHost1.Name = "elementHost1";
  27. this.elementHost1.Size = new System.Drawing.Size(552, 413);
  28. this.elementHost1.TabIndex = 5;
  29. this.elementHost1.Text = "elementHost1";
  30. this.elementHost1.Child = this.documentViewer1;
  31. this.panel2.Controls.Add(elementHost1);
  32. }
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
  36. {
  37. textBox1.Text = openFileDialog1.FileName;
  38. }
  39. }
  40. private void button2_Click(object sender, EventArgs e)
  41. {
  42. string fileName = textBox1.Text.Trim();
  43. if (!string.IsNullOrEmpty(fileName))
  44. {
  45. XpsDocument document = null;
  46. if (fileName.Substring(fileName.LastIndexOf(".")).Contains("xps"))
  47. {
  48. //显示xps文档
  49. document = new XpsDocument(fileName, FileAccess.Read);
  50. documentViewer1.Viewer.Document = document.GetFixedDocumentSequence();
  51. }
  52. else
  53. {
  54. //显示word文档
  55. string tempPath = Forms.Application.StartupPath + @"\detectTemp";//xps缓存文件路径
  56. if (!Directory.Exists(tempPath))
  57. {
  58. Directory.CreateDirectory(tempPath);
  59. }
  60. //word先转换xps再显示
  61. document = Office.DocumentViewerHelper.ConvertWordToXps(fileName, tempPath + @"\temp.xps");
  62. documentViewer1.Viewer.Document = document.GetFixedDocumentSequence();
  63. }
  64. //关闭流
  65. document.Close();
  66. }
  67. }
  68. }
  69. }